Click here to Skip to main content
15,868,292 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a combobox.

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

1 cancel (keep changes)

2 No (discard changes)

3 Yes (save the chages)

for example:

the combobox contains on the values

Computer

Laptop

Phone

TV

Camera

The user chose the "Camera" then changed it to "Camera78778"
then the user chose another value (say "Computer" ) from the combobox

the messageBox warn them and give them a chance to

1 cancel (keep changes): the combox is "Camera78778"

2 No (discard changes): the combox is "Computer"

3 Yes (save the chages): the combox is "Computer" but here the changes has saved.

I need the code of the cancel.

I tried comboBox1.SelectedIndexChanged -= comboBox1._SelectedIndexChanged;
but no solution.
and I tried comboBox1_SelectionChangeCommitted but no solution.

thanks advanced.
Posted

There are no events in this control which could be cancelled, as far as I can see. Indeed, such event implemented for the process of selection of the list element would be too confusing to the user. I would simply create an impression of the non-working control. Imagine that you try to click on some list element, and see the old element persistently getting in selection. For a keyboard, it could be even worse, so nothing at all could be selectable. Isn't that obvious?

So, there are no such events, because the whole idea is bad. And it's better not to "fix" this quite natural behavior. It will be much better to fix your UI design itself. Assume that the requirements for the use of a combo box include one simple rule: all list elements present in the list are always valid for the selection. If you design assumes that some elements are invalid under some condition, remove them and leave only valid ones, dynamically. You can do it dynamically, for example, in your handler of GotFocus event.

Alternatively, you can use a set of radio buttons. A radio button, in contrast to the list element, can be shown but disabled, so it could not be selected on some condition. Or create some UI design not using such controls at all. It all depends on your ultimate goals.

—SA
 
Share this answer
 
Comments
Mukhtar Ghaleb 4-Jul-15 15:21pm    
thank you for your reply.but I saw a Program and i want to make a Program like it.
Sergey Alexandrovich Kryukov 5-Jul-15 0:20am    
No buts. If you saw something good, which I doubt, you have to describe the behavior in all the detail.
—SA
Mukhtar Ghaleb 4-Jul-15 18:50pm    
No solution yet
Sergey Alexandrovich Kryukov 5-Jul-15 0:19am    
What do you want for a solution? Only don't tell me that you want to implement what you want. The idea is wrong.
—SA
Mukhtar Ghaleb 5-Jul-15 16:56pm    
No. the idea is not wrong, a Good programmer can do what he wants but maybe the good programmers don't have the time to answer my question. the program I want to make like it is PEACHTREE accounting...
this is the solution

string TxtCombo = "";
int lastIndexcomboBox1 = -1;

private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{

if (lastIndexcomboBox1 == -1 && comboBox1.Text == "")
{
comboBox1.Tag = "Clean";
}
else if (lastIndexcomboBox1 == -1 && comboBox1.Text != "")
{
comboBox1.Tag = "notClean";
}
else if (((classDetails)comboBox1.Items[lastIndexcomboBox1]).AccountCodeAlternateKey.ToString() == comboBox1.Text)
comboBox1.Tag = "Clean";
else if (((classDetails)comboBox1.Items[lastIndexcomboBox1]).AccountCodeAlternateKey.ToString() != comboBox1.Text)
comboBox1.Tag = "notClean";

if(comboBox1.Tag.ToString() == "notClean")
{
TxtCombo = comboBox1.Text;

DialogResult dr = MessageBox.Show("Do you want to save", MessageBoxButtons.YesNoCancel);

if (dr == DialogResult.Cancel)
{
this.BeginInvoke((MethodInvoker)delegate { this.comboBox1.Text = TxtCombo; });
}

}

if (comboBox1.Tag.ToString() == "Clean" && cmbType.Tag.ToString() == "Clean")
{
lastIndexcomboBox1 = comboBox1.SelectedIndex;
}

}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Jul-15 21:20pm    
I rarely saw so bad code, sorry. It's pain to see.
—SA
Mukhtar Ghaleb 6-Jul-15 21:40pm    
you didn't understand my question, then,it's pain to see.
Sergey Alexandrovich Kryukov 6-Jul-15 21:46pm    
There, there. Remember that everyone can read what we write here.
Good luck.
—SA
Mukhtar Ghaleb 6-Jul-15 22:01pm    
Then, why you said it's pain,
give a solution.
Good luck.
Mukhtar Ghaleb

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900