J Talati J Talati - 7 months ago 11
Vb.net Question

Add items to ListBox on page load and transfer an item to another ListBox on button click

'This Code is not working. Please Help.
'Not sure what is needed to do here.

Public Class WebForm2
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ListBox1.Items.Add("test")
ListBox1.Items.Add("test2")
End Sub

Protected Sub AddOneButton0_Click(sender As Object, e As EventArgs) Handles AddOneButton0.Click
If (ListBox1.SelectedIndex = -1) Then
MsgBox("please select an item")
Else
ListBox2.Items.Add(ListBox1.SelectedItem)
ListBox1.Items.Remove(ListBox1.SelectedItem)
End If
End Sub


End Class

'Adding HTML to help figure out if i did anything wrong with my code.

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="WebForm2.aspx.vb" Inherits="GBATExcel.WebForm2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<!DOCTYPE html>

<html>
<head>
<title></title>
</head>
<body>
<div style="height: 904px">

<asp:ListBox ID="ListBox1" runat="server" style="position:absolute; top: 101px; left: 28px; height: 170px; width: 111px;"></asp:ListBox>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;

<br />

<asp:ListBox ID="ListBox2" runat="server" style="position:absolute; top: 103px; left: 223px; height: 167px; width: 131px;"></asp:ListBox>
<asp:Button ID="RemoveAllButton" runat="server" Text="&lt;&lt;" style="position:absolute; top: 435px; left: 153px;"/>

<asp:Button ID="AddOneButton2" runat="server" Text="&gt;" style="position:absolute; top: 308px; left: 157px;"/>

<asp:Button ID="RemoveOneButton2" runat="server" Text="&lt;" style="position:absolute; top: 565px; left: 156px;"/>

<asp:Button ID="AddAllButton" runat="server" Text="&gt;&gt;" style="position:absolute; top: 194px; left: 153px;"/>

<asp:ListBox ID="ListBox3" runat="server" style="position:absolute; top: 302px; left: 29px; height: 177px; width: 106px;"></asp:ListBox>

<asp:ListBox ID="ListBox4" runat="server" style="position:absolute; top: 520px; left: 223px; height: 170px; width: 129px;"></asp:ListBox>

<asp:Button ID="AddOneButton0" runat="server" Text="&gt;" style="position:absolute; top: 110px; left: 157px;"/>

<asp:Button ID="RemoveOneButton0" runat="server" Text="&lt;" style="position:absolute; top: 151px; left: 158px;"/>

<asp:Button ID="RemoveAllButton0" runat="server" Text="&lt;&lt;" style="position:absolute; top: 233px; left: 154px;"/>

<asp:ListBox ID="ListBox5" runat="server" style="position:absolute; top: 730px; left: 28px; height: 170px; width: 111px;"></asp:ListBox>

<asp:ListBox ID="ListBox6" runat="server" style="position:absolute; top: 302px; left: 223px; height: 171px; width: 128px;"></asp:ListBox>

<asp:Button ID="AddOneButton3" runat="server" Text="&gt;" style="position:absolute; top: 737px; left: 156px;"/>

<asp:Button ID="RemoveOneButton3" runat="server" Text="&lt;" style="position:absolute; top: 350px; left: 157px;"/>

<asp:Button ID="AddAllButton1" runat="server" Text="&gt;&gt;" style="position:absolute; top: 396px; left: 153px;"/>

<asp:Button ID="RemoveAllButton1" runat="server" Text="&lt;&lt;" style="position:absolute; top: 648px; left: 152px;"/>

<asp:ListBox ID="ListBox7" runat="server" style="position:absolute; top: 520px; left: 29px; height: 170px; width: 110px;"></asp:ListBox>

<asp:ListBox ID="ListBox8" runat="server" style="position:absolute; top: 729px; left: 222px; height: 170px; width: 134px;"></asp:ListBox>

<asp:Button ID="AddOneButton4" runat="server" Text="&gt;" style="position:absolute; top: 525px; left: 156px;"/>

<asp:Button ID="RemoveOneButton4" runat="server" Text="&lt;" style="position:absolute; top: 776px; left: 156px;"/>

<asp:Button ID="AddAllButton2" runat="server" Text="&gt;&gt;" style="position:absolute; top: 607px; left: 152px;"/>

<asp:Button ID="AddAllButton0" runat="server" Text="&gt;&gt;" style="position:absolute; top: 819px; left: 152px;"/>

<asp:Button ID="RemoveAllButton2" runat="server" Text="&lt;&lt;" style="position:absolute; top: 864px; left: 152px;"/>

</div>
</body>
</html>
</asp:Content>


'Basically i am having trouble with this and wanted some help

Answer

You should add the items inside a Not IsPostBack condition:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        ListBox1.Items.Add("test")
        ListBox1.Items.Add("test2")
    End If
End Sub

And remove the item from ListBox1 before adding it to ListBox2:

Protected Sub AddOneButton0_Click(sender As Object, e As EventArgs) Handles AddOneButton0.Click
    If (ListBox1.SelectedIndex = -1) Then
        MsgBox("please select an item")
    Else
        Dim item As ListItem = ListBox1.SelectedItem
        ListBox1.Items.Remove(item)
        ListBox2.Items.SelectedIndex = -1
        ListBox2.Items.Add(item)
    End If
End Sub

Note: the selected item must be reset before inserting the new item in ListBox2

Comments