TSFLLC
asked on
Why do my comboboxes display a value instead of being blank until a value is chosen from dropdown?
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.OleDbDat aAdapter
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.OleDbCom mand("SELE CT * FROM Contacts ORDER BY ContactName", TSFConnection)
daContacts.Fill(dsContacts )
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
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.OleDbDat
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.OleDbCom
daContacts.Fill(dsContacts
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
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 =""
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 =""
ASKER
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).NewR ow
dr.Item("ContactIndex") = ""
dsContacts1.Tables(0).Rows .InsertAt( dr, 0)
dr = dsContacts2.Tables(0).NewR ow
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.
Thanks!
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).NewR
dr.Item("ContactIndex") = ""
dsContacts1.Tables(0).Rows
dr = dsContacts2.Tables(0).NewR
dr.Item("ContactIndex") = ""
dsContacts2.Tables(0).Rows
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.
Thanks!
ASKER
OOPS! Where I wrote 'ContactIndex'....I meant 'ContactName'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That ought to clear it for you