Pages

Showing posts with label ListBox. Show all posts
Showing posts with label ListBox. Show all posts

Thursday 9 July 2015

List Box Control

Introduction :
This code will show how to use ListBox to Select/Filter items :
Design Code :
<div style="width: 100%; float: left;">
            <div style="width: 25%; float: left; margin-top: 10px;">
                <asp:ListBox ID="ListBox1" runat="server" Height="150px" Width="98%" SelectionMode="Multiple">
                    <asp:ListItem>A</asp:ListItem>
                    <asp:ListItem>B</asp:ListItem>
                    <asp:ListItem>C</asp:ListItem>
                    <asp:ListItem>D</asp:ListItem>
                    <asp:ListItem>E</asp:ListItem>
                    <asp:ListItem>F</asp:ListItem>
                </asp:ListBox>
            </div>
            <div style="width: 8%; float: left; margin: 10px;">
                <asp:Button ID="btn01" runat="server" OnClick="btn01_Click" Text=">" Style="margin: 3px 14px;" />
                <asp:Button ID="Button2" runat="server" OnClick="Button1_Click" Text=">>" Style="margin: 3px 14px;" />
                <asp:Button ID="Button3" runat="server" OnClick="Button2_Click" Text="<" Style="margin: 3px 14px;" />
                <asp:Button ID="Button4" runat="server" OnClick="Button3_Click" Text="<<" Style="margin: 3px 14px;" />
            </div>

            <div style="width: 25%; float: left; margin-top: 10px;">
                <asp:ListBox ID="ListBox2" runat="server" Height="150px" Width="98%" SelectionMode="Multiple"></asp:ListBox>
            </div>
            <asp:Label ID="lbltxt" runat="server" ForeColor="Red"></asp:Label>
        </div>


Code Behind : 

ArrayList arraylist1 = new ArrayList();
    ArrayList arraylist2 = new ArrayList();
    protected void btn01_Click(object sender, EventArgs e)
    {
        lbltxt.Visible = false;
        if (ListBox1.SelectedIndex >= 0)
        {
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                if (ListBox1.Items[i].Selected)
                {
                    if (!arraylist1.Contains(ListBox1.Items[i]))
                    {
                        arraylist1.Add(ListBox1.Items[i]);
                    }
                }
            }
            for (int i = 0; i < arraylist1.Count; i++)
            {
                if (!ListBox2.Items.Contains(((ListItem)arraylist1[i])))
                {
                    ListBox2.Items.Add(((ListItem)arraylist1[i]));
                }
                ListBox1.Items.Remove(((ListItem)arraylist1[i]));
            }
            ListBox2.SelectedIndex = -1;
        }
        else
        {
            lbltxt.Visible = true;
            lbltxt.Text = "Please select atleast one in Listbox1 to move";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lbltxt.Visible = false;
        while (ListBox1.Items.Count != 0)
        {
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                ListBox2.Items.Add(ListBox1.Items[i]);
                ListBox1.Items.Remove(ListBox1.Items[i]);
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        lbltxt.Visible = false;
        if (ListBox2.SelectedIndex >= 0)
        {
            for (int i = 0; i < ListBox2.Items.Count; i++)
            {
                if (ListBox2.Items[i].Selected)
                {
                    if (!arraylist2.Contains(ListBox2.Items[i]))
                    {
                        arraylist2.Add(ListBox2.Items[i]);
                    }
                }
            }
            for (int i = 0; i < arraylist2.Count; i++)
            {
                if (!ListBox1.Items.Contains(((ListItem)arraylist2[i])))
                {
                    ListBox1.Items.Add(((ListItem)arraylist2[i]));
                }
                ListBox2.Items.Remove(((ListItem)arraylist2[i]));
            }
            ListBox1.SelectedIndex = -1;
        }
        else
        {
            lbltxt.Visible = true;
            lbltxt.Text = "Please select atleast one in Listbox2 to move";
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        lbltxt.Visible = false;
        while (ListBox2.Items.Count != 0)
        {
            for (int i = 0; i < ListBox2.Items.Count; i++)
            {
                ListBox1.Items.Add(ListBox2.Items[i]);
                ListBox2.Items.Remove(ListBox2.Items[i]);
            }
        }

    }