![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
关于C#的WinForm编程:我在comboBox1_SelectedIndexChanged事件里面修改Text值为什么显示出来愣是不对??
以下代码,当选择下列列表某一项后,点按钮可以检测出各个事件发生的先后顺序与当时comboBox1的Text值,奇怪的是,我在SelectedIndexChanged里面修...
以下代码,当选择下列列表某一项后,点按钮可以检测出各个事件发生的先后顺序与当时comboBox1的Text值,奇怪的是,我在SelectedIndexChanged里面修改comboBox1.Text的值时,在TextChanged事件里面发现comboBox1.Text已经被修改成想要的新值了,可是界面显示出来的值愣是没有改变!这到底怎么回事啊?如何才能达到显示新值的效果?
string s = "",t="";
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("A");
comboBox1.Items.Add("B");
comboBox1.Items.Add("C");
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = comboBox1.Text;
textBox1.Text = s;
s = "";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
s = s + "SelectedIndexChanged = " + comboBox1.Text + "\r\n";
comboBox1.Text = t + "," + comboBox1.Text; //原内容与新内容合并显示
}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
s = s + "DropDownClosed = " + comboBox1.Text + "\r\n";
t = comboBox1.Text; //备份原来的内容
}
private void comboBox1_TextChanged(object sender, EventArgs e)
{
s = s + "TextChanged = " + comboBox1.Text + "\r\n";
}
我的目的是:在手工修改comboBox1.Text值时,程序不要串接其他内容进去,但如果是点击下列列表选择某项内容,程序就要串接其他内容进去再一并显示在comboBox1.Text里面。 展开
string s = "",t="";
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("A");
comboBox1.Items.Add("B");
comboBox1.Items.Add("C");
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = comboBox1.Text;
textBox1.Text = s;
s = "";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
s = s + "SelectedIndexChanged = " + comboBox1.Text + "\r\n";
comboBox1.Text = t + "," + comboBox1.Text; //原内容与新内容合并显示
}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
s = s + "DropDownClosed = " + comboBox1.Text + "\r\n";
t = comboBox1.Text; //备份原来的内容
}
private void comboBox1_TextChanged(object sender, EventArgs e)
{
s = s + "TextChanged = " + comboBox1.Text + "\r\n";
}
我的目的是:在手工修改comboBox1.Text值时,程序不要串接其他内容进去,但如果是点击下列列表选择某项内容,程序就要串接其他内容进去再一并显示在comboBox1.Text里面。 展开
2个回答
展开全部
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
s = s + "SelectedIndexChanged = " + comboBox1.Text + "\r\n";
BeginInvoke(new EventHandler(InvokeAfter), sender, e);
}
void InvokeAfter(object sender, EventArgs e)
{
comboBox1.Text = (t + "," + comboBox1.Text).TrimStart(','); //原内容与新内容合并显示}
{
s = s + "SelectedIndexChanged = " + comboBox1.Text + "\r\n";
BeginInvoke(new EventHandler(InvokeAfter), sender, e);
}
void InvokeAfter(object sender, EventArgs e)
{
comboBox1.Text = (t + "," + comboBox1.Text).TrimStart(','); //原内容与新内容合并显示}
追问
懒得试了 改其他了
追答
感谢你改其他还采纳我的答案。谢谢你。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询