如何在c#中取消组合框的值变化? [英] How to cancel a combobox's value change in c#?

查看:75
本文介绍了如何在c#中取消组合框的值变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框。



如果用户进行了更改但未保存,并尝试从组合框中选择其他选项,则messageBox会警告他们给他们一个机会



1取消(保持更改)



2否(丢弃更改)



3是(保存chages)



例如:



组合框中包含的值



电脑



笔记本电脑



手机



电视



相机



用户选择相机然后将其更改为Camera78778

然后用户从中选择另一个值(比如计算机)组合框



messageBox警告他们并给他们机会



1取消(保持更改) :combox是Camera78778



2否(丢弃更改):combox是电脑



3是(sa ve chages):combox是Computer但是这里的更改已经保存。



我需要取消的代码。



我试过comboBox1.SelectedIndexChanged - = comboBox1._SelectedIndexChanged;

但没有解决方案。

我尝试了comboBox1_SelectionChangeCommitted但没有解决方案。



感谢高级。

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.

推荐答案

此控件中没有可以取消的事件,尽我所能看到。实际上,对于选择列表元素的过程实现的这种事件对用户来说太混乱了。我只想创建一个非工作控件的印象。想象一下,您尝试单击某个列表元素,并看到旧元素持续进入选择状态。对于键盘,它甚至可能更糟,因此根本无法选择。这不是很明显吗?



所以,没有这样的事件,因为整个想法都不好。最好不要修复这种非常自然的行为。修复UI设计本身会好得多。假设组合框的使用的要求包括一个简单的规则:列表中存在的所有列表元素始终对选择有效。如果您设计假定某些元素在某些条件下无效,请将其删除并仅动态保留有效元素。您可以动态执行此操作,例如,在 GotFocus 事件的处理程序中。



或者,您可以使用一组单选按钮。与列表元素不同,单选按钮可以显示但禁用,因此在某些情况下无法选择。或者创建一些根本不使用这种控件的UI设计。这一切都取决于你的最终目标。



-SA
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


这是解决方案/>


string TxtCombo =;

int lastIndexcomboBox1 = -1;



private void comboBox1_SelectionChangeCommitted(object sender,EventArgs e)

{



if(lastIndexcomboBox1 == -1 && comboBox1.Text == )

{

comboBox1.Tag =干净;

}

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(你想要保存,MessageBoxButtons.YesNoCancel );



if(dr == DialogResult.Cancel)

{

this.BeginInvoke((MethodInvoker) )委托{this.comboBox1.Text = TxtCombo; });

}



}



if(comboBox1.Tag .ToString()==Clean&& cmbType.Tag.ToString()==Clean)

{

lastIndexcomboBox1 = comboBox1.SelectedIndex;

}



}
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;
}

}


这篇关于如何在c#中取消组合框的值变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆