Why do my comboboxes display a value instead of being blank until a value is chosen from dropdown?

Posted on 2006-04-12
Medium Priority
Last Modified: 2010-04-23
I'm rewriting a MSAccess application to VB.NET for more functionality.  I have come a long way with my understanding
but I'm puzzled with what is probably a simple fix for VB.NET novices.

I have a "Clear / New" button on a form for initializing all fields on the form in preparation for data entry of new record.
I have several combo boxes that are required fields and several that are not.  I do not want any of the combo boxes to
display a value until the user click's the dropdown and choses value.  Why do they all contain the first value from the
datatable instead of being blank...AND...more importantly, how do I clear these values until the control is chosen?

Below is my code in various parts:

Public daContacts As New System.Data.OleDb.OleDbDataAdapter
Public dsContacts As New DataSet
Public dtContacts1 As New DataTable
Public dtContacts2 As New DataTable
Public arrMrMrs() As String = {"Mr.", "Mrs.", "Ms.", "Dr."}

daContacts.SelectCommand = New System.Data.OleDb.OleDbCommand("SELECT * FROM Contacts ORDER BY ContactName", TSFConnection)
dtContacts1 = dsContacts.Tables(0)
dtContacts2 = dsContacts.Tables(0)

cmboBuyer.DataSource = dtContacts1
cmboBuyer.ValueMember = "RecID"
cmboBuyer.DisplayMember = "ContactName"

cmboSeller.DataSource = dtContacts2
cmboSeller.ValueMember = "RecID"
cmboSeller.DisplayMember = "ContactName"

cmboMrMrs.DataSource = arrMrMrs
Question by:TSFLLC
  • 2
  • 2

Expert Comment

ID: 16440463
Have you tried Combobox1.Text=""

That ought to clear it for you

Expert Comment

ID: 16440603
oh... and one more thing... Use

        ComboBox1.Text = ""
        ComboBox1.SelectedIndex = -1 '(Meaning NO item should be selected)

Just to make sure that nothing is selected in the Combobox1... Since It will maintain any selection even if Combobox1.Text =""

Author Comment

ID: 16440834
I found both of these answers previously and neither one of them worked because they are bound to a datatable.

I did find this piece of code and it does work.
However, I don't like it for (2) reasons:

1) An inordinate amount of code to clear a field.
2) An empty first record when you click on the dropdown.

But it does work....maybe someone else has a better way.

        Dim dr As DataRow
        dr = dsContacts1.Tables(0).NewRow
        dr.Item("ContactIndex") = ""
        dsContacts1.Tables(0).Rows.InsertAt(dr, 0)

        dr = dsContacts2.Tables(0).NewRow
        dr.Item("ContactIndex") = ""
        dsContacts2.Tables(0).Rows.InsertAt(dr, 0)

        cmboSeller.SelectedIndex = 0
        cmboBuyer.SelectedIndex = 0

In MS Access, even though you have a datasource of a SQL statement......
It is simply a matter of cmboseller.text = "" to resolve the problem.


Author Comment

ID: 16440852
OOPS!   Where I wrote 'ContactIndex'....I meant 'ContactName'
LVL 14

Accepted Solution

jake072 earned 1500 total points
ID: 16442297
After I set a datasource, when I set the ComboBox's SelectedIndex = -1, and no value is chosen - depending on the type of ComboBox you've chosen...  If the user must select a value (DropDownStyle=DropDownList), than I don't think there's another way.


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question