Selected text in a ComboBox

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?
 
S

Some Guy

Set them to -1
.cboBox1.SelectedIndex = -1
.cboBox2.SelectedIndex = -1
.cboBox6.SelectedIndex = -1
 
G

Guest

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"
 
S

Some Guy

It works

ComboBox1.SelectedText = "A"
ComboBox2.SelectedText = "B"
ComboBox3.SelectedText = "C"
ComboBox1.SelectedIndex = -1
ComboBox2.SelectedIndex = -1
ComboBox3.SelectedIndex = -1
 
G

Guest

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.


:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top