Saturday, 5 October 2013

How to use ListBox Control in Windows Application C# .Net

Add, Clear,Remove with List Box Control in Windows Application………………

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 TestListBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//add new item to ListBox(text from input TextBox) and clear input TextBox...

private void btnAdd_Click(object sender, EventArgs e)
{
if (txtaddSubject.Text != "" && txtaddSubject.Text != null)
{
listBoxSubject.Items.Add(txtaddSubject.Text);
txtaddSubject.Text = String.Empty;
// txtaddSubject.Text = "";
//txtaddSubject.Clear();
}
else
{
MessageBox.Show("Please fill  TextBox Field", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

//code for remove  item in ListBox if one is selected.............

private void btnRemove_Click(object sender, EventArgs e)
{
if (listBoxSubject.SelectedIndex != -1)
listBoxSubject.Items.RemoveAt(listBoxSubject.SelectedIndex);
}

//code for clear all item in ListBox.............

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

//code for exit Application.............

private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}

1 comments: