• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 52
  • Last Modified:

ComboBox text is highlighted until the box is accessed

I have several ComboBoxes in my program, and the ones that have text already in the box end up highlighted.  This is true even looking at the form in Visual Studio (see attached).  Is there a way to keep the text from being highlighted?  The text in these boxes are highlighted when the program is launched.
2 Solutions
Éric MoreauSenior .Net ConsultantCommented:
How do you feed theses controls? What other properties are set?
BrianAuthor Commented:
Here's the property settings I have changed:

Anchor - Top
AutoCompleteCustomSource - Collection
AutoCompleteMode - SuggestAppend
AutoCompleteSource - ListItems
FormattingEnabled - True
Margin - 3, 2, 3, 2
Modifiers - Friend
TabIndex - 2
Text - White

All other settings are the defaults.
NorieVBA ExpertCommented:
How are you populating them?
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Éric MoreauSenior .Net ConsultantCommented:
Is that a plain-old combo box? or one from a third party? or one that you have inherited?

I don't have the same behavior.
BrianAuthor Commented:
It's a plain-old combo box, but I do have it on a panel.  I noticed that when I add text in the Text property under a Combo Box that's not on the panel, it does get highlighted.
Éric MoreauSenior .Net ConsultantCommented:
Really strange. I have added a panel to my dummy project and I still don't have your issue!

Which version of VS do you have? I have VS2017.
BrianAuthor Commented:
I have VS2015, but I have a feeling that it's not the version.  I wonder if it could be a setting on the panel?  I don't see anything unusual with the settings.  The TabIndex on the panel is 5, but the TabStop is set to false, so I don't think that would be an issue.  It's odd that all 4 of those combo boxes on the panel are highlighted.  It must be related to the panel.

Strange... I changed the TabIndex to 100 and it had no affect.  I closed and reopened the project and text in the combo boxes are no longer highlighted in VS, but when I run the program, they are still highlighted.  I have done a lot of moving around on the form...  I think I added the panel later, move the controls onto it.  I also removed some things from the form and moved the panel and controls around.  At some point something bazaar must have happened.
Éric MoreauSenior .Net ConsultantCommented:
I found something. In my demo sample, I was not setting the Anchor property. It appears that this is your issue. I have been able to replicate your behavior in VS2017.

The solution is waiting for you at https://stackoverflow.com/questions/786119/editbox-portion-of-combobox-gets-selected-automatically
it is the edit control part of the combobox which has the EM_NOHIDESEL style bit set, what makes the selection to be hilited even if the control doesn't have the focus.

you can get a handle to the edit box by calling like

Dim hwndCBEdit as IntPtr
hwndCBEdit = GetDlgItem(myComboBox.Handle, 1001) 

Open in new window

and hide or show selection by

Dim fchangeNoHideSel as IntPtr
fchangeNoHideSel  = IntPtr.Zero

' Hide:
SendMessage(hwndCBEdit, EM_HIDESELECTION, New IntPtr(1), fchangeNoHideSel)
' Show:
SendMessage(hwndCBEdit, EM_HIDESELECTION, IntPtr.Zero, fchangeNoHideSel)

Open in new window

note, with the 3rd argument fchangeNoHideSel you could try to change the EM_NOHIDESEL style bit permanently for the edit control.

if set to 1 it should update the EM_NOHIDESEL style permanently to 'hide selection' or 'no hide selection'. if that works you should need to call the SendMessage only once.

fchangeNoHideSel  = New IntPtr(1)
' Hide and set EM_NOHIDESEL style bit
SendMessage(hwndCBEdit, EM_HIDESELECTION, New IntPtr(1), fchangeNoHideSel)

Open in new window

BrianAuthor Commented:
Thank you both for your help!  Sara, your method is probably the more correct way to solve the issue, but a little advanced for me.  I'm just a casual programmer at best! :)  Anyway, I found a solution that works for me.  Here's what I've done...

        cbPaperColor.Focus()            '// Temp focus to remove highlighting
        cbPaperSize.Focus()             '// Temp focus to remove highlighting
        cbColorOrBlacknWhite.Focus()    '// Temp focus to remove highlighting
        cbStapled.Focus()               '// Temp focus to remove highlighting
        txtBuilding.Focus()             '// Give focus to building combo box

Open in new window

I set focus on each one, then take focus away.  Problem solved!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now