April 14, 2014_Monday
namespace UsingComboBox_Control
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//The main property of ComboBox is: Items.
//It is an array (or collection) that holds
//a list of items to be displayed in the ComboBox
//To display an item in the ComboBox, you should
// add it to the ComboBox Items property
cboCities.Items.Add("Seattle");
cboCities.Items.Add("Portland");
//you can use the Add method to add one item at a time.
//If you define a string array
//you can add the entire array in a single statement
string[] cities = { "New York", "Vancouver", "Houston", "Dallas", "Miami", "Los Angeles", "San Fransisco", "San Diego" };
//add the entire array to the ComboBox Items
cboCities.Items.AddRange(cities);
//Pre-select a city (default city)
// cboCities.SelectedIndex = 0;
//or
cboCities.Text = "Seattle";
//==================================================================
string[] countries = { "USA", "Canada", "Mexico", "Brazil", "Argentina", "Peru", "UK", "Germany", "Spain", "China", "India", "Pakistan", "Russia", "Poland", "Egypt", "Syria" };
cboCountries.Items.AddRange(countries);
cboCountries.SelectedIndex = 0;
}
private void btnGetCity_Click(object sender, EventArgs e)
{
//get the selected city from the ComboBox
//cboCities
//option 1: using the Text property
string selectedCity = cboCities.Text;
//option 2: using the SelectedItem property
//string selectedCity = cboCities.SelectedItem.ToString();
//display it
txtSelectedItem.Text = selectedCity;
}
private void cboCountries_SelectedIndexChanged(object sender, EventArgs e)
{
//code here runs every time a new selection is made
//get selected country
string selectedCountry = cboCountries.Text;
//display it to the textbox.
txtSelectedCountry.Text = selectedCountry;
}
}
}

No comments:
Post a Comment