473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Listbox Problems

The code below works to an extent. The problem with it is that no
matter which item is selected, the first one in the list is always the
item that gets added/removed. How can I alleviate this problem? I have
tried If Not Page.IsPostBack and still nothing.

Private Sub btnCol1ToCol2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCol1ToCol2.Click

Dim s As String = lbColumn1.SelectedItem.Text()
lbColumn1.Items.Add(s)
lbColumn2.Items.Remove(s)

lblDisplay.Text = "Deleted: <b>" & lbColumn1.SelectedItem.Text & "</b>"

End Sub

Nov 19 '05 #1
6 1539
Sparky,

Where did you put the If Not IsPostBack?

It sounds like you're databinding your list on every page load thus
resetting the selected item to the first.

The If Not IsPostBack should go around your databinding routine so that it
only occurrs the very first time the page is loaded.

Let me know if that isn't working.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Sparky Arbuckle" <tw*@secureroot.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
The code below works to an extent. The problem with it is that no
matter which item is selected, the first one in the list is always the
item that gets added/removed. How can I alleviate this problem? I have
tried If Not Page.IsPostBack and still nothing.

Private Sub btnCol1ToCol2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCol1ToCol2.Click

Dim s As String = lbColumn1.SelectedItem.Text()
lbColumn1.Items.Add(s)
lbColumn2.Items.Remove(s)

lblDisplay.Text = "Deleted: <b>" & lbColumn1.SelectedItem.Text & "</b>"

End Sub

Nov 19 '05 #2
I've tried putting the If Not IsPosBack around the sub that binds the
listboxes and also around the event handler that adds/removes the
selected listbox items. I have been unsuccessful down both avenues.

Nov 19 '05 #3
Sparky,

Is it possible to show more of the code? Or a short example with static
items in the lists to work with?

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Sparky Arbuckle" <tw*@secureroot.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
I've tried putting the If Not IsPosBack around the sub that binds the
listboxes and also around the event handler that adds/removes the
selected listbox items. I have been unsuccessful down both avenues.

Nov 19 '05 #4
Here you go Justin:

This is the sub that handles the "Delete Drawings" button. It simply
hides / shows panels and populates the listbox.

Private Sub hlbtnDeleteDrawings_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles hlbtnDeleteDrawings.Click

If Page.IsPostBack Then
PanelDeleteDrawings.Visible = True
PanelAddDrawings.Visible = False
PanelOptions.Visible = True
lblDrawingList.Text = "Delete drawings from: <b>" &
ddlDrawingList.SelectedItem.Text() & "</b>"
End If

PopulateListBox()

End Sub
_____________

This is the function that populates the listbox with data.

Function PopulateListBox()

Dim strDLNumber As String = ddlDrawingList.SelectedItem.Text()

Dim objConn As New
OleDb.OleDbConnection(ConfigurationSettings.AppSet tings("strConnection"))
Dim strSQL As String = "SELECT tblDrawings.DrawingNumber,
tblDrawings.ID FROM tblDrawingList INNER JOIN tblDrawings ON
tblDrawingList.ID = tblDrawings.ID WHERE (DLNumber = '" & strDLNumber &
"');"

Dim ds As New DataSet
Dim objCommand As New OleDb.OleDbCommand(strSQL, objConn)
Dim myCommand As New OleDb.OleDbDataAdapter(strSQL, objConn)

myCommand.Fill(ds, "DataTable")

lbColumn1.DataSource = ds.Tables("DataTable")
lbColumn1.DataTextField = "DrawingNumber"
lbColumn1.DataValueField = "ID"
lbColumn1.DataBind()

End Function
___________________________
This is the sub for the button to transfer Items from Column1 to
Column2:

Private Sub btnCol1ToCol2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCol1ToCol2.Click

Dim s As String = lbColumn1.SelectedItem.Text
lbColumn2.Items.Add(s)
lbColumn1.Items.Remove(s)

End Sub

Nov 19 '05 #5
Sparky,

A simple test shows that your code should be working. And that the problem
has to be that databinding is recurring somewhere.

Here's what I tested with.

WebForm Code:

<form id="Form1" method="post" runat="server">
<asp:ListBox id="ListBox1" runat="server"></asp:ListBox>
<asp:ListBox id="ListBox2" runat="server"></asp:ListBox>
<asp:Button id="Button2" runat="server" Text="Button"></asp:Button>
</form>

Code Behind:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
'---First page load only
ListBox1.Items.Add(New ListItem("1"))
ListBox1.Items.Add(New ListItem("2"))
ListBox1.Items.Add(New ListItem("3"))
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
ListBox2.Items.Add(ListBox1.SelectedItem.Text)
ListBox1.Items.Remove(ListBox1.SelectedItem.Text)
End Sub

Keeping the task simple like this the code works. The only thing that could
be creating the problem you are describing is if the list box is being
re-bound. Based on the code you sent me you should change your populate list
box line to:

Function PopulateListBox()

If Not IsPostBack Then

Dim strDLNumber As String = ddlDrawingList.SelectedItem.Text()

Dim objConn As New
OleDb.OleDbConnection(ConfigurationSettings.AppSet tings("strConnection"))
Dim strSQL As String = "SELECT tblDrawings.DrawingNumber,
tblDrawings.ID FROM tblDrawingList INNER JOIN tblDrawings ON
tblDrawingList.ID = tblDrawings.ID WHERE (DLNumber = '" & strDLNumber &
"');"

Dim ds As New DataSet
Dim objCommand As New OleDb.OleDbCommand(strSQL, objConn)
Dim myCommand As New OleDb.OleDbDataAdapter(strSQL, objConn)

myCommand.Fill(ds, "DataTable")

lbColumn1.DataSource = ds.Tables("DataTable")
lbColumn1.DataTextField = "DrawingNumber"
lbColumn1.DataValueField = "ID"
lbColumn1.DataBind()

End If

End Function
Of course there may be a better place in your code to put the If Not
IsPostBack, but this should work for testing...

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Sparky Arbuckle" <tw*@secureroot.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Here you go Justin:

This is the sub that handles the "Delete Drawings" button. It simply
hides / shows panels and populates the listbox.

Private Sub hlbtnDeleteDrawings_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles hlbtnDeleteDrawings.Click

If Page.IsPostBack Then
PanelDeleteDrawings.Visible = True
PanelAddDrawings.Visible = False
PanelOptions.Visible = True
lblDrawingList.Text = "Delete drawings from: <b>" &
ddlDrawingList.SelectedItem.Text() & "</b>"
End If

PopulateListBox()

End Sub
_____________

This is the function that populates the listbox with data.

Function PopulateListBox()

Dim strDLNumber As String = ddlDrawingList.SelectedItem.Text()

Dim objConn As New
OleDb.OleDbConnection(ConfigurationSettings.AppSet tings("strConnection"))
Dim strSQL As String = "SELECT tblDrawings.DrawingNumber,
tblDrawings.ID FROM tblDrawingList INNER JOIN tblDrawings ON
tblDrawingList.ID = tblDrawings.ID WHERE (DLNumber = '" & strDLNumber &
"');"

Dim ds As New DataSet
Dim objCommand As New OleDb.OleDbCommand(strSQL, objConn)
Dim myCommand As New OleDb.OleDbDataAdapter(strSQL, objConn)

myCommand.Fill(ds, "DataTable")

lbColumn1.DataSource = ds.Tables("DataTable")
lbColumn1.DataTextField = "DrawingNumber"
lbColumn1.DataValueField = "ID"
lbColumn1.DataBind()

End Function
___________________________
This is the sub for the button to transfer Items from Column1 to
Column2:

Private Sub btnCol1ToCol2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCol1ToCol2.Click

Dim s As String = lbColumn1.SelectedItem.Text
lbColumn2.Items.Add(s)
lbColumn1.Items.Remove(s)

End Sub

Nov 19 '05 #6
Sparky,

I've got it. (I'll post to the forum also.) I should have noticed that you
were setting you're list box item's text AND value.

Adding and removing an item by name as string works if just the text has
been set as in the example I sent you earlier, but if the items have both
text and value set you need to use the full list item in the add and remove
methods.

Like this:

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

Try

Dim s As ListItem = ListBox1.SelectedItem

ListBox2.Items.Add(s)

ListBox1.Items.Remove(s)

ListBox1.SelectedIndex = -1

ListBox2.SelectedIndex = -1

Catch ex As Exception

Throw ex

End Try

End Sub

(Errors are thrown the next time you remove an item if you don't set the
list boxes selected item to none.)

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...
Sparky,

A simple test shows that your code should be working. And that the problem
has to be that databinding is recurring somewhere.

Here's what I tested with.

WebForm Code:

<form id="Form1" method="post" runat="server">
<asp:ListBox id="ListBox1" runat="server"></asp:ListBox>
<asp:ListBox id="ListBox2" runat="server"></asp:ListBox>
<asp:Button id="Button2" runat="server" Text="Button"></asp:Button>
</form>

Code Behind:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
'---First page load only
ListBox1.Items.Add(New ListItem("1"))
ListBox1.Items.Add(New ListItem("2"))
ListBox1.Items.Add(New ListItem("3"))
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
ListBox2.Items.Add(ListBox1.SelectedItem.Text)
ListBox1.Items.Remove(ListBox1.SelectedItem.Text)
End Sub

Keeping the task simple like this the code works. The only thing that
could be creating the problem you are describing is if the list box is
being re-bound. Based on the code you sent me you should change your
populate list box line to:

Function PopulateListBox()

If Not IsPostBack Then

Dim strDLNumber As String = ddlDrawingList.SelectedItem.Text()

Dim objConn As New
OleDb.OleDbConnection(ConfigurationSettings.AppSet tings("strConnection"))
Dim strSQL As String = "SELECT tblDrawings.DrawingNumber,
tblDrawings.ID FROM tblDrawingList INNER JOIN tblDrawings ON
tblDrawingList.ID = tblDrawings.ID WHERE (DLNumber = '" & strDLNumber &
"');"

Dim ds As New DataSet
Dim objCommand As New OleDb.OleDbCommand(strSQL, objConn)
Dim myCommand As New OleDb.OleDbDataAdapter(strSQL, objConn)

myCommand.Fill(ds, "DataTable")

lbColumn1.DataSource = ds.Tables("DataTable")
lbColumn1.DataTextField = "DrawingNumber"
lbColumn1.DataValueField = "ID"
lbColumn1.DataBind()

End If

End Function
Of course there may be a better place in your code to put the If Not
IsPostBack, but this should work for testing...

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Sparky Arbuckle" <tw*@secureroot.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Here you go Justin:

This is the sub that handles the "Delete Drawings" button. It simply
hides / shows panels and populates the listbox.

Private Sub hlbtnDeleteDrawings_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles hlbtnDeleteDrawings.Click

If Page.IsPostBack Then
PanelDeleteDrawings.Visible = True
PanelAddDrawings.Visible = False
PanelOptions.Visible = True
lblDrawingList.Text = "Delete drawings from: <b>" &
ddlDrawingList.SelectedItem.Text() & "</b>"
End If

PopulateListBox()

End Sub
_____________

This is the function that populates the listbox with data.

Function PopulateListBox()

Dim strDLNumber As String = ddlDrawingList.SelectedItem.Text()

Dim objConn As New
OleDb.OleDbConnection(ConfigurationSettings.AppSet tings("strConnection"))
Dim strSQL As String = "SELECT tblDrawings.DrawingNumber,
tblDrawings.ID FROM tblDrawingList INNER JOIN tblDrawings ON
tblDrawingList.ID = tblDrawings.ID WHERE (DLNumber = '" & strDLNumber &
"');"

Dim ds As New DataSet
Dim objCommand As New OleDb.OleDbCommand(strSQL, objConn)
Dim myCommand As New OleDb.OleDbDataAdapter(strSQL, objConn)

myCommand.Fill(ds, "DataTable")

lbColumn1.DataSource = ds.Tables("DataTable")
lbColumn1.DataTextField = "DrawingNumber"
lbColumn1.DataValueField = "ID"
lbColumn1.DataBind()

End Function
___________________________
This is the sub for the button to transfer Items from Column1 to
Column2:

Private Sub btnCol1ToCol2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCol1ToCol2.Click

Dim s As String = lbColumn1.SelectedItem.Text
lbColumn2.Items.Add(s)
lbColumn1.Items.Remove(s)

End Sub


Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andrew | last post by:
Hi I just started learning wxPython I wanted to know how I could do this in wxPython self.listbox.delete(0, END) for item in self.results: self.listbox.insert(END, item)
1
by: Irwin M. Fletcher | last post by:
ListBox DataSource DisplayMember Property Problems in C# I have a (single select) listbox with data and when I click on the list I can't get the right text selected. My listbox is setup...
2
by: mathieu cupryk | last post by:
I have problems with listboxes in the webform2.cs, the textboxes are working well when I do a click on next. I am missing something. It works with the textboxes. Here is the file: using System;...
7
by: Grant Schenck | last post by:
Hello, I have a ListBox control on a form. I add members of a class to the Items collection. They show up and I can select them. The text shown is from my classes ToString override. Now,...
11
by: John Dann | last post by:
I'm still struggling to find a way of reordering the items within the same single listbox with drag and drop. I think I've got the drag working but it's the drop code I can't figure out. What I...
2
by: dan heskett | last post by:
I am owner-drawing a listbox, in an attempt to create a nice list with some custom "fields" and text layout. Essentially it works, but I must be missing something big, conceptually, because I...
4
by: lgbjr | last post by:
Hi All, I've got a listbox on a VB.NET form. when the form opens, the ListBox SelectionMode is set to Single. while running various routines on the form, items get added to the list box (results...
6
by: Papa.Coen | last post by:
I've just spend a lot of time solving the following problem: I used dotNet 2.0 / Visualstudio 2005 / C# / aspx, enable viewstate is set on all controls. I have 2 listboxes; the left contains items...
2
by: Steve Potter | last post by:
I am trying to find some method of attaching a Listbox object to a list object so as the contents of the list are changed the contents of the Listbox will be updated to match. I have found a few...
2
by: cshaw | last post by:
Hello Everyone, I am having problems with a listbox control. I have a page with a couple of labels and drop-down lists at the top, and then below there is a table with two columns, the first column...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.