C# ComboBox SelectedIndexChanged事件:如何获取以前选择的索引?
我有一个用户控件,它有一个组合框和一个SelectedIndexChanged事件处理程序。在事件处理程序中,我需要能够告诉以前选择的索引是什么。。。谁能给我指出正确的方向吗C# ComboBox SelectedIndexChanged事件:如何获取以前选择的索引?,c#,winforms,.net-3.5,C#,Winforms,.net 3.5,我有一个用户控件,它有一个组合框和一个SelectedIndexChanged事件处理程序。在事件处理程序中,我需要能够告诉以前选择的索引是什么。。。谁能给我指出正确的方向吗 private void cboTargetMode_SelectedIndexChanged(object sender, EventArgs e) { // need to get the previously selected index and do some handling here... //
private void cboTargetMode_SelectedIndexChanged(object sender, EventArgs e)
{
// need to get the previously selected index and do some handling here...
// ... some handler code here ...
switch (cboTargetMode.SelectedIndex)
{
case 1: // ..... some code here...
break;
case 2: // ..... some code here...
break;
case 3: // ..... some code here...
break;
default: // ..... some code here...
break;
}
}
没有内置内容,您需要侦听此事件并跟踪实例变量 使用-1作为未初始化的“最后一个索引”,所以在第一次传递时设置它,但不使用它。在后续过程中,您可以使用它并设置它
您可以始终使用自己的派生ComboBox类来执行此操作,并覆盖SelectedIndexChanged上的
并公开一个以前选择的Index
属性。这样,它就不会与表单紧密耦合。或者,由于您可以使用事件执行此操作,因此它也可以作为一个变量来实现。我想您必须将当前(稍后将成为前一个)存储到一个变量中,以便将其用作缓存或类似的东西
private void cboTargetMode_SelectedIndexChanged(object sender, EventArgs e) {
// need to get the previously selected index and do some handling here...
// ... some handler code here ...
// Assuming that the variable PreviousSelectedIndex is declared in the class with value -1.
if (PreviousSelectedIndex < 0)
PreviousSelectedIndex = cbo.TargetMode.SelectedIndex;
else
// Do some handling here...
switch (cboTargetMode.SelectedIndex) {
case 1: // ..... some code here...
break;
case 2: // ..... some code here...
break;
case 3: // ..... some code here...
break;
default: // ..... some code here...
break;
}
}
private void cboTargetMode\u SelectedIndexChanged(对象发送方,事件参数e){
//需要获取以前选择的索引并在此处进行一些处理。。。
//…这里有一些处理程序代码。。。
//假设在值为-1的类中声明了变量PreviousSelectedIndex。
如果(以前选择的索引<0)
上一个SelectedIndex=cbo.TargetMode.SelectedIndex;
其他的
//在这里做一些处理。。。
开关(cboTargetMode.SelectedIndex){
案例1://…这里有一些代码。。。
打破
案例2://…这里有一些代码。。。
打破
案例3://…这里有一些代码。。。
打破
默认值://…此处有一些代码。。。
打破
}
}
这是你已经想到的吗
否则,可能会与事件一起工作?我只是说不出这个事件是发生在SelectedIndexChanged
事件之前还是之后=(comboBox\u SelectionChangeCommitted事件如何?这是在进行选择后调用的,菜单已折叠,但项目未完全更改。因此只需阅读comboBox.SelectedText或comboBox.SelectedValue,甚至comboBox.SelectedIndex
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
int prevIndex = comboBox1.SelectedIndex;
}
这是一个获得当前选定索引,我只是需要它,我找不到任何地方,所以我希望它有帮助
private void lsbx_layers_SelectedIndexChanged(object sender, EventArgs e)
{
int i = lsbx_layers.SelectedIndices[0];//selected index
MessageBox.Show("Selected item at index : " + i);
}
我有一个与此类似的问题,我使用
我循环并设置了他们所有的lostFocus事件:
foreach(Control control in this.Controls)
{
if(control is ComboBox)
{
((ComboBox)control).LostFocus += ComboBox_LostFocus;
}
}
并且有一个dictionary对象来保存旧值
public Dictionary<string, int> comboBoxOldValues = new Dictionary<string, int>();
公共字典comboBoxOldValues=新字典();
最后确保该值存在或设置为旧索引,然后保存到字典
private void ComboBox_LostFocus(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
if (comboBox.DataSource is List<YourDataType>)
{
if (((List<YourDataType>)comboBox.DataSource).Count(x => x.YourValueMember == (YourValueMemberType)comboBox.SelectedValue) == 0)
{
if (comboBoxOldValues.Keys.Count(x => x == comboBox.Name) > 0)
{
comboBox.SelectedIndex = comboBoxOldValues[comboBox.Name];
}
else
comboBox.SelectedIndex = -1;
}
}
if (comboBoxOldValues.Keys.Count(x => x == comboBox.Name) > 0)
{
comboBoxOldValues[comboBox.Name] = comboBox.SelectedIndex;
}
else
comboBoxOldValues.Add(comboBox.Name, comboBox.SelectedIndex);
if (comboBox.SelectedIndex == -1)
comboBox.Text = string.Empty;
}
private void组合框\u LostFocus(对象发送方,事件参数e)
{
ComboBox ComboBox=(ComboBox)发送方;
如果(comboBox.DataSource是列表)
{
if(((列表)comboBox.DataSource).Count(x=>x.YourValueMember==(YourValueMemberType)comboBox.SelectedValue==0)
{
if(comboBoxOldValues.Keys.Count(x=>x==comboBox.Name)>0)
{
comboBox.SelectedIndex=comboBoxOldValues[comboBox.Name];
}
其他的
comboBox.SelectedIndex=-1;
}
}
if(comboBoxOldValues.Keys.Count(x=>x==comboBox.Name)>0)
{
comboBoxOldValues[comboBox.Name]=comboBox.SelectedIndex;
}
其他的
添加(comboBox.Name,comboBox.SelectedIndex);
如果(comboBox.SelectedIndex==-1)
comboBox.Text=string.Empty;
}
您可以在下拉列表事件中获取并存储旧值。然后在选择更改提交事件中恢复到该旧值(如果有保证).@Zekeriya,这是6年前的事了,但我通过双向绑定SelectedItem
并管理视图模型上绑定属性的setter
中以前的选择来解决了这个问题。说实话,这是一种更好的方法,因为它更容易实现,而且控制不可知——这意味着我不需要为我将列表绑定到的每个不同控件编写一些处理程序代码。好的,我上个月需要解决此问题,但接受的awnser对我不起作用。在某种程度上,您当前的响应也不起作用,但我稍后会检查它……无论如何,感谢您的响应:)当前我们使用@my shop的MVVM理念是避免转换器,避免行为(除非绝对必要),并尝试让viewmodel做更多的工作-即让它提供SelectedItem等。它不应该是“实例变量”而不是“类变量”吗?@FrankSchmitt追逐一枚徽章,挖掘一个近8年的帖子?;-)但你是对的,我已经修改了我的答案。@Adamhuldsworth谢谢。不,我(目前)不是在寻找徽章:-)
private void ComboBox_LostFocus(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
if (comboBox.DataSource is List<YourDataType>)
{
if (((List<YourDataType>)comboBox.DataSource).Count(x => x.YourValueMember == (YourValueMemberType)comboBox.SelectedValue) == 0)
{
if (comboBoxOldValues.Keys.Count(x => x == comboBox.Name) > 0)
{
comboBox.SelectedIndex = comboBoxOldValues[comboBox.Name];
}
else
comboBox.SelectedIndex = -1;
}
}
if (comboBoxOldValues.Keys.Count(x => x == comboBox.Name) > 0)
{
comboBoxOldValues[comboBox.Name] = comboBox.SelectedIndex;
}
else
comboBoxOldValues.Add(comboBox.Name, comboBox.SelectedIndex);
if (comboBox.SelectedIndex == -1)
comboBox.Text = string.Empty;
}