Databind combo box with our showing first row of data

  • Thread starter Thread starter moondaddy
  • Start date Start date
M

moondaddy

I'm writing an app in vb.net 1.1 and all of my forms use a combo box to
navigate to different records. when the form loads I databind a dataset
table to the combo box. However, the form is opening up to a blank - new
record and I don't want any text showing in the text area of the combo box.
I want the combo box's text area to show nothing until the user selects
something from its list. Below is the code used to populate the combo and
some remmed lines where it tried to force the combo to have nothing
selected. Can someone please advise me on how to do this?

Thanks!


Private Sub cmbGoToRowSource(ByRef tb As
wsContactMangaer.dsContactMngr.tbAddressListByCustDataTable)
Try
Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
Me.cmbGoTo.DataSource = tb
Me.cmbGoTo.DisplayMember = "Name"
Me.cmbGoTo.ValueMember = "ID"
'Me.cmbGoTo.SelectedIndex = -1 'These 3 lines still left text in
the combo box text area
'Me.cmbGoTo.Text = ""
'Me.cmbGoTo.SelectedText = ""

Catch ex As Exception
ErrLog(ex)
Finally
Cursor.Current = System.Windows.Forms.Cursors.Default
End Try
End Sub
 
Thanks. I found that I only needed to set the selectedindex to "-1" one
time, but after the form was finished loading and set to visible.
aprearantly setting the selected index to -1 doesn't work if the form hasn't
been set to visible yet. I figured this out be adding a test button and
setting the index to -1 in the button's click event. This is unfortunate
because all of my form's initialization code is called from the new sub
where I pass in parameters to tell the form what data to populate with, etc.
like this:

private frm as Myform
'========= now from a method
frm = new Myform (PK, flag1, flag2)
frm.visible = true

so I tried a hack work around by executing the code:
Me.cmbGoTo.SelectedIndex = -1
from the forms' visiblechanged event so when the form finally becomes
visible the combo boxes' text is cleared. this work, but I wouldn't want to
use it as a standard way of doing things because what happens if I need to
change a form's visible property? the dropdown boxes' indexes will all
change and cause havoc.

can anyone think of a better solution? it would be great if there was an
event such as form finished loading event if selected index = -1 would work
in such an event.

any good ideas?

Thanks again.



--
(e-mail address removed)
Tim Wilson said:
In order to clear a databound ComboBox, try setting the SelectedIndex to
"-1" twice.
http://support.microsoft.com/default.aspx?scid=kb;en-us;327244

--
Tim Wilson
.Net Compact Framework MVP

<Feedback>
Do have an opinion on the effectiveness of Microsoft Windows Mobile and
Embedded newsgroups? Let us know!
https://www.windowsembeddedeval.com/community/newsgroups
</Feedback>
 
Yeah, I've run into this problem before as well. It's kind of a pain. One
way to do this is to override the forms OnActivated method (or hook the
forms Activated event) and perform any one time form initialization there.
But you'll need to use a flag to ensure that the code is called only once.

private bool initialized = false;

protected override void OnActivated (System.EventArgs e)
{
if (!initialized)
{
initialized = true;
this.comboBox1.SelectedIndex = -1;
}
base.OnActivated(e);
}

--
Tim Wilson
..Net Compact Framework MVP

<Feedback>
Do have an opinion on the effectiveness of Microsoft Windows Mobile and
Embedded newsgroups? Let us know!
https://www.windowsembeddedeval.com/community/newsgroups
</Feedback>

moondaddy said:
Thanks. I found that I only needed to set the selectedindex to "-1" one
time, but after the form was finished loading and set to visible.
aprearantly setting the selected index to -1 doesn't work if the form hasn't
been set to visible yet. I figured this out be adding a test button and
setting the index to -1 in the button's click event. This is unfortunate
because all of my form's initialization code is called from the new sub
where I pass in parameters to tell the form what data to populate with, etc.
like this:

private frm as Myform
'========= now from a method
frm = new Myform (PK, flag1, flag2)
frm.visible = true

so I tried a hack work around by executing the code:
Me.cmbGoTo.SelectedIndex = -1
from the forms' visiblechanged event so when the form finally becomes
visible the combo boxes' text is cleared. this work, but I wouldn't want to
use it as a standard way of doing things because what happens if I need to
change a form's visible property? the dropdown boxes' indexes will all
change and cause havoc.

can anyone think of a better solution? it would be great if there was an
event such as form finished loading event if selected index = -1 would work
in such an event.

any good ideas?

Thanks again.
 
Thanks I thought about doing something like this, but then thought that I
just didn't know how to do it correctly. maybe it will be better in 2.0
 
Back
Top