给combox添加一个AddItem事件,当增加item时引发该事件!处理该事件下的程序.
1.先建立一个combox组件,把事件加入进去.代码如下
2.添加一个窗体,加入一个combox继承于上面的ExtendCombox类,另外再加上一个textBox,和一个button,
源代码如下

1.先建立一个combox组件,把事件加入进去.代码如下
1
Public Class ExtendCombox
2
Inherits ComboBox
3
4
组件设计器生成的代码
44
45
Public Event ItemAdded(ByVal strValue As String)
46
Public Function AddDataItem(ByVal strValue As String)
47
' 当用户选择添加项时唤起 ItemAdded
48
RaiseEvent ItemAdded(strValue)
49
End Function
50
51
52
53
54
End Class
55
Public Class ExtendCombox2
Inherits ComboBox3

4
组件设计器生成的代码44

45
Public Event ItemAdded(ByVal strValue As String)46
Public Function AddDataItem(ByVal strValue As String)47
' 当用户选择添加项时唤起 ItemAdded48
RaiseEvent ItemAdded(strValue)49
End Function50

51

52

53

54
End Class55

2.添加一个窗体,加入一个combox继承于上面的ExtendCombox类,另外再加上一个textBox,和一个button,
源代码如下
1
Public Class Form1
2
Inherits System.Windows.Forms.Form
3
4
Windows 窗体设计器生成的代码
78
79
80
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
81
Dim str As String = Me.txtValue.Text.Trim()
82
ComboBox1.AddDataItem(str)
83
84
End Sub
85
86
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
87
88
End Sub
89
90
Private Sub ComboBox1_ItemAdded(ByVal strValue As String) Handles ComboBox1.ItemAdded
91
92
Dim str As String = Me.txtValue.Text.Trim()
93
If (Me.ComboBox1.Items.IndexOf(str) = -1) Then
94
Me.ComboBox1.Items.Add(str)
95
Me.ComboBox1.SelectedIndex = Me.ComboBox1.Items.IndexOf(str)
96
End If
97
End Sub
98
End Class
99
最后运行显示
Public Class Form12
Inherits System.Windows.Forms.Form3

4
Windows 窗体设计器生成的代码78

79

80
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click81
Dim str As String = Me.txtValue.Text.Trim()82
ComboBox1.AddDataItem(str)83

84
End Sub85

86
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load87

88
End Sub89

90
Private Sub ComboBox1_ItemAdded(ByVal strValue As String) Handles ComboBox1.ItemAdded91

92
Dim str As String = Me.txtValue.Text.Trim()93
If (Me.ComboBox1.Items.IndexOf(str) = -1) Then94
Me.ComboBox1.Items.Add(str)95
Me.ComboBox1.SelectedIndex = Me.ComboBox1.Items.IndexOf(str)96
End If97
End Sub98
End Class99



浙公网安备 33010602011771号