Examples with ListBox:-



Add a new Form i.e.,FormListBox1 to the Windows Application WABasics
Create two labels,two textboxes  and one listBox & set the following properties to them


label1:
     Text      : SelectedItem
     Name   : lblItem


label2:
     Text     : SelectedIndex
     Name   : lblIndex




textbox1:
     Name   : txtItem



textbox2:
     Name   : txtIndex




listBox1:
     Name   : lstSample
Items         : Click on Ellipsis button this will open the
StringCollectionEditor
Enter Strings in the Collection click on Ok

   



Design :-

 
  
Generate theEventHandler for lstSample selectedIndexChanged  Event
& write the following code in Source[FormListBox1.cs] 


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WABasics
{
    public partial class FormListBox1 : Form
    {
        public FormListBox1()
        {
            InitializeComponent();
        }

        private void lstSample_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtIndex.Text = lstSample.SelectedIndex.ToString();
            txtItem.Text = lstSample.SelectedItem.ToString();
        }

       
    }
}




Result :-






Add a new Form i.e.,FormListBox2 to the Windows Application WABasics
Create two labels,two textboxes  and one listBox ,six buttons & set the following properties to them


label1:
     Text      : Enter Any Item
     Name   : lblItem


label2:
     Text     : Enter Index value
     Name   : lblIndex




textbox1:
     Name   : txtItem



textbox2:
     Name   : txtIndex




listBox1:
     Name   : lstSample

   


button1:
     Text      : Add
     Name   : btnAdd

Button2:
     Text      : Insert
     Name   : btnInsert

Button3:
     Text      : Remove
     Name   : btnRemove

Button4:
     Text      : RemoveAt
     Name   : btnRemoveAt

Button5:
     Text      : Clear
     Name   : btnClear

Button6:
     Text      : Close
     Name   : btnClose



Design :-

Generate the EventHandlers  for btnAdd ,btnInsert,btnRemove,btnRemoveAt,btnClear,btnClose buttons Click Events
& write the following code in Source[FormListBox2.cs]


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WABasics
{
    public partial class FormListBox2 : Form
    {
        public FormListBox2()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            lstSample.Items.Add(txtItem.Text);
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl is TextBox)
                {
                    ctrl.Text = "";
                }
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
               lstSample.Items.Insert(Convert.ToInt32(txtIndex.Text),txtItem.Text);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            lstSample.Items.Remove(txtItem.Text);
        }

        private void btnRemoveAt_Click(object sender, EventArgs e)
        {
            lstSample.Items.RemoveAt(Convert.ToInt32(txtIndex.Text));
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            lstSample.Items.Clear();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormListBox2_Load(object sender, EventArgs e)
        {

        }
       
    }
}




Result :-



No comments:

Post a Comment