取消选择组合框选定值 [英] Deselect combobox selected value

查看:27
本文介绍了取消选择组合框选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我向社区道歉,因为标题可能看起来有点误导了真正的问题".我编写了一个代码,用相同的值填充两个组合框,组合框上发生的每次更改我都会执行检查以查看所选值是否等于另一个组合框;如果相等,则执行特定代码,否则继续.我需要取消对价值的选择,我做到了:

First of all, I apologize to the community because the title may seem a bit 'misleading from what actually is the real problem. I have made a code that populates two combobox with the same values, with each change that occurs on the combobox I perform a check to see if the selected value is equal to another combobox; if it is equal, then execute a particular code otherwise proceed. I need to implement a de-selection of value and I did it with:

ComboBox1.SelectedIndex = -1 
Combobox2.SelectedIndex = -1

这工作正常,但检查值是否相等的代码会干扰此操作.事实上,在每个组合框中我都有这样的东西:

This works fine, but the code that checks if the values are equal interfere with this operation. In fact, within each combobox I have something like this:

Private Sub ComboBox2_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged

If ComboBox3.SelectedIndex = ComboBox2.SelectedIndex Then
MsgBox ("You can not compare two equal teams", "Warning")
off ()
End If
...

其中off ()"是一个函数,它不会继续你正在做的事情.我该如何解决这个问题?

where "off ()" is the function that then doesn't continue what you're doing. How do I solve this problem?

推荐答案

您必须在重置组合框时禁用该事件.这可以通过使用 RemoveHandler/AddHandler 删除事件并再次添加来完成.

You'll have to disable the event when resetting the combobox. This can be done by removing the event with RemoveHandler/AddHandler and adding it again.

另一种选择是使用标志.(这只是一个展示思路的例子,flag变量要正确放置)

An other option is to use a flag. (This is just an example to show an idea, the flag variable should be properly placed).

Private FreezeEventFlag As Boolean = False ' or True, it depends..
' Declare it toplevel, initialize its value depending on
' the way you're going to initialize your Comboboxes selected values.
' Then set it's value to True each time you want to
' disable the event handling in any code block that resets
' SelectedIndexes to -1 like below :

    ' ...
    FreezeEventFlag = True
    ComboBox1.SelectedIndex = -1 
    Combobox2.SelectedIndex = -1
    FreezeEventFlag = False ' Don't forget to set the Flag to false !

Private Sub ComboBox2_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged

    If FreezeEventFlag Then Exit Sub ' Just ignore this event this time.

    If ComboBox3.SelectedIndex = ComboBox2.SelectedIndex Then
        MsgBox ("You can not compare two equal teams", "Warning")
        off ()
    End If
End Sub

这篇关于取消选择组合框选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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