监视组合框值之间变化的最简单方法

发布于 2024-12-29 13:56:10 字数 178 浏览 6 评论 0原文

我正在寻找一种优雅的方式来跟踪组合框值之间的变化。我想要做的是在 SelectionChanged 事件发生时触发自定义事件,但仅限于特定值更改。这意味着知道初始值是多少。仅当初始值从 z 更改时才会触发该事件。如果初始值为 a、b 或 c,则不会触发该事件。但如果初始值为 z,它将被解雇。

有人有解决这个问题的优雅方法吗?

I'm looking for an elegant way to track changes between values for a combo box. What I'm looking to do is fire a custom event when the SelectionChanged event happens, but only for a specific value changes. This implies knowing what the initial value was. The event will only be fired when the initial value is changed from z. If the initial value is a, b, or c, the event will not be fired. But if the initial value was z, it will be fired.

Does anyone have an elegant way to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他是夢罘是命 2025-01-05 13:56:10

为此,您必须创建一个自定义事件处理程序,并且可能是自定义事件参数,

 //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
 }

尚未编译代码,但从逻辑上讲它应该适合您。

For this you will have to create a custom event handler and may be custom event args,

 //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 ()
     {
     }
 }

Inside your form

 //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
 }

Haven't compiled the code, but logically it should work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文