944,139 Members | Top Members by Rank

Ad:
  • C# Code Snippet
  • Views: 125780
  • C# RSS
0

Experiments with a ListBox (C#)

by on Dec 20th, 2004
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.
C# Code Snippet (Toggle Plain Text)
  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.  
Comments on this Code Snippet
Jan 2nd, 2005
0

Re: Experiments with a ListBox (C#)

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.
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 23rd, 2009
0

Re: Experiments with a ListBox (C#)

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
Last edited by adatapost; Nov 23rd, 2009 at 12:12 am. Reason: Email Snipped. Emails, fake signatures, and personal information will be snipped out of offending post.
Light Poster
bravo659 is offline Offline
27 posts
since May 2005
Nov 23rd, 2009
0

Re: Experiments with a ListBox (C#)

After all these years there is nothing wrong with this code. I used Visual Studio C# 2008.
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Nov 23rd, 2009
0

Re: Experiments with a ListBox (C#)

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
Light Poster
bravo659 is offline Offline
27 posts
since May 2005
Apr 9th, 2010
0

suggestion

I would just like to point something out, I hope you don't take any offense. There is a quicker way to add items to the listbox.

C# Syntax (Toggle Plain Text)
  1. String[] items = { "item1", "second item", "another item", "and another..." };
  2.  
  3. private void LoadLBbutton_Click(object sender, System.EventArgs e)
  4. {
  5. foreach (String item in items)
  6. listBox1.Items.Add(item);
  7.  
  8. label1.Text = "Select an item...";
  9. }
Last edited by baeltazor; Apr 9th, 2010 at 7:05 am.
Newbie Poster
baeltazor is offline Offline
1 posts
since Apr 2010
Apr 9th, 2010
1

Re: Experiments with a ListBox (C#)

baeltazor, I see you are accustom to the .net baby sitter as much as I am. . C and C++ people don't tend to use foreach loops often when the migrate to .net because its only possible in programming languages that use virtual machines like .net's CLR. But it actually adds an overhead, slowing down the execution of the code. Plus your version of the code requires creating an addition object, that takes time to initialize, takes more system resource, and twice the memory.

Not that its a bad way to do it. computers these days are plenty fast and for each loops and they are very fun and easy to use.

But even though its less typing, technically the OP's way of doing it was "faster" by terms of the program execution.
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008
Dec 20th, 2010
0

Re: Experiments with a ListBox (C#)

Newbie Poster
vinay53 is offline Offline
1 posts
since Dec 2010
Mar 5th, 2011
0

Thanks

Thank U very much
Newbie Poster
anucom is offline Offline
9 posts
since Feb 2011
Message:
Previous Thread in C# Forum Timeline: help inputting 2-dimensional array data by keyboard
Next Thread in C# Forum Timeline: Quicksort with 1 parameter?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC