首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

winform 中在给 ComboBox 控件的 SelectedIndex 属性赋值时怎样避免 SelectedIndexChanged 事件的发生解决方法

2012-02-02 
winform 中在给 ComboBox 控件的 SelectedIndex 属性赋值时怎样避免 SelectedIndexChanged 事件的发生如题

winform 中在给 ComboBox 控件的 SelectedIndex 属性赋值时怎样避免 SelectedIndexChanged 事件的发生
如题

[解决办法]
你可以通过选删除事件操作完后再注册事件来达到目白,比如:

RemoveHandler Me.comboBox1.SelectedIndexChanged, New EventHandler(AddressOf Me.comboBox1_SelectedIndexChanged)

Me.comboBox1.SelectedIndex = (Me.comboBox1.Items.Count - 1)

AddHandler Me.comboBox1.SelectedIndexChanged, New EventHandler(AddressOf Me.comboBox1_SelectedIndexChanged)

[解决办法]
Imports System.Data.OleDb

Public Class Form1

' 用一个布尔型变量来标记下拉菜单是否打开过,如果打开过则执行ComboBox1_SelectedIndexChanged
' 如果没有打开过则不执行
Dim flag As Boolean = False
Dim c As Int32 = 0

Private Sub ComboBox1_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDown
flag = True
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If flag Then
MsgBox( "SelectedIndexChanged! ")
End If
flag = False
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
' 这段代码用于演示下拉菜单没有打开时,不跳出MsgBox
Me.ComboBox1.SelectedIndex = c
c += 1
If c > 9 Then
c = 0
End If
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For i As Int32 = 0 To 9
Me.ComboBox1.Items.Add( "Item " & i)
Next
End Sub

End Class
[解决办法]
ls,我的方法就是这样。ls的ls,这是winform,lz说得很清楚。

热点排行