Populating initial Combobox display text

K

Keith

Hello - this started out as a minor annoyance - and now is
starting to bother me more and more - I'm hoping someone
can help me.

I would like to have a combobox display - NOT initially be
blank - but contain a value. However the value is just
for user reference - not really one of the combobox
choices. For example intially the combobox should equal
the word Select - and the item values can be
say 'one', 'two', and 'three'.

I tried just typing in the text value - in the properties
of the combobox control/object - but that did not work.
Someone told me in order to display the value - it has to
be an existing part of the collection. Doesn't completely
make sense to me - because why have a Text properties
field of the control - if you can't use it (everytime you
fill something in - and leave that field - it's blanked
out)

So what I ended up doing was - adding the item to the:
Windows Form Designer Generated Code Section - something
like this:

Me.ComboBox1.Items.Add("Select")
Me.ComboBox1.Items.Add("one")
Me.ComboBox1.Items.Add("two")
Me.ComboBox1.Items.Add("three")
Me.ComboBox1.Location = New System.Drawing.Point
(8, 25)
Me.ComboBox1.Size = New System.Drawing.Size(220,
22)
Me.ComboBox1.Text = "Select"

Well - it works - but here's the real problem. Every now
and then (I think when I click on that combobox - or even
on the form somehow - to open the design) - it removes the
last line: Me.ComboBox1.Text = "Select".

So - sometimes I compile - and I get "Select" as the
initial default value for the combobox and other times - I
do not and I have to re-add it. Sure - minor annoyance -
but when you have to do this 10, 20, 30, 50 times - it
becomes a MAJOR annoyance.

Am I doing something wrong?
Can someone suggest a better way?
Thank you.
 
S

SStory

Sounds like you are trying to do a webpage type thing of having a value that
isn't in the list.

Looks like to me that if the style of the combobox is such that you can edit
the text, setting the text property should be allowed?

Of course if the style is one of the other types it of course won't let you
do this. Have you tried it with the correct style?

Shane
 
C

Cor Ligthert

HI Keith,

I think that your problem has to do with one of the few documentated on MSDE
documented bugs in the combobox.

However even beside that I think this would be a better solution for you
problem (I made this sample for you so you have to try it).

I hope this helps?

Cor
\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.ComboBox1.SelectedIndex = 0
Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
End Sub
Private Sub ComboBox1_SelectedIndexChanged _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles _
ComboBox1.SelectedIndexChanged
If Me.ComboBox1.Text <> "Select" Then
'do something
End If
Me.ComboBox1.SelectedIndex = 0
End Sub
///
 
K

Keith

Cor,

thanks for you code - I tried it - but didn't get the
results I expected. Perhaps I did not accurately state
what I was attempting to do. I wanted to have the Select
text appear - as the initial text of the Combobox - and
then once the person clicked on any of the choices
(besides Select) - the Select code would disappear. I
ended up doing it in the following manner:
(just curious - you mention this combobox bug is well
know - any idea when anyone will "fix" it?)

Keep in mind this code is out there - but it works...
The Combobox Collection is populated with the following
entries: Select, One, and Two
Declare a public variable:
Public firsttimeonlycounter As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Text = "Select"
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ComboBox1.SelectedIndexChanged
If firsttimeonlycounter <> 1 And
ComboBox1.SelectedItem <> "Select" Then
ComboBox1.Items.Remove("Select")
firsttimeonlycounter = 1
End If
End Sub

Thanks again.
 
C

Cor Ligthert

Hi Keith,

I thought that the problem was that you did want keep the "Select", because
standard when you set in the loadevent (or in the text in the designer)
combobox.text = "Select" that is only showed once

:)

Cor
 

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