473,769 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_C lick(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles btnCol1ToCol2.C lick

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

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

End Sub

Nov 19 '05 #1
6 1557
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.goo glegroups.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_C lick(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles btnCol1ToCol2.C lick

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

lblDisplay.Text = "Deleted: <b>" & lbColumn1.Selec tedItem.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.goo glegroups.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 hlbtnDeleteDraw ings_Click(ByVa l sender As System.Object,
ByVal e As System.EventArg s) Handles hlbtnDeleteDraw ings.Click

If Page.IsPostBack Then
PanelDeleteDraw ings.Visible = True
PanelAddDrawing s.Visible = False
PanelOptions.Vi sible = True
lblDrawingList. Text = "Delete drawings from: <b>" &
ddlDrawingList. SelectedItem.Te xt() & "</b>"
End If

PopulateListBox ()

End Sub
_____________

This is the function that populates the listbox with data.

Function PopulateListBox ()

Dim strDLNumber As String = ddlDrawingList. SelectedItem.Te xt()

Dim objConn As New
OleDb.OleDbConn ection(Configur ationSettings.A ppSettings("str Connection"))
Dim strSQL As String = "SELECT tblDrawings.Dra wingNumber,
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.OleDbComm and(strSQL, objConn)
Dim myCommand As New OleDb.OleDbData Adapter(strSQL, objConn)

myCommand.Fill( ds, "DataTable" )

lbColumn1.DataS ource = ds.Tables("Data Table")
lbColumn1.DataT extField = "DrawingNum ber"
lbColumn1.DataV alueField = "ID"
lbColumn1.DataB ind()

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

Private Sub btnCol1ToCol2_C lick(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles btnCol1ToCol2.C lick

Dim s As String = lbColumn1.Selec tedItem.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.EventArg s) 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
ListBox2.Items. Add(ListBox1.Se lectedItem.Text )
ListBox1.Items. Remove(ListBox1 .SelectedItem.T ext)
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.Te xt()

Dim objConn As New
OleDb.OleDbConn ection(Configur ationSettings.A ppSettings("str Connection"))
Dim strSQL As String = "SELECT tblDrawings.Dra wingNumber,
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.OleDbComm and(strSQL, objConn)
Dim myCommand As New OleDb.OleDbData Adapter(strSQL, objConn)

myCommand.Fill( ds, "DataTable" )

lbColumn1.DataS ource = ds.Tables("Data Table")
lbColumn1.DataT extField = "DrawingNum ber"
lbColumn1.DataV alueField = "ID"
lbColumn1.DataB ind()

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.goo glegroups.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 hlbtnDeleteDraw ings_Click(ByVa l sender As System.Object,
ByVal e As System.EventArg s) Handles hlbtnDeleteDraw ings.Click

If Page.IsPostBack Then
PanelDeleteDraw ings.Visible = True
PanelAddDrawing s.Visible = False
PanelOptions.Vi sible = True
lblDrawingList. Text = "Delete drawings from: <b>" &
ddlDrawingList. SelectedItem.Te xt() & "</b>"
End If

PopulateListBox ()

End Sub
_____________

This is the function that populates the listbox with data.

Function PopulateListBox ()

Dim strDLNumber As String = ddlDrawingList. SelectedItem.Te xt()

Dim objConn As New
OleDb.OleDbConn ection(Configur ationSettings.A ppSettings("str Connection"))
Dim strSQL As String = "SELECT tblDrawings.Dra wingNumber,
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.OleDbComm and(strSQL, objConn)
Dim myCommand As New OleDb.OleDbData Adapter(strSQL, objConn)

myCommand.Fill( ds, "DataTable" )

lbColumn1.DataS ource = ds.Tables("Data Table")
lbColumn1.DataT extField = "DrawingNum ber"
lbColumn1.DataV alueField = "ID"
lbColumn1.DataB ind()

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

Private Sub btnCol1ToCol2_C lick(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles btnCol1ToCol2.C lick

Dim s As String = lbColumn1.Selec tedItem.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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Try

Dim s As ListItem = ListBox1.Select edItem

ListBox2.Items. Add(s)

ListBox1.Items. Remove(s)

ListBox1.Select edIndex = -1

ListBox2.Select edIndex = -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******** ********@TK2MSF TNGP14.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.EventArg s) 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
ListBox2.Items. Add(ListBox1.Se lectedItem.Text )
ListBox1.Items. Remove(ListBox1 .SelectedItem.T ext)
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.Te xt()

Dim objConn As New
OleDb.OleDbConn ection(Configur ationSettings.A ppSettings("str Connection"))
Dim strSQL As String = "SELECT tblDrawings.Dra wingNumber,
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.OleDbComm and(strSQL, objConn)
Dim myCommand As New OleDb.OleDbData Adapter(strSQL, objConn)

myCommand.Fill( ds, "DataTable" )

lbColumn1.DataS ource = ds.Tables("Data Table")
lbColumn1.DataT extField = "DrawingNum ber"
lbColumn1.DataV alueField = "ID"
lbColumn1.DataB ind()

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.goo glegroups.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 hlbtnDeleteDraw ings_Click(ByVa l sender As System.Object,
ByVal e As System.EventArg s) Handles hlbtnDeleteDraw ings.Click

If Page.IsPostBack Then
PanelDeleteDraw ings.Visible = True
PanelAddDrawing s.Visible = False
PanelOptions.Vi sible = True
lblDrawingList. Text = "Delete drawings from: <b>" &
ddlDrawingList. SelectedItem.Te xt() & "</b>"
End If

PopulateListBox ()

End Sub
_____________

This is the function that populates the listbox with data.

Function PopulateListBox ()

Dim strDLNumber As String = ddlDrawingList. SelectedItem.Te xt()

Dim objConn As New
OleDb.OleDbConn ection(Configur ationSettings.A ppSettings("str Connection"))
Dim strSQL As String = "SELECT tblDrawings.Dra wingNumber,
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.OleDbComm and(strSQL, objConn)
Dim myCommand As New OleDb.OleDbData Adapter(strSQL, objConn)

myCommand.Fill( ds, "DataTable" )

lbColumn1.DataS ource = ds.Tables("Data Table")
lbColumn1.DataT extField = "DrawingNum ber"
lbColumn1.DataV alueField = "ID"
lbColumn1.DataB ind()

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

Private Sub btnCol1ToCol2_C lick(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles btnCol1ToCol2.C lick

Dim s As String = lbColumn1.Selec tedItem.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
10625
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
15168
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 using the DataSource property. I set my ValueMember and DisplayMember listBox1.ValueMember = "TypeID";
2
2119
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; using System.Collections; using System.Collections.Specialized; using System.ComponentModel; using System.Data; using System.Drawing;
7
2510
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, later on I need to update this item. I find it in the Items collection and update a member used by the ToString member function. However, the text shown for the item in the ListBox does not change.
11
1807
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 have currently is (with the listbox set to AllowDrop): Sub MyListBox_MouseDown(args etc...) DoDragDrop(MyListBox.SelectedItem, DragDropEffects.Move) End Sub Sub MyListBox_DragEnter(args etc...)
2
3136
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 get all kinds of screen artifacts and weirdness. My general goal is: list item with a few areas for text, every other item shaded a light color for readability, font color changes with selection. The listbox is populated with custom structurs...
4
5042
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 of computations, etc). when all of the calculations are complete, I'm trying to change the listbox selection mode from single to MultiExtended, using: myListBox.SelectionMode=SelectionMode.MultiExtended
6
3659
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 that can be (dynamically) added to the right box. Adding the first item went fine, it shows up in the right box. Adding the second item resulted in losing _every_ control following the right box. 'View source' reveiled that the rendering of the...
2
2098
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 references to something like this in this old post http://groups.google.com/group/comp.lang.python/browse_thread/thread/3a6fe7534c812f55/43f201ab53cfdbf7 as well as here http://effbot.org/zone/wck-4.htm . It just seems that there should be some...
2
3933
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 contains a listbox and the second column contains some buttons. I am trying to display it such that if the listbox is empty it will be at least 100px wide, but if there is content in the box I want it to expand dynamically such that if the...
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9422
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10208
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9987
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7404
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5294
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2812
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.