title: How to cancel a combobox's value change in c#?
body: <p>I have a combobox.</p>
  
  <p>If the user has made changes, but not saved, and tries to select a different option from the combobox, a messageBox warn them and give them a chance to</p>
  
  <p>1 cancel (keep changes)</p>
  
  <p>2 No  (discard changes)</p>
  
  <p>3 Yes (save the chages)</p>
  
  <p>for example:</p>
  
  <p>the combobox contains on the values</p>
  
  <p>Computer</p>
  
  <p>Laptop</p>
  
  <p>Phone</p>
  
  <p>TV</p>
  
  <p>Camera</p>
  
  <p>The user chose the "Camera" then changed it to "Camera78778"
  then the user chose another value (say "Computer" ) from the combobox</p>
  
  <p>the messageBox warn them and give them a chance to</p>
  
  <p>1 cancel (keep changes): the combox is "Camera78778"</p>
  
  <p>2 No  (discard changes): the combox is "Computer"</p>
  
  <p>3 Yes (save the chages): the combox is "Computer" but here the changes has saved.</p>
  
  <p>I need the code of the cancel.</p>
  
  <p>I tried comboBox1.SelectedIndexChanged -= comboBox1._SelectedIndexChanged;
  but no solution.
   and I tried comboBox1_SelectionChangeCommitted but no solution.</p>
  
  <p>thanks advanced.</p>
  
  <p>UPDATE</p>
  
  <pre><code>    int lastIndexcomboBox1 = -1;
  
      private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
      {
      Myfunction(comboBox1.SelectedIndex);
      }
  
      private void Myfunction(int comboBox1SelectedIndex)
      {
  
          if(comboBox1.Tag.ToString() == "not changed")
          {
  
          }
          else
          {
              DialogResult dr = MessageBox.Show("Do you want to save", "", MessageBoxButtons.YesNoCancel);
              if (dr == DialogResult.Yes)
              {
  
              }
              else if (dr == DialogResult.No)
              {
  
              }
              else if (dr == DialogResult.Cancel)
              {
                  comboBox1.SelectedIndexChanged -= comboBox1_SelectedIndexChanged;
                  comboBox1.SelectedIndex = lastIndexcomboBox1;
                  comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
              }
          }
  
      }
  </code></pre>