Getting Previous ComboBox Value

I want my application to capture a ComboBox value and then set the value selected by the user, or somehow get the previously selected value.

The fact is that in my form there are four lists and a ComboBox (which contains all the values ​​from the lists), and I want to return the ComboBox value back to the list from which it was taken, and then remove the newly selected item from another / the same list .

+8
c # winforms
source share
1 answer

You want to handle the ComboBox.Enter event. Then save the SelectedItem or SelectedValue in a member variable. Whenever you want, you can reassign this value to the ComboBox.

Register an event. You can do this in one of two ways:

Do it through the designer. Select your combo box. In the Properties window, click the Lightning icon to display all of its events. Then find "Enter" and double-click in the box. It will automatically generate a callback function ("event handler") for you and hook it to the event.

enter image description here

enter image description here

You can do the same programmatically. In the constructor, attach the event handler to the correct signature:

 public partial class Form1 : Form { public Form1() { InitializeComponent(); comboBox1.Enter += comboBox1_Enter; } private void comboBox1_Enter(object sender, EventArgs e) { m_cb1PrevVal = comboBox1.SelectedValue; } private void RestoreOldValue() { comboBox1.SelectedValue = m_cb1PrevVal; } } 
+16
source share

All Articles