C# Source Codes: List Box

Sunday, January 10, 2010

List Box








Source Code : Form1.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 List_Box
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Add add = new Add();
add.ShowDialog();
if (add.DialogResult == DialogResult.OK)
{
listBox1.Items.Add(add.textBox1.Text);
}
}

private void button2_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == -1)
{
MessageBox.Show("Please select an item first to remove !", "No item selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
}
}




public partial class Add : Form
{
public Add()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;
this.button1.Location = new System.Drawing.Point(111, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(51, 20);
this.button1.TabIndex = 0;
this.button1.Text = "Add";
this.button1.UseVisualStyleBackColor = true;

//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(97, 20);
this.textBox1.TabIndex = 1;
//
// Add Form
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(171, 39);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "Add";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Enter Your Text";
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Button button1;
public System.Windows.Forms.TextBox textBox1;
}



}




Source Code : Form1.Designer.cs

namespace List_Box
{
partial class Form1
{
///
/// Required designer variable.
///

private System.ComponentModel.IContainer components = null;

///
/// Clean up any resources being used.
///

/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.listBox1);
this.groupBox1.Location = new System.Drawing.Point(10, 12);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(288, 248);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "My List Box";
//
// button2
//
this.button2.Location = new System.Drawing.Point(129, 197);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(102, 38);
this.button2.TabIndex = 2;
this.button2.Text = "Remove";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 197);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(102, 38);
this.button1.TabIndex = 1;
this.button1.Text = "Add";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(12, 35);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(267, 147);
this.listBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.ClientSize = new System.Drawing.Size(309, 272);
this.Controls.Add(this.groupBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = " List Box";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
}
}





Download :

exe
http://sites.google.com/site/ogremyfiles/ListBox_EXE.rar
Source Code
http://sites.google.com/site/ogremyfiles/ListBox_SourceCode.rar



Designed by : Ogre