Example with ComboBox:-



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


label1:
     Text      : Enter AnyItem
     Name   : lblItem


label2:
     Text     : Enter Any index
     Name   : lblIndex


label3:
     Text     : Select Any Item
     Name   : lblSelectItem



textbox1:
     Name   : txtItem



textbox2:
     Name   : txtIndex



comboBox1:
     Name         : cmbItems
    


button1:
     Name   : btnAdd
     Text     : Add

button2:
     Name   : btnInsertSubtract
     Text     : Insert

button3:
     Name   : btnRemove
     Text     : Remove

button4:
     Name   : btnRemoveAt
     Text     : RemoveAt

Button5:
     Name   : btnClear
     Text     : Clear


Button6:
     Name   : btnClose
     Text     : Close




Design :-



Generate the EventHandlers  for btnAdd ,btnInsert,btnRemove,btnRemoveAt,btnClear,btnClose buttons clickEvents
Generate the Event Handler for cmbItems Default Event i.e.,SelectedIndexChanged
& write the following code in Source[FormComboBox1.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 FormComboBox1 : Form
    {
        public FormComboBox1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            cmbSample.Items.Add(txtItem.Text);
        }

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

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

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

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

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

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




Result :-

Add

Insert

No comments:

Post a Comment