Experiments with a ListBox (C#)

Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
vegaseat vegaseat is offline Offline Dec 20th, 2004, 8:11 pm |
0
I am afraid, I am starting to like C#, despite the somewhat bloated .Net Framework requirements. Mister Bill's Microsoft is very supportive though. The language has a nice flow compared to GUI programming in C++. Here we are looking at a standard ListBox, add some items, sort them and select them.
Quick reply to this message  
C# Syntax
  1. /*
  2.  * Created with SharpDevelop free C# system from
  3.  * http://www.icsharpcode.net/opensource/sd/
  4.  * User: vegaseat
  5.  *
  6.  * Create a ListBox, then add, sort, select items
  7.  * A Windows Application
  8.  */
  9.  
  10. using System;
  11. using System.Drawing;
  12. using System.Collections;
  13. using System.ComponentModel;
  14. using System.Windows.Forms;
  15. using System.Data;
  16.  
  17. namespace ListBox1
  18. {
  19. // Summary description for Form1
  20. // so we got a form (window) with a label, 2 buttons and a listbox ...
  21. public class Form1 : System.Windows.Forms.Form
  22. {
  23. private System.Windows.Forms.Label label1;
  24. private System.Windows.Forms.Button sortLBbutton;
  25. private System.Windows.Forms.Button LoadLBbutton;
  26. private System.Windows.Forms.ListBox listBox1;
  27. // Required designer variable
  28. private System.ComponentModel.Container components = null;
  29.  
  30. // time to build the form and it's components ...
  31. public Form1()
  32. {
  33. InitializeComponent();
  34. }
  35.  
  36. // clean up any resources being used ...
  37. protected override void Dispose( bool disposing )
  38. {
  39. if (disposing)
  40. {
  41. if (components != null)
  42. {
  43. components.Dispose();
  44. }
  45. }
  46. base.Dispose(disposing);
  47. }
  48.  
  49. // all the components in detail ...
  50. private void InitializeComponent() {
  51. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
  52. this.listBox1 = new System.Windows.Forms.ListBox();
  53. this.LoadLBbutton = new System.Windows.Forms.Button();
  54. this.sortLBbutton = new System.Windows.Forms.Button();
  55. this.label1 = new System.Windows.Forms.Label();
  56. this.SuspendLayout();
  57. //
  58. // listBox1
  59. //
  60. this.listBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192)));
  61. this.listBox1.ItemHeight = 16;
  62. this.listBox1.Location = new System.Drawing.Point(8, 8);
  63. this.listBox1.Name = "listBox1";
  64. this.listBox1.Size = new System.Drawing.Size(168, 244);
  65. this.listBox1.TabIndex = 0;
  66. this.listBox1.SelectedIndexChanged += new System.EventHandler(this.ListBox1SelectedIndexChanged);
  67. //
  68. // LoadLBbutton
  69. //
  70. this.LoadLBbutton.Location = new System.Drawing.Point(200, 16);
  71. this.LoadLBbutton.Name = "LoadLBbutton";
  72. this.LoadLBbutton.Size = new System.Drawing.Size(128, 23);
  73. this.LoadLBbutton.TabIndex = 1;
  74. this.LoadLBbutton.Text = "Load ListBox";
  75. this.LoadLBbutton.Click += new System.EventHandler(this.LoadLBbutton_Click);
  76. //
  77. // sortLBbutton
  78. //
  79. this.sortLBbutton.Location = new System.Drawing.Point(200, 56);
  80. this.sortLBbutton.Name = "sortLBbutton";
  81. this.sortLBbutton.Size = new System.Drawing.Size(128, 23);
  82. this.sortLBbutton.TabIndex = 3;
  83. this.sortLBbutton.Text = "Sort the ListBox";
  84. this.sortLBbutton.Click += new System.EventHandler(this.sortLBbuttonClick);
  85. //
  86. // label1
  87. //
  88. this.label1.Location = new System.Drawing.Point(8, 264);
  89. this.label1.Name = "label1";
  90. this.label1.Size = new System.Drawing.Size(160, 24);
  91. this.label1.TabIndex = 2;
  92. this.label1.Text = "---";
  93. //
  94. // Form1
  95. //
  96. this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
  97. this.BackColor = System.Drawing.Color.WhiteSmoke;
  98. this.ClientSize = new System.Drawing.Size(344, 296);
  99. this.Controls.Add(this.sortLBbutton);
  100. this.Controls.Add(this.label1);
  101. this.Controls.Add(this.LoadLBbutton);
  102. this.Controls.Add(this.listBox1);
  103. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  104. this.Name = "Form1";
  105. this.Text = "Load a ListBox and sort ...";
  106. this.ResumeLayout(false);
  107. }
  108.  
  109. //
  110. // The main entry point for the application
  111. //
  112. static void Main()
  113. {
  114. Application.Run(new Form1());
  115. }
  116.  
  117. //
  118. // the events, or let's do something with the components ...
  119. //
  120.  
  121. // load some data into the ListBox
  122. private void LoadLBbutton_Click(object sender, System.EventArgs e)
  123. {
  124. listBox1.Items.Add("Helmut");
  125. listBox1.Items.Add("Helga");
  126. listBox1.Items.Add("Andreas");
  127. listBox1.Items.Add("Volger");
  128. listBox1.Items.Add("Kurt");
  129. listBox1.Items.Add("Erich");
  130. listBox1.Items.Add("Bjorn");
  131. listBox1.Items.Add("Lena");
  132. listBox1.Items.Add("Kristina");
  133. label1.Text = "Select an item ...";
  134. }
  135.  
  136. // selected ListBox item is transferred to label1
  137. void ListBox1SelectedIndexChanged(object sender, System.EventArgs e)
  138. {
  139. label1.Text = listBox1.SelectedItems[0].ToString();
  140. }
  141.  
  142. // sort the items of the ListBox
  143. void sortLBbuttonClick(object sender, System.EventArgs e)
  144. {
  145. listBox1.Sorted = true;
  146. }
  147.  
  148. }
  149. }
  150.  
0
vegaseat vegaseat is offline Offline | Jan 2nd, 2005
Just a note: What you get from SharpDevelop is actually the IDE written in C# that uses the compiler from the .NET Framework Version 1.1 Redistributable Package called dotnetfx.exe from Microsoft. The IDE works very much like Visual C# or VB. For this program the Form Builder within the IDE generates most of the center portion of the code.
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC