By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,294 Members | 2,650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,294 IT Pros & Developers. It's quick & easy.

Clearing items from combobox

P: n/a
Hola

I wanted to kno how to clear the current text that is displayed in my combobox
I am filling it with a query which is run against my database

I have tried this but it not work

Combobox1.text = "

am i using the wrong event
Can someone advise me with correction?
Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I think anyone will have to have additional info to answer your question.
In the simpliest of cases, manually adding the items from the result of the
query, the line of code you illustrated will clear the text of the combo.
Therefore, I have to ask, does your statement that you are filling the combo
with a query, mean that the combo is bound to a query result? If so, where
the Combobox.text = "" instruction located; ie, what event? Are you setting
any other properties of the combo, ie, DisplayMember, etc. I don't know for
sure that I can answer the question, but I know I would need more info to
answer it. Maybe someone else can answer it with the current info, I can't.
If you provide answers to these questions, maybe someone can answer the
problem question.

http://www.knowdotnet.com

"Newbee" <an*******@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
Hola,

I wanted to kno how to clear the current text that is displayed in my combobox. I am filling it with a query which is run against my database.

I have tried this but it not work.

Combobox1.text = ""

am i using the wrong event?
Can someone advise me with correction?

Jul 21 '05 #2

P: n/a
Newbee:

What event are you firing this from? Is it a bound control or are you
populating it as you walk through a datareader for instance.

I just added a bound combobox control to a form, bound it to an array and
used this code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

ComboBox1.Text = String.Empty

End Sub
Which worked fine. If I had done it on Form_Load, it wouldn't have b/c the
binding took place after I called this, perhaps that's the problem? In
addition, I set the SelectedIndex to -1 and that cleared it as well.
ComboBox1.SelectedIndex = -1

Can you post the whole code snippet or tell me more about how it's populated
and where you are calling this from?

"Newbee" <an*******@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
Hola,

I wanted to kno how to clear the current text that is displayed in my combobox. I am filling it with a query which is run against my database.

I have tried this but it not work.

Combobox1.text = ""

am i using the wrong event?
Can someone advise me with correction?

Jul 21 '05 #3

P: n/a
I'd seriously recommend another approach....First, pull over two queries
with two dataadapters. If the tables are in a Parent Child format, (where
the top combobox uses the parents data and the child data is displayed in
the bottom one, you may want to use a DataRelation). Or, if they aren't,
you can just bind the bottom one to a dataview and set it's rowfilter to
correspond to whatever was selected in the top combobox.

DataReaders rock and they are very easy to use..but you'll potentially be
making a LOT of trips to the DB that aren't necessary if you do it that way.

However, more to the point, what happens if you set the selectedIndex of the
combobox to -1? That should do it for you although I just used similar code
and can't cause the problem to happen.
If you want to pursue the DataBound method, this should help, and either
way, the -1 index should do it for you.
Here's some code I use to do essentailly the same thing with a grid. I bind
the grid to a dataview..Then when the comboboxes selectedindex changes, I
reset the rowfilter:
Dim dv2 As New DataView(ds.Tables(0))

dv2.RowFilter = "Work_Type = '" CType(cboDepartment.SelectedItem, String)"'"

"Newbee" <an*******@discussions.microsoft.com> wrote in message
news:4B**********************************@microsof t.com...
I am filling the combobox like so:
Try
GioCn.Open()
Dim reader3 As SqlDataReader
reader3 = cmd.ExecuteReader()
While reader3.Read
ComboBox1.Items.Add(reader3.GetSqlString(0))
End While
Catch ex As Exception
MsgBox(ex.Message)
Finally
GioCn.Close()
End Try

The above coding is fired when the combobox0 (the combobox above it) value is changed. (valuechanged). i.e when combobox0 value is changed, this code
is run to fill comboBox1. I write the combobox1.text = "" in a button onclick event. (like a reset button)
the code does not seem to clear the text value of my combobox!!! :(

Jul 21 '05 #4

P: n/a
I'm about to head home now...if you want, I can send you a working example
of this. Cheers,

Bill
"Newbee" <an*******@discussions.microsoft.com> wrote in message
news:4B**********************************@microsof t.com...
I am filling the combobox like so:
Try
GioCn.Open()
Dim reader3 As SqlDataReader
reader3 = cmd.ExecuteReader()
While reader3.Read
ComboBox1.Items.Add(reader3.GetSqlString(0))
End While
Catch ex As Exception
MsgBox(ex.Message)
Finally
GioCn.Close()
End Try

The above coding is fired when the combobox0 (the combobox above it) value is changed. (valuechanged). i.e when combobox0 value is changed, this code
is run to fill comboBox1. I write the combobox1.text = "" in a button onclick event. (like a reset button)
the code does not seem to clear the text value of my combobox!!! :(

Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.