C# Task
C# Task
C# TextBox Properties
You can set background color and foreground color through property
window and programmatically.
textBox1.BackColor = Color.Blue;
textBox1.ForeColor = Color.White;
Textbox BorderStyle
You can set 3 different types of border style for textbox, they are
None, FixedSingle and fixed3d.
textBox1.BorderStyle = BorderStyle.Fixed3D;
textBox1.MaxLength = 40;
C# ComboBox Control
C# controls are located in the Toolbox of the development
environment, and you use them to create objects on a form with a
simple series of mouse clicks and dragging motions. A ComboBox
displays a text box combined with a ListBox, which enables the user
to select items from the list or enter a new value .
How add a item to combobox
comboBox1.Items.Add("Sunday");
comboBox1.Items.Add("Monday");
comboBox1.Items.Add("Tuesday");
ComboBox SelectedItem
How to retrieve value from ComboBox
string var;
var = comboBox1.Text;
Or
var item = this.comboBox1.GetItemText(this.comboBox1.SelectedItem);
MessageBox.Show(item);
You can remove items from a combobox in two ways. You can
remove item at a the specified index or giving a specified item by
name.
comboBox1.Items.RemoveAt(1);
The above code will remove the second item from the combobox.
comboBox1.Items.Remove("Friday");
The above code will remove the item "Friday" from the combobox.
DropDownStyle
comboBox1.Items.Add("test1");
comboBox1.Items.Add("test2");
comboBox1.Items.Add("test3");
comboBox1.SelectedItem = "test3";
or
comboBox1.SelectedIndex = comboBox1.FindStringExact("test3");
ComboBox DataSource Property
How to populate a combo box with a DataSet ?
comboBox1.DataSource = ds.Tables[0];
comboBox1.ValueMember = "au_id";
comboBox1.DisplayMember = "au_lname";
C# ListBox Control
The ListBox control enables you to display a list of items to the user
that the user can select by clicking.
First you should create a fresh List Object and add items to the List.
The next step is to bind this List to the Listbox. In order to do that
you should set datasource of the Listbox.
listBox1.DataSource = nList;
Full Source code
First you should create a connection string and fetch data from
database to a Dataset.
listBox1.DataSource = ds.Tables[0];
listBox1.ValueMember = "au_id";
listBox1.DisplayMember = "au_lname";
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string connetionString = null;
SqlConnection connection;
SqlCommand command;
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
int i = 0;
string sql = null;
//connetionString = "Data Source=ServerName;Initial
Catalog=databasename;User ID=userid;Password=yourpassword";
//sql = "select au_id,au_lname from authors";
connection = new SqlConnection(connetionString);
try
{
connection.Open();
command = new SqlCommand(sql, connection);
adapter.SelectCommand = command;
adapter.Fill(ds);
adapter.Dispose();
command.Dispose();
connection.Close();
listBox1.DataSource = ds.Tables[0];
listBox1.ValueMember = "au_id";
listBox1.DisplayMember = "au_lname";
}
catch (Exception ex)
{
MessageBox.Show("Cannot open connection ! ");
}
}
}
}
When you want to clear the Listbox, if the ListBox already binded
with Datasource, you have to set the Datasource of Listbox as null.
listBox1.DataSource = null;
You can add the event handler using the Properties Window and
selecting the Event icon and double-clicking on
SelectedIndexChanged as you can see in following image.
The event will fire again when you select a new item. You can write
your code within SelectedIndexChanged event . When you double
click on ListBox the code will automatically come in you code editor
like the following image.
From the following example you can understand how to fire the
SelectedIndexChanged event
First you should drag two listboxes on your Form. First listbox you
should set the List as Datasource, the List contents follows:
When you load this form you can see the listbox is populated with
List and displayed first quarter and second quarter. When you click
the "Fist Quarter" the next listbox is populated with first quarter
months and when you click "Second Quarter" you can see the
second listbox is changed to second quarter months. From the
following program you can understand how this happened.
C# RadioButton Control
A radio button or option button enables the user to select a single
option from a group of choices when paired with other RadioButton
controls. When a user clicks on a radio button, it becomes checked,
and all other radio buttons with same group become unchecked
The RadioButton control can display text, an Image, or both. Use
the Checked property to get or set the state of a RadioButton.
radioButton1.Checked = true;
The radio button and the check box are used for different functions.
Use a radio button when you want the user to choose only one
option. When you want the user to choose all appropriate options,
use a check box. Like check boxes, radio buttons support a Checked
property that indicates whether the radio button is selected.
RadioButton Properties
Checked:-
Gets or sets a value indicating whether the control is checked.
Examples
The following code example evaluates a ListBox selection and the Checked property of a RadioButton.
PerformClick method of
When a specified item is selected from the list box, the
another RadioButton is called. This example requires that two RadioButton controls and
a ListBox have been instantiated on a form.
private void ClickMyRadioButton()
{
// If Item1 is selected and radioButton2
// is checked, click radioButton1.
if (listBox1.Text == "Item1" && radioButton2.Checked)
{
radioButton1.PerformClick();
}
}
Property Value
Type: System.Drawing.Image
The Image displayed on the button control. The default value is null.
ButtonBase.Image Property
Image:
Syntax
C#
C++
F#
VB
public Image Image { get; set; }
Property Value
Type: System.Drawing.Image
The Image displayed on the button control. The default value is null.
Remarks
When the Image property is set, the ImageList property will be set to null, and
the ImageIndex property will be set to its default, -1.
Note
Control.Font Property
Font:-
Examples
The following code example displays a FontDialog to the user and changes the Font of
a DateTimePicker control. This example requires that you have a Form with Button and
a DateTimePicker on it.
private void myButton_Click(object sender, EventArgs e)
{
FontDialog myFontDialog = new FontDialog();
if(myFontDialog.ShowDialog() == DialogResult.OK)
{
// Set the control's font.
myDateTimePicker.Font = myFontDialog.Font;
}
}
LinkLabel Class
Represents a Windows label control that can display hyperlinks.
LinkBehavior Property
Gets or sets a value that represents the behavior of a link.
Examples
The following example demonstrates using the LinkLabel class, with multiple LinkArea sections
defined, to display a label on a form. The example demonstrates setting
the AutoSize, LinkBehavior, DisabledLinkColor, LinkColor, and VisitedLinkColor properties to
customize the look of the LinkLabel. The first LinkArea is specified using
the LinkLabel.LinkArea property. Additional links are added to the LinkLabel using
the LinkLabel.LinkCollection.Add method. The example handles the LinkClicked event by starting
the Web browser for hyperlinks, and displaying a MessageBox for other links.
using System;
using System.Drawing;
using System.Windows.Forms;
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public Form1()
{
// Create the LinkLabel.
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.linkLabel1.TabIndex = 0;
this.linkLabel1.TabStop = true;
// Add an event handler to do something when the links are clicked.
this.linkLabel1.LinkClicked += new
System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkCli
cked);
// Create new links using the Add method of the LinkCollection class.
// Underline the appropriate words in the LinkLabel's Text property.
// The words 'Register', 'Microsoft', and 'MSN' will
// all be underlined and behave as hyperlinks.
// Set up how the form should be displayed and add the controls to
the form.
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[]
{this.linkLabel1});
this.Text = "Link Label Example";
}
Anchor Property
Gets or sets the edges of the container to which a control is bound and determines how a control is
resized with its parent.
Examples
The following code example adds a Button to a form and sets some of its common properties. The
example anchors the button to the bottom-right corner of the form so it keeps its relative position as the
form is resized. Next it sets the BackgroundImage and resizes the button to the same size as
the Image. The example then sets the TabStop to true and sets the TabIndex property. Lastly, it adds
an event handler to handle the Click event of the button. This example requires that you have
an ImageList named imageList1.
// Add a button to a form and set some of its common properties.
private void AddMyButton()
{
// Create a button and add it to the form.
Button button1 = new Button();
// Specify the layout style of the background image. Tile is the default.
button1.BackgroundImageLayout = ImageLayout.Center;
AutoSize Property
Gets or sets a value indicating whether the control is automatically resized to display its entire
contents.
Examples
The following code example demonstrates the AutoSize property. To run this example, paste the following
code in a form and call the InitializeLabel method from the form's constructor or Load method
// Declare a label.
internal System.Windows.Forms.Label Label1;
// Set the label to a small size, but set the AutoSize property
// to true. The label will adjust its length so all the text
// is visible, however if the label is wider than the form,
// the entire label will not be visible.
this.Label1.Size = new System.Drawing.Size(10, 10);
this.Controls.Add(this.Label1);
this.Label1.AutoSize = true;
this.Label1.Text = "The text in this label is longer" +
" than the set size.";