comboBox.SelectedIndex的问题:下拉列表中选择好了,但是selectedindex值不变!

yangyi_33855 2011-04-09 03:16:21
comboBox1控件的设置;
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
"测试",
"0",
"1"});
this.comboBox1.Location = new System.Drawing.Point(17, 59);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(102, 20);
this.comboBox1.TabIndex = 0;
窗体的构造函数:
InitializeComponent();
comboBox1.SelectedIndex = 0;
if (comboBox1.SelectedIndex == 1)
{
label1.Text = "选择了第一个数";
}
else if (comboBox1.SelectedIndex == 2)
{
label1.Text = "选择了第二个数";
}

不是说选中comboBox控件下拉列表中,SelectedIndex的值会跟着变么?
...全文
1989 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
liulangdeyuyu 2011-04-09
  • 打赏
  • 举报
回复
yangyi_33855 2011-04-09
  • 打赏
  • 举报
回复
我草,问题我自己解决了,日了,就是没有使用任何事件处理函数,我这样搞,就算选好了,也只算是预处理,程序还没有对我的选择操作做处理,所以不能实现我想要的结果!
还是谢谢楼上这位兄弟热心指导了!
必须的。
heavenopener 2011-04-09
  • 打赏
  • 举报
回复 1

if (comboBox1.SelectedIndex == 1)
{
label1.Text = "选择了第一个数";
}
else if (comboBox1.SelectedIndex == 2)
{
label1.Text = "选择了第二个数";
}

这段代码不应该加在窗体构造函数中,应该加载SelectChanged事件处理的函数中。你给下拉列表加个SelectChanged事件。
public partial class Form1 : Form { public Form1() { InitializeComponent(); } SerialPort port1 = new SerialPort(); string InputData = String.Empty; delegate void SetTextCallback(string text); private void Port_Select() {//获取机器的串口地址 string[] ports = SerialPort.GetPortNames(); foreach (string port in ports) { comboBox1.Items.Add(port); } } private void Form1_Load_1(object sender, EventArgs e) { Port_Select(); this.comboBox1.SelectedIndex = 0; this.comboBox2.SelectedIndex = 0; } private void button1_Click(object sender, EventArgs e) { if (button1.Text == "关闭串口") //当要关闭串口的时候 { port1.DiscardOutBuffer(); port1.DiscardInBuffer(); port1.Close(); button1.Text = "打开串口"; label3.Text = "串口当前状况:未打开"; comboBox1.Enabled = true; comboBox2.Enabled = true; } else if (button1.Text == "打开串口") //当要打开串口的时候 { try { port1.PortName = comboBox1.SelectedItem.ToString(); port1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem); port1.DataBits = 8; port1.RtsEnable = true; port1.Open(); port1.DiscardOutBuffer(); port1.DiscardInBuffer(); button1.Text = "关闭串口"; comboBox1.Enabled = false; comboBox2.Enabled = false; label3.Text = "串口:" + comboBox1.SelectedItem.ToString() + " 波特率:" + comboBox2.SelectedItem.ToString() + " 数据位:8 "; } catch { button1.Text = "打开串口"; label3.Text = "串口:" + comboBox1.SelectedItem.ToString() + "打开失败"; MessageBox.Show("该串口无法打开"); } } } 资源部分代码
Public Class 控件代码 '全局变量 - TrackBar1 改变图片大小使用 Dim l As Double Dim t As Double #Region "时间控件 标签控件 链接标签控件 日期控件 下拉菜单控件 " '窗体的设置 Private Sub 控件代码_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '设置时间控件启用 Timer1.Enabled = True '设置窗体不可改变大小 Me.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink '设置窗体启动居 Me.StartPosition = FormStartPosition.CenterScreen '设置窗体启动最大/最小化 'Me.WindowState = FormWindowState.Maximized / FormWindowState.Minimized '启动自动加载listbox1项目 '调用方法 加载listbox选项 SelectItems() '設定DataGridView1滿屏 DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill '加载树形菜单视图 LoadTreeView() 'ComboBox1 启动加载选项 '启动清空 ComboBox1 ComboBox1.Items.Clear() Me.ComboBox1.Items.Add("ComboBox1启动加载选项") Dim strSql As String = "SELECT EmpID FROM VWEmployees" Dim ds As DataSet = GetDataSet(strSql) If ds.Tables.Item(0).Rows.Count = 0 Then MessageBox.Show("资料不存在") Else For i As Integer = 0 To ds.Tables.Item(0).Rows.Count - 1 Me.ComboBox1.Items.Add(ds.Tables.Item(0).Rows(i).Item(0)) Next ComboBox1.SelectedIndex = 0 End If '记录画面起始 - 距左侧 顶部的距离 l = PictureBox1.Location.X.ToString() t = PictureBox1.Location.Y.ToString() End Sub '时间控件的设置 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick TextBox1.Text = Now & " 系统时间" End Sub '链接标签的用法 - 两种打开方式 Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked '启动的是默认浏览器 - 推荐使用 System.Diagnostics.Process.Start("http://www.baidu.com/") 'Shell("explorer.exe http://www.baidu.com/") End Sub #End Region

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧