1
我正在寻找一个优雅的方式来跟踪值组合框之间的变化。我期望做的是在SelectionChanged事件发生时触发自定义事件,但仅限于特定的值更改。这意味着要知道初始值是什么。只有当初始值从z改变时,该事件才会被触发。如果初始值为a,b或c,则该事件不会被触发。但如果初始值是z,它将被触发。最简单的方法来监视组合框值之间的变化
有没有人有一个优雅的方式来解决这个问题?
我正在寻找一个优雅的方式来跟踪值组合框之间的变化。我期望做的是在SelectionChanged事件发生时触发自定义事件,但仅限于特定的值更改。这意味着要知道初始值是什么。只有当初始值从z改变时,该事件才会被触发。如果初始值为a,b或c,则该事件不会被触发。但如果初始值是z,它将被触发。最简单的方法来监视组合框值之间的变化
有没有人有一个优雅的方式来解决这个问题?
为此,您需要创建一个自定义事件处理程序,并且可以自定义事件参数,
//Event Handler Class
public class SpecialIndexMonitor
{
public event EventHandler<SpecialIndexEventArgs> SpecialIndexSelected;
//Custom Function to handle Special Index
public void ProcessRequest(object sender, System.EventArgs e)
{
//Your custom logic
//Your code goes here
//Raise event
if(SpecialIndexSelected != null)
{
SpecialIndexEventArgs args = new SpecialIndexEventArgs{
SelectedIndex = ((ComboBox) sender).SelectedIndex;
};
SpecialIndexSelected(this, args);
}
}
}
//Custom Event Args
public class SpecialIndexEventArgs : EventArgs
{
//Custom Properties
public int SelectedIndex { get; set; } //For Example
//Default Constructor
public SpecialIndexEventArgs()
{
}
}
内,您的形式
//Hold previous value
private string _previousItem;
//IMPORTANT:
//After binding items to combo box you will need to assign,
//default selected item to '_previousItem',
//which will make sure SelectedIndexChanged works all the time
// Usage of Custom Event
private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
string selectedItem = (string)comboBox1.SelectedItem;
if(string.Equals(_previousItem,)
switch(_previousItem)
{
case "z":
{
SpecialIndexMonitor spIndMonitor = new SpecialIndexMonitor();
spIndMonitor.SpecialIndexSelected +=
new EventHandler<SpecialIndexEventArgs>(SpecialIndexSelected);
break;
}
case "a":
case "b":
break;
}
_previousItem = selectedItem; //Re-Assign the current item
}
void SpecialIndexSelected(object sender, SpecialIndexEventArgs args)
{
// Your code goes here to handle the special index
}
有没有编译的代码,但在逻辑上它应该为你工作。
哪种编程语言? – emaillenin 2012-01-29 04:59:50
没错。对此感到悲伤。我正在使用.NET环境。 C#和VS2010更具体。 – user953710 2012-01-29 23:17:36