Selected text in a ComboBox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following code:
.cboBox1.Items.AddRange(New Object() {"A", "B", "C"})
.cboBox2.Items.AddRange(New Object() {"A", "B", "C"})
.cboBox6.Items.AddRange(New Object() {"A", "B", "C"})
.cboBox1.SelectedIndex = 0
.cboBox2.SelectedIndex = 1
.cboBox6.SelectedIndex = 2
When this form opens, A, B, and C are all highlighted in their respective
controls. I have tried the following ways to unhighlight the text but
without success.

.cboBox6.SelectionLength = 0
.cboBox6.Select(0, 0)
.cboBox1.Focus()

Is there a way to unhighlight the text in the comboboxes that do not have
focus?
 
Set them to -1
.cboBox1.SelectedIndex = -1
.cboBox2.SelectedIndex = -1
.cboBox6.SelectedIndex = -1
 
This solution does not work because I want to actually display values in each
comboBox. I tried the following code and each item still remained
highlighted.

.cboBox1.Items.AddRange(New Object() {"A", "B", "C"})
.cboBox2.Items.AddRange(New Object() {"A", "B", "C"})
.cboBox6.Items.AddRange(New Object() {"A", "B", "C"})
.cboBox1.SelectedIndex = -1
.cboBox2.SelectedIndex = -1
.cboBox6.SelectedIndex = -1
.cboBox1.Text = "A"
.cboBox2.Text = "B"
.cboBox6.Text = "C"
 
It works

ComboBox1.SelectedText = "A"
ComboBox2.SelectedText = "B"
ComboBox3.SelectedText = "C"
ComboBox1.SelectedIndex = -1
ComboBox2.SelectedIndex = -1
ComboBox3.SelectedIndex = -1
 
You are correct. I finally went to a new project and could duplicate your
result. In my actual application, however, I still ended up with all
combobox text highlighted. Below is my actual abridged code that, frankly,
does not make sense and seems to indicate some kind of bug in .NET

FYI: In the actual application, I had the following line of code associated
with each combobox in the parent form:

Me.cboBox2.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)

I then opened the form with:
Dim mfrmCommon As frmCommon = New frmCommon
With mfrmCommon
.......................Set text, etc. for various controls on form
.ShowDialog()
End With

When opening frmCommon this way, every combobox was highlighted. I added
the following line of code in the With statement for each combobox and the
problem went away.

.cboBox1.Anchor = CType((System.Windows.Forms.AnchorStyles.Top
Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)

I short, I changed the combobox anchor from top, left, right to top, left to
solve the problem. Go figure.


:
 
Back
Top