获取ComboBox的先前值 [英] Getting previous value of the ComboBox
问题描述
我希望我的应用程序获取ComboBox的值,然后设置用户选择的ComboBox或以某种方式获取先前选择的值.
I want my application to grab the value of a ComboBox and then to set the one chosen by the user or to somehow get the previously selected value.
问题是,在我的表单中,有四个列表和一个ComboBox(其中包含列表中的所有值),我想将ComboBox的值重新填充回它来自的列表,然后删除其他/相同列表中新选择的项目.
The thing is that within my Form, there are four lists and a ComboBox (which contains all the values from the lists) and I want to repopulate the value of the ComboBox back to the list it was taken from and then remove the newly selected item from other/same list.
推荐答案
您要处理 SelectedItem
或 SelectedValue
到成员变量.然后,无论何时需要,您都可以将该值重新分配给ComboBox.
You want to handle the ComboBox.Enter
event. Then save off the SelectedItem
or SelectedValue
to a member variable. Whenever you want then, you can re-assign that value to the ComboBox.
注册活动.您可以通过以下两种方式之一进行操作:
Register for the event. You can do this one of two ways:
通过设计器完成.选择您的组合框.在属性窗口"中,单击闪电图标以显示其所有事件.然后找到"Enter",然后在框中双击.它将自动为您生成回调函数(事件处理程序"),并将其连接到事件.
Do it through the designer. Select your combo box. In the "Properties window", click the lightning bolt icon to show all of its events. Then find "Enter", and double-click in the box. It will automatically generate the callback function ("event handler") for you, and wire it up to the event.
您可以通过编程方式执行相同的操作.在构造函数中,连接具有正确签名的事件处理程序:
You can programatically do the same thing. In the constructor, hook up an event handler of the correct signature:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox1.Enter += comboBox1_Enter;
}
private void comboBox1_Enter(object sender, EventArgs e)
{
m_cb1PrevVal = comboBox1.SelectedValue;
}
private void RestoreOldValue()
{
comboBox1.SelectedValue = m_cb1PrevVal;
}
}
这篇关于获取ComboBox的先前值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!