Reputation: 4655
I have never noted strange behaviour on Comboboxes until now. For see what is happening you may create a minimal example. Open a new project, put textbox, two comboboxes with DropDownStyle=DropDown
(what is initial) and button on it and paste following code to Form.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
TextBox1.Text = "Mess"
Button1.Text = "Increase Font"
ComboBox1.Items.AddRange({"One", "Two", "Three"})
ComboBox1.Text = "Two"
ComboBox2.Items.AddRange({"Left", "Right", "Up", "Down"})
ComboBox2.Text = "Up"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Font = New Font(Me.Font.Name, Me.Font.Size + 1)
TextBox1.Focus()
End Sub
End Class
What is happenning is that in case of pressing a Button1 Form's fontsize increases and Comboboxes selects his text! That way my GUI becames messed.
Did anybody know how to get rid of that and get Comboboxes with expected functionality?
Upvotes: 2
Views: 76
Reputation: 19340
It seem to be some problem in framework. Consider it "funny behavior". But you can do this
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Font = New Font(Me.Font.Name, Me.Font.Size + 1)
TextBox1.Focus()
ComboBox1.SelectionLength = 0
ComboBox2.SelectionLength = 0
End Sub
If your cbo is sub-classed as you pointed in the comment than in your super class add
Private Sub ComboBox2_Layout(sender As Object, e As LayoutEventArgs) Handles ComboBox2.Layout
DirectCast(sender, ComboBox).SelectionLength = 0
End Sub
this works for all cbo's on the form
Private Sub Form1_Layout(sender As Object, e As LayoutEventArgs) Handles MyBase.Layout
For Each c As Control In Me.Controls
Dim combo As ComboBox = TryCast(c, ComboBox)
If combo IsNot Nothing AndAlso combo.DropDownStyle = ComboBoxStyle.DropDown Then
combo.SelectionLength = 0
End If
Next
End Sub
Upvotes: 2