Is there a preview version for C # ComboBox on Winform

I came from the VBA world and remember that there was a challenge BeforeUpdatethat I could make in the drop-down list. Now I am in C # (and love it), and I was wondering if there is a BeforeUpdatecall ComboBoxin Winform?

I can make an invisible text field and store the information that I need, and after updating, look at this field for what I need, but I was hoping there was a simpler solution.

+2
source share
5 answers

You can consider SelectionChangeCommited.

From MSDN:

SelectionChangeCommitted . SelectedIndexChanged SelectedValueChanged , .

, combobox, . , , "" . . . .

+4

WF , . . . . BeforeUpdate.

using System;
using System.ComponentModel;
using System.Windows.Forms;

public class MyComboBox : ComboBox {
  public event CancelEventHandler BeforeUpdate;

  public MyComboBox() {
    this.DropDownStyle = ComboBoxStyle.DropDownList;
  }

  private bool mBusy;
  private int mPrevIndex = -1;

  protected virtual void OnBeforeUpdate(CancelEventArgs cea) {
    if (BeforeUpdate != null) BeforeUpdate(this, cea);
  }

  protected override void OnSelectedIndexChanged(EventArgs e) {
    if (mBusy) return;
    mBusy = true;
    try {
      CancelEventArgs cea = new CancelEventArgs();
      OnBeforeUpdate(cea);
      if (cea.Cancel) {
        // Restore previous index
        this.SelectedIndex = mPrevIndex;
        return;
      }
      mPrevIndex = this.SelectedIndex;
      base.OnSelectedIndexChanged(e);
    }
    finally {
      mBusy = false;
    }
  }
}
+10

ValueMemberChanged, Validating, SelectedIndexChanged TextChanged. , PreUpdate, , , .

+1

. , , , . , . , . ComboBox , SelectedItem . SelectedValueChanged. , SelectedItem .

private object oldItem = new object();

        private void button3_Click(object sender, EventArgs e)
        {
            DateTime date = DateTime.Now;
            for (int i = 1; i <= 10; i++)
            {
                this.comboBox1.Items.Add(date.AddDays(i));
            }

            oldItem = this.comboBox1.SelectedItem;
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            //do what you need with the oldItem variable
            if (oldItem != null)
            {
                MessageBox.Show(oldItem.ToString());
            }

            this.oldItem = this.comboBox1.SelectedItem;
        }
+1

, , DropDown. , , . , , BeforeUpdate.

0

All Articles