textbox and listbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I'm going around in circles with this problem and I hope someone can help me.

I have a listbox that is populated from a SQL Server database, with about 600 items, all strings.
I have a textbox on the same form, where I want to display which item is selected.

In the 'OnSelectedIndexChanged' event, I need code that has the textbox update to whatever value is currently selected in the listbox.

If I use the code:

TextBox1.Text = ListBox1.SelectedItem.toString

it diplays the incorrect item in the textbox.

If I use a datarow view with the following code:
Dim drv As DataRowView = lBoxCP.SelectedItem
txtCPMain.Text = drv("STR_CUTTING_PERMIT")
It works quite well, but after clicking several different items, the text box starts showing incorrect items.

SelectionMode is set to One.

Any suggestions?
Thanks,
Amber
 
Hi,

Try binding the textbox and the listbox to the same datasource. The
currency manager will keep them in sync.

Ken
--------------
amber said:
Hello,
I'm going around in circles with this problem and I hope someone can help me.

I have a listbox that is populated from a SQL Server database, with about 600 items, all strings.
I have a textbox on the same form, where I want to display which item is selected.

In the 'OnSelectedIndexChanged' event, I need code that has the textbox
update to whatever value is currently selected in the listbox.
If I use the code:

TextBox1.Text = ListBox1.SelectedItem.toString

it diplays the incorrect item in the textbox.

If I use a datarow view with the following code:
Dim drv As DataRowView = lBoxCP.SelectedItem
txtCPMain.Text = drv("STR_CUTTING_PERMIT")
It works quite well, but after clicking several different items, the text
box starts showing incorrect items.
 
Hi Amber,

Are you sure you use the protected event 'OnSelectedIndexChanged' because
than I think you have to show some code to give us an idea what you are
doing.

Cor
 
Thanks Ken

It is bound to the same dataset, isn't it? Here's my code

The list box is 1st populated by this code

Private Sub get_all_CPs(

lBoxCP.DataSource = Nothin
lBoxCP.Items.Clear(
lBoxCP.Refresh(

DsCP1.Clear(
SqlDA_CP.Fill(DsCP1

Dim dvCP As DataVie
dvCP = New DataVie

With dvC
.Table = DsCP1.Tables("TDT_CUTTING_PERMIT"
.Sort = "STR_CUTTING_PERMIT
End Wit

lBoxCP.DataSource = dvC
lBoxCP.DisplayMember = "STR_CUTTING_PERMIT
lBoxCP.Refresh(

End Su

And here's the listbox event code

Private Sub lBoxCP_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handle
lBoxCP.SelectedIndexChange

Dim drv As DataRowView = lBoxCP.SelectedIte
txtCPMain.Text = drv("STR_CUTTING_PERMIT"

End Su

Thanks
Ambe
 
Hi Amber,

That is what I thought it is the

SelectedIndexChanged not OnSelectedIndexChanged, which exist also

However when you add that one row I did you can delete that complete event
in my opinion
\\\
lBoxCP.DataSource = dvCP
lBoxCP.DisplayMember = "STR_CUTTING_PERMIT"
txtCPMain.Text.DataBindings.Add(Text",dvCP,"SRT_CUTTING_PERMIT")
lBoxCP.Refresh()
///

I do not know if you fill the dataset more and than you can set by the clear
of the dataset
txtCPMain.Text.Databindings.clear

I hope this helps?

Cor
 
I do not know if you fill the dataset more and than you can set by the
clear

typos

I do not know if you fill the dataset more than once and than you can set by
the clear
 
Hi,

Instead of setting the value in the selected index changed event
try this.

Private Sub get_all_CPs()

lBoxCP.DataSource = Nothing
lBoxCP.Items.Clear()
lBoxCP.Refresh()

DsCP1.Clear()
SqlDA_CP.Fill(DsCP1)

Dim dvCP As DataView
dvCP = New DataView

With dvCP
.Table = DsCP1.Tables("TDT_CUTTING_PERMIT")
.Sort = "STR_CUTTING_PERMIT"
End With

lBoxCP.DataSource = dvCP
lBoxCP.DisplayMember = "STR_CUTTING_PERMIT"
lBoxCP.Refresh()

txtCPMain.DataBindings.Add("Text", dvCP, "STR_CUTTING_PERMIT")


End Sub

Ken
--------------
amber said:
Thanks Ken,

It is bound to the same dataset, isn't it? Here's my code:

The list box is 1st populated by this code:

Private Sub get_all_CPs()

lBoxCP.DataSource = Nothing
lBoxCP.Items.Clear()
lBoxCP.Refresh()

DsCP1.Clear()
SqlDA_CP.Fill(DsCP1)

Dim dvCP As DataView
dvCP = New DataView

With dvCP
.Table = DsCP1.Tables("TDT_CUTTING_PERMIT")
.Sort = "STR_CUTTING_PERMIT"
End With

lBoxCP.DataSource = dvCP
lBoxCP.DisplayMember = "STR_CUTTING_PERMIT"
lBoxCP.Refresh()

End Sub

And here's the listbox event code:

Private Sub lBoxCP_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles
 
Back
Top