第三节 8下拉列表
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace _8下拉列表 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //取得combox的几种方式 MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//索引 MessageBox.Show(Convert.ToString(comboBox1.SelectedValue)); MessageBox.Show(Convert.ToString(comboBox1.SelectedText)); MessageBox.Show(Convert.ToString(comboBox1.SelectedItem)); //文本 } private void button2_Click(object sender, EventArgs e) { string str1 = tb_start.Text; string str2 = tb_end.Text; int i1, i2; if (!int.TryParse(str1, out i1)) { MessageBox.Show("开始数值必须为整数!"); return; } if (!int.TryParse(str2, out i2)) { MessageBox.Show("结束数值必须为整数!"); return; } int sum = 0; switch (comboBox2.SelectedIndex) { case 0://+ sum = i1 + i2; break; case 1://- sum = i1 - i2; break; case 2://* sum = i1 * i2; break; case 3:// / if (i2 == 0) { MessageBox.Show("被除数不能为0!"); return; } sum = i1 / i2; break; default: throw new Exception("无法认别的计算符!"); } tb_sum.Text = Convert.ToString(sum); } private void tb_private_SelectedIndexChanged(object sender, EventArgs e) { string str = Convert.ToString(tb_private.SelectedItem); tb_city.Items.Clear(); //清空tb_city中的所有元素 if (str == "四川") { tb_city.Items.Add("成都"); tb_city.Items.Add("巴中"); tb_city.Items.Add("达州"); } else if (str == "江西") { tb_city.Items.Add("南昌"); tb_city.Items.Add("九江"); } else if (str == "安徽") { tb_city.Items.Add("黄山"); tb_city.Items.Add("平湖"); } //MessageBox.Show("显示"); } } }