2015-01-12 46 views
1

我有一个问题从这个问题导致:How can I handle ComboBox selected index changing?该问题的答案的第一个评论基本上要求与我在这里问的相同。如何返回索引更改事件上的先前选择的索引

它描述了如何捕捉索引更改事件。这工作正常,但我有一个错误提供程序等待这个事件,有效地使某些领域强制性。如果它等于真(或换句话说,强制性字段为空),则它退出子。

这工作正常;数据保持不变并且强调字段突出显示,但是由于所选索引已经改变的事实而出现问题。换句话说,您可以看到原始索引中的数据,但组合框中实际突出显示的索引已经更改。该事件的ChangedIndex,它在索引更改时触发。

无论如何我可以重新选择以前的索引和/或取消转换到新的索引?是否有类似ChangeNode的事件与DeletingRecord对RecordDeleted事件的作用相似?

编辑我正在使用ListBox而不是没有SelectedIndexChanging事件的组合框。 ,

listBox.SelectionChanged += new SelectionChangedEventHandler(listBox_SelectionChanged);

做一次检查,如果你的错误提供商有true值,如果是这样,我:

回答

1

如果您使用的ListBoxSystem.Windows.Controls命名空间,你可以添加一个事件处理程序ListBox.SelectionChanged相信这应该工作(我有使用相同的事件ComboBox控制类似的逻辑):

您可以将其添加到事件处理程序:

//Check if error provider returned true 
if(hasError) 
{ 
    //Cast the sender object as ListBox 
    ListBox listbox = (ListBox)sender; 

    //If there was already something selected before, set it as the SelectedItem 
    if(e.RemovedItems.Count > 0) 
    { 
     listBox.SelectedItem = e.RemovedItems[0]; 
    } 
} 

当然,如果您能够选择多个项目,这可能不起作用。

编辑:因为它似乎就像你在System.Windows.Forms命名空间中使用ListBox(不具备SelectionChanged事件),你可以尝试在你的代码的属性后面,表示当前选定的指数ListBox

在您的SelectedIndexChanged事件中,检查错误提供程序的条件。如果它有错误,则返回保存到您的属性的项目,否则将值更新为新选择的项目。

不是最优雅的解决方案,但它应该工作。

int _CurrentSelectedIndex = -1; //variable to keep track of the SelectedIndex initialized to default value (-1) 

//Add event listener to the ListBox.SelectedIndexChanged event 
ListBox listBox.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged); 


    //Event handler implementation 
    void listbox_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     //Cast the sender object as ListBox 
     ListBox listbox = (ListBox)sender; 

     //validation function detected an error 
     if(!passedValidation) 
     { 
      listBox.SelectedIndex = _CurrentSelectedIndex; //revert to the previously selected item 
     } 

     //Passed validation - update variable to keep track of the SelectedIndex 
     else 
     { 
      _CurrentSelectedIndex = listBox.SelectedIndex 
     } 
    } 
+0

我试过把它放到我的代码中,但似乎sysargs没有'RemovedItems'参数。我正在从SelectedIndexChanged事件和VB.net中工作。不知道这是否与它有关? –

+0

只有几个我可以使用的论据,没有做任何我需要的。 –

+0

@Noodlemanny这听起来像你在'System.Windows.Forms'命名空间中使用'ListBox',而不是'System.Windows.Controls'中的'ListBox',因为你使用'SelectedIndexChanged'事件而不是'SelectionChanged事件。你必须在'System.Windows.Forms'中使用ListBox吗? – Saggio

0

考虑过这个问题后,我想出了一个简单的解决方案。我简单地创建了一个名为'ListBoxIndex'的新变量,并为其提供了默认的开始索引。当所有必填字段都被填充并且验证函数通过true时,我将ListBoxIndex的值设置为listBox.SelectedIndex。当验证返回false时,我简单地将listBox.SelectedIndex重置为ListBoxIndex值,从而将其重置为以前的索引。

+0

很高兴您能够使用它!但是,这不是我上面的同样的解决方案(唯一的变化是我在'ListBox'上的一个事件中完成了,就像你试图在OP中一样)? :p – Saggio

+0

公平起见,我并不真正理解你的解决方案,更不用说让它工作:/无论如何, –

+0

本质上就是你上面描述的 - 我在ListBox的SelectedIndexChanged事件中添加了一个事件处理程序,如果验证函数返回'false'(由'!passedValidation'布尔变量表示),它将'SelectedIndex'设置为'_CurrentSelectedIndex'变量中保存的索引。如果它通过验证,则将_CurrentSelectedIndex中的值更新为其更改的值。什么部分很难遵循?我更新了一下,但让我知道如果我应该添加更多的细节,以帮助将来有类似问题的任何人 – Saggio

相关问题