@hansPassant 答案的 VB 版本,我今天需要它。对于接手 VB 项目但没有出路的人来说,这可能会有所帮助:
修改特定控件的 Resize 事件(此处为ComboBox1):
Private Sub ComboBox1_Resize(sender As Object, e As EventArgs) Handles ComboBox1.Resize
Dim curr_combo = TryCast(sender, System.Windows.Forms.ComboBox)
If ((Not curr_combo.Focused) And curr_combo.DropDownStyle = ComboBoxStyle.DropDown) Then
curr_combo.SelectionLength = 0
End If
End Sub
要创建通用事件,您可以附加到多个ComboBoxs:
Private Sub ComboBox_Resize(sender As Object, e As EventArgs)
Dim curr_combo = TryCast(sender, System.Windows.Forms.ComboBox)
If ((Not curr_combo.Focused) And curr_combo.DropDownStyle = ComboBoxStyle.DropDown) Then
curr_combo.SelectionLength = 0
End If
End Sub
然后将其添加到您的 ComboBox 控件之一,如下所示:
AddHandler my_combo.Resize, AddressOf ComboBox_Resize
或者像这样对他们所有人:
For Each obj As Object In Me.Controls()
Dim combo = TryCast(obj, ComboBox)
If TypeOf combo Is ComboBox Then
AddHandler combo.Resize, AddressOf ComboBox_Resize
End If
Next