DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C# (http://www.daniweb.com/forums/forum61.html)
-   -   Code Snippet: Experiments with a ListBox (C#) (http://www.daniweb.com/forums/thread216383.html)

vegaseat Dec 20th, 2004 8:11 pm
Experiments with a ListBox (C#)
 
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.

  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.  
vegaseat Jan 2nd, 2005 12:38 am
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.

bravo659 Nov 23rd, 2009 12:10 am
Hello, I have tried your code and for some reason is not working properly. When I try to select an item in the listbox it suppose to go to the label.
Is there a possible that I may have something wrong with the compiler I am currently using?

I appreciate for your feedback.

Regards,

Desi Bravo

ddanbe Nov 23rd, 2009 3:30 am
After all these years there is nothing wrong with this code. I used Visual Studio C# 2008.

bravo659 Nov 23rd, 2009 9:44 am
Hi David,
Believe it or not I copy and pasted the above code to visual C# compiler and is not firing. I am not saying there is something wrong with the code. I am saying that is not working. I am working on similar project and I coded in the selectedindexchanged for when a user select a name in the listbox it fires to the textbox with the correct information. So I was wondering it may be my visual C# express edition that is not working properly or something. Can you help?
Thanks!

Regards,

Desi Bravo


All times are GMT -4. The time now is 5:55 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC