Remove the highlighting from combobox when selected

pilsdumps

Member
Joined
Jul 27, 2009
Messages
18
Programming Experience
3-5
Hi,

There's probably some easy way to do this but it's driving me mad...

I have a form with a combo box that is populated from a database. When I set the text of the combo box to the value in the db
VB.NET:
.Uc_address1.cmb_country.SelectedText = C_User.country

the combobox remains highlighted, and if the text of the selected item is longer than the width of the combo box, it only shows the last characters. I want to remove this highlighting when the form is loaded (similar to what happens when you tab away from the combo, but still allow the highlighting when a user enters the combobox for entering data.

How can I achieve this?

I've seen someone suggest overriding the paint event, but I'm a bit green here and my attempts at this have failed.

Thanks
 
Hi,

Thanks for the response but I've tried that without any luck. I've had a look into this further, and there appears to be others noticing strange combobox behaviour....

To explain further...

The combobox inherits from a base one that is set with the appropriate formatting. Its constructor populates the item list after first clearing the existing items.
VB.NET:
Inherits uc_combo_base

public sub New()

fill_combo

End sub

    Private Sub fill_combo()

        Me.Items.Clear()

        'fill with specialism table
        Dim sqlrunner As New C_SQLRunner(My.Settings.trials_server, My.Settings.trials_db)
        Dim r As SqlDataReader

        r = sqlrunner.RunSPReader("mp_get_specialisms")

        While r.Read
            Me.Items.Add(r.Item("specialism"))
            asc_specialism.Add(r.Item("specialism"))
        End While

        With Me
            .AutoCompleteSource = AutoCompleteSource.CustomSource
            .AutoCompleteCustomSource = asc_specialism
            .AutoCompleteMode = AutoCompleteMode.Suggest
        End With

        'clean up
        sqlrunner.Dispose()
        r.Close()


    End Sub

There are two odd things happening;

1. The items do not clear but seem to keep growing
2. The item remains highlighted as stated before when doing
VB.NET:
.cmb_specialism.SelectedText = C_User.speciality

I've tried setting the selectedindex to -1 and it makes no difference.

I can't believe I'm the only one to have found this, but a search of the Internet has not provided a definitive solution.

Any ideas?
 
Remove combo box highlighting

Hi,

I have a user control that contains a combo box and a button. The combobox contains a list of user professions (nurse, doctor etc.). I've created a text property that accesses the combobox text so by using
VB.NET:
my_user_control.text = "some text"
the combobox is filled appropriately.

If I run this code directly from a buttons click event the user control fills the combobox text appropriately and does not remain highlighted. However, if I run this code from a constructor or other sub that is not via an event the combobox text fills but remains highlighted.

I need to populate forms from a db and fill the user control appropriately but want to remove the highlighting from the combobox. Someone suggested
VB.NET:
my_user_control.selectedindex = -1
but this simply clears any text from the combobox.

Can someone help me out? Such a seemingly trivial task of removing some highlighting is driving me nuts....!

Thanks in advance.
 
Hi,

I've cleared up some of the issues I've had with this. I don't think the issue is with the fill_combo function. However, in answer to your question, the combobox is filled via the constructor. This works fine and produces a list of professions as I require.

The problem is that when I read from the db, I can't seem to set the combo text to a value without the text remaining highlighted (see post
'Remove combo box highlighting' added today).

Thanks
 
Resolved

Hi,

Finally resolved this issue by overrriding the onpaint method of the user control as below;
VB.NET:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)

        Me.combobox.SelectionLength = 0

    End Sub

Using selectedindex = -1 only blanked out the text.

I'm still a bit perplexed as to why the problem arose as it did (as I described earlier) but most importantly, this solves it.

Thanks
 
Back
Top