Description
Tools: Windows 2000 & VS.NET SDK Beta 2.0
The source code demonstrates how to use a listbox control in C# - adding, deleting and altering strings. The source code is self-explanatory. To compile the code, install .NET SDK including C# compiler and use the following commands on MS-DOS prompt:
>csc ListBox.cs
>ListBox.exe
ListBoxDA001.gif
The source code listed in the following table.
  1. namespace test
  2. {
  3. using System;
  4. using System.Drawing;
  5. using System.Collections;
  6. using System.ComponentModel;
  7. using System.Windows.Forms;
  8. using System.Data;
  9. public class ListBox : System.Windows.Forms.Form
  10. {
  11. private System.ComponentModel.Container container;
  12. private System.Windows.Forms.Button buttonAdd;
  13. private System.Windows.Forms.Button buttonClose;
  14. private System.Windows.Forms.Button buttonModify;
  15. private System.Windows.Forms.Button buttonDelete;
  16. private System.Windows.Forms.Button buttonMoveUp;
  17. private System.Windows.Forms.Button buttonMoveDown;
  18. private System.Windows.Forms.ListBox listbox;
  19. private System.Windows.Forms.TextBox textbox;
  20. private System.Windows.Forms.Label label;
  21. private int nSelectedIndex;
  22. //*********SIZE & LOCATION******************//
  23. // COMPONENT - BUTTON(s) aligned along X-axis.
  24. const int BUTTON_LENGTH = 50;
  25. const int BUTTON_HEIGHT = 20;
  26. const int FIRSTBUTTON_XPOS = 20;
  27. const int FIRSTBUTTON_YPOS =220;
  28. const int XSPACING = 70; // (Note: XSPACING >= BUTTON_LENGTH)
  29. const int YSPACING = 0;
  30. //COMPONENT - MOVE BUTTONS
  31. const int MBUTTON_LENGTH = 20;
  32. const int MBUTTON_HEIGHT = 20;
  33. const int FIRSTMBUTTON_XPOS = 220;
  34. const int FIRSTMBUTTON_YPOS =70;
  35. const int SECONDMBUTTON_XPOS = 220;
  36. const int SECONDMBUTTON_YPOS =100;
  37. // COMPONENT - LISTBOX
  38. const int LISTBOX_LENGTH = 3*BUTTON_LENGTH;
  39. const int LISTBOX_HEIGHT = 6*BUTTON_HEIGHT;
  40. const int LISTBOX_XPOS = 50;
  41. const int LISTBOX_YPOS = 50;
  42. // COMPONENT - LABEL
  43. const int LABEL_LENGTH = 50;
  44. const int LABEL_HEIGHT = 50;
  45. const int LABEL_XPOS = 20; // align it with first button
  46. const int LABEL_YPOS = 173;
  47. // COMPONENT - TEXTBOX
  48. const int TEXTBOX_LENGTH = 120;
  49. const int TEXTBOX_HEIGHT = 50;
  50. const int TEXTBOX_XPOS = 70;
  51. const int TEXTBOX_YPOS = 170;
  52. public ListBox() : base()
  53. {
  54. InitializeComponent();
  55. }
  56. public override void Dispose()
  57. {
  58. base.Dispose();
  59. container.Dispose();
  60. }
  61. private void InitializeComponent()
  62. {
  63. // this
  64. this.container = new System.ComponentModel.Container();
  65. this.Text="List Box";
  66. // buttonAdd
  67. this.buttonAdd = new System.Windows.Forms.Button();
  68. buttonAdd.Location = new System.Drawing.Point(FIRSTBUTTON_XPOS,FIRSTBUTTON_YPOS);
  69. buttonAdd.Text = "&Add";
  70. buttonAdd.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
  71. buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click); buttonAdd.Enabled = false;
  72. this.Controls.Add(this.buttonAdd);
  73. //buttonModify
  74. this.buttonModify = new System.Windows.Forms.Button();
  75. buttonModify.Location = new System.Drawing.Point(FIRSTBUTTON_XPOS+XSPACING,FIRSTBUTTON_YPOS+YSPACING);
  76. buttonModify.Text = "&Modify";
  77. buttonModify.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
  78. buttonModify.Click += new System.EventHandler(this.buttonModify_Click); buttonModify.Enabled = false;
  79. this.Controls.Add(this.buttonModify);
  80. //buttonDelete
  81. this.buttonDelete = new System.Windows.Forms.Button();
  82. buttonDelete.Location = new System.Drawing.Point(FIRSTBUTTON_XPOS+2*XSPACING,FIRSTBUTTON_YPOS+2*YSPACING); buttonDelete.Text = "&Delete";
  83. buttonDelete.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
  84. buttonDelete.Enabled = false;
  85. buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click);
  86. this.Controls.Add(this.buttonDelete);
  87. // buttonClose
  88. this.buttonClose = new System.Windows.Forms.Button();
  89. buttonClose.Location = new System.Drawing.Point(FIRSTBUTTON_XPOS+3*XSPACING,FIRSTBUTTON_YPOS+3*YSPACING); buttonClose.Text = "&Close";
  90. buttonClose.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
  91. buttonClose.Click += new System.EventHandler(this.buttonClose_Click);
  92. this.Controls.Add(this.buttonClose);
  93. // listbox
  94. this.listbox = new System.Windows.Forms.ListBox();
  95. listbox.Location = new System.Drawing.Point(LISTBOX_XPOS,LISTBOX_YPOS);
  96. listbox.Size = new System.Drawing.Size(LISTBOX_LENGTH,LISTBOX_HEIGHT);
  97. listbox.Click += new System.EventHandler(this.listbox_SelectedIndexChanged);
  98. listbox.BackColor = (Color)System.Drawing.SystemColors.Desktop;
  99. this.Controls.Add(this.listbox);
  100. // label
  101. this.label = new System.Windows.Forms.Label();
  102. label.Location = new System.Drawing.Point(LABEL_XPOS,LABEL_YPOS);
  103. label.Size = new System.Drawing.Size(LABEL_LENGTH,LABEL_HEIGHT);
  104. label.Text = "Enter:";
  105. this.Controls.Add(this.label);
  106. // textbox
  107. this.textbox = new System.Windows.Forms.TextBox();
  108. textbox.Location = new System.Drawing.Point(TEXTBOX_XPOS,TEXTBOX_YPOS); textbox.Click += new System.EventHandler(this.textbox_Click);
  109. textbox.Size = new System.Drawing.Size(TEXTBOX_LENGTH,TEXTBOX_HEIGHT);
  110. this.Controls.Add(this.textbox);
  111. // buttonMoveUp
  112. this.buttonMoveUp = new System.Windows.Forms.Button();
  113. buttonMoveUp.Location = new System.Drawing.Point(FIRSTMBUTTON_XPOS,FIRSTMBUTTON_YPOS);
  114. buttonMoveUp.Text = "<";
  115. buttonMoveUp.Size = new System.Drawing.Size(MBUTTON_LENGTH,MBUTTON_HEIGHT);
  116. buttonMoveUp.Click += new System.EventHandler(this.buttonMoveUp_Click);
  117. buttonMoveUp.Enabled = false;
  118. this.Controls.Add(this.buttonMoveUp);
  119. // buttonMoveDown
  120. this.buttonMoveDown = new System.Windows.Forms.Button();
  121. buttonMoveDown.Location = new System.Drawing.Point(SECONDMBUTTON_XPOS,SECONDMBUTTON_YPOS);
  122. buttonMoveDown.Text = ">";
  123. buttonMoveDown.Size = new System.Drawing.Size(MBUTTON_LENGTH,MBUTTON_HEIGHT);
  124. buttonMoveDown.Click += new System.EventHandler(this.buttonMoveDown_Click);
  125. buttonMoveDown.Enabled = false;
  126. this.Controls.Add(this.buttonMoveDown);
  127. }
  128. protected void textbox_Click(Object sender, System.EventArgs e)
  129. {
  130. this.buttonAdd.Enabled = true;
  131. if (this.listbox.Items.Count>0)
  132. EnableAllListBoxButtons();
  133. }
  134. protected void listbox_SelectedIndexChanged(object sender, System.EventArgs e)
  135. {
  136. nSelectedIndex = this.listbox.SelectedIndex;
  137. string szSelected = (string)this.listbox.SelectedItem;
  138. this.textbox.Text = szSelected;
  139. }
  140. protected void buttonAdd_Click(Object sender, System.EventArgs e)
  141. {
  142. if (this.textbox.Text !="")
  143. {
  144. this.listbox.Items.Add(this.textbox.Text);
  145. this.textbox.Text = "";
  146. EnableAllListBoxButtons();
  147. }
  148. }
  149. protected void buttonModify_Click(Object sender, System.EventArgs e)
  150. {
  151. this.listbox.Items[nSelectedIndex] = this.textbox.Text;
  152. }
  153. protected void buttonDelete_Click(Object sender, System.EventArgs e)
  154. {
  155. nSelectedIndex = this.listbox.SelectedIndex;
  156. this.listbox.Items.Remove(nSelectedIndex);
  157. System.Console.WriteLine("Remove fn does not work...");
  158. }
  159. protected void buttonClose_Click(Object sender, System.EventArgs e)
  160. {
  161. this.Close();
  162. }
  163. protected void buttonMoveUp_Click(Object sender, System.EventArgs e)
  164. {
  165. if (this.listbox.SelectedIndex >0)
  166. this.listbox.SelectedIndex--;
  167. }
  168. protected void buttonMoveDown_Click(Object sender, System.EventArgs e)
  169. {
  170. if (this.listbox.SelectedIndex < this.listbox.Items.Count-1)
  171. this.listbox.SelectedIndex++;
  172. }
  173. private void EnableAllListBoxButtons()
  174. {
  175. this.buttonAdd.Enabled = true;
  176. this.buttonModify.Enabled = true;
  177. this.buttonDelete.Enabled = true;
  178. this.buttonMoveUp.Enabled = true;
  179. this.buttonMoveDown.Enabled = true;
  180. }
  181. [STAThread]
  182. public static void Main(string[] args)
  183. {
  184. Application.Run(new ListBox());
  185. }
  186. } // class
  187. } // namespace
Further Readings
Here are more articles on ListBox control.