You want to handle the ComboBox.Enter
event. Then save off the SelectedItem
or SelectedValue
to a member variable. Whenever you want then, you can re-assign that value to the ComboBox.
Register for the event. You can do this one of two ways:
Do it through the designer. Select your combo box. In the "Properties window", click the lightning bolt icon to show all of its events. Then find "Enter", and double-click in the box. It will automatically generate the callback function ("event handler") for you, and wire it up to the event.
![enter image description here](https://cdn.statically.io/img/i.sstatic.net/Eh6Z7.png)
![enter image description here](https://cdn.statically.io/img/i.sstatic.net/69H5b.png)
You can programatically do the same thing. In the constructor, hook up an event handler of 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;
}
}