How To Add and Clear Items in A ListBox Control
How To Add and Clear Items in A ListBox Control
This example adds the contents of a Windows Forms TextBox control to a ListBox control when you click
button1, and clears the contents when you click button2.
Example
Copy Code
Example
Copy Code
1
sender, System.EventArgs e)
{
if ((string)listBox1.SelectedItem == "Two")
MessageBox.Show((string)listBox1.SelectedItem);
}
Note:
This code can also be used with a ComboBox control by substituting
a ComboBox control named comboBox1 for the ListBox control and
changing the code from listBox1 to comboBox1.
Example
Copy Code
myList[0] = "One";
myList[1] = "Two";
myList[2] = "Three";
myList[3] = "Four";
listBox1.Items.AddRange(myList);
}
Note:
This code can also be used with a ComboBox control by substituting
a ComboBox control named comboBox1 for the ListBox control and
2
changing the code from listBox1 to comboBox1.
Robust Programming
The following conditions may cause an exception:
The array contains one or more null values.
Example
Copy Code
3
This example requires:
A form with a ListBox control named listBox1 and a Button control named button1. Set the
button1Click event handler to button1_Click.
Note:
This code can also be used with a ComboBox control by substituting
a ComboBox control named comboBox1 for the ListBox control
and changing the code from listBox1 to comboBox1.
You can display the date on a Windows Form by using calendar controls, such as the MonthCalendar control
or a DateTimePicker control. The DateTimePicker control also enables you to display the time.
You can also use these controls to collect input from the user to use the date or time selected elsewhere in
your application. The MonthCalendar control enables you to select a range of dates. For more information,
see How to: Select a Range of Dates in a Calendar Control.
this.label1.Text =
this.monthCalendar1.SelectionRange.Start.ToShortDateString(
6. Create a DateChanged event handler for the MonthCalendar1 control. You can do this by double-
clicking the control in the designer.
7. Add the following code to the MonthCalendar_DateChanged event handler. This code sets the
label to the selected date, but this time in long date format.
C# Copy Code
this.label1.Text =
this.monthCalendar1.SelectionRange.Start.ToShortDateString(
4
3. Add the following code to the Form1_Load event handler. This code sets the format of the
control to display a time, instead of a date, and lets the user change the time that is
displayed.
C# Copy Code
this.dateTimePicker1.Format =
DateTimePickerFormat.Time;
this.dateTimePicker1.Width = 100;
this.dateTimePicker1.ShowUpDown = true;
4. Add a Button control to the form, and change the following properties.
Property Value
Name currentTime
this.dateTimePicker1.Value = DateTime.Now;
See Also
Concepts
Date and Time Controls
Designing a User Interface in Visual C# Express
Visual C# Guided Tour
Example
Copy Code
See Also
Concepts
Date and Time Controls
Designing a User Interface in Visual C# Express
Visual C# Guided Tour
Example
Copy Code
Example
Copy Code
6
private void displayTime()
{
Label1.Text = DateTime.Now.ToShortTimeString();
}
See Also
Concepts
Date and Time Controls
Example
Copy Code
// Difference in days.
int differenceInDays = ts.Days;