944,079 Members | Top Members by Rank

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

Put your MouseDown in a ListBox (C#)

by on Feb 8th, 2005
Many moons ago I wrote a Delphi program that could tell the exact character in a ListBox, as you clicked on it with a mouse. I found a little info on the NET that lets you get at least the item in a ListBox using C#. I made it work and still need to expand it to the character level. Looks interesting anyway and could be used for other components.
C# Code Snippet (Toggle Plain Text)
  1. // respond to a MouseDown event inside a ListBox
  2. // shows item in the ItemHeight-range of mouse y-position
  3. // with the proper font this could be expanded to show character at mouse x-position
  4. //
  5. // MS Visual C# .NET by vegaseat 06feb2005
  6. //
  7. // this is a Windows Application
  8.  
  9. using System;
  10. using System.Windows.Forms;
  11.  
  12. namespace ListBoxMouse1
  13. {
  14. public class MainForm : System.Windows.Forms.Form
  15. {
  16. private System.Windows.Forms.Label label1;
  17. private System.Windows.Forms.ListBox listBox1;
  18.  
  19. public MainForm()
  20. {
  21. InitializeComponent();
  22. // now set this particular event handler to function ButtonDown()
  23. this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler( this.ButtonDown );
  24. }
  25.  
  26. // program entry point
  27. public static void Main(string[] args)
  28. {
  29. Application.Run(new MainForm());
  30. }
  31.  
  32. #region Windows Forms Designer generated code
  33. private void InitializeComponent()
  34. {
  35. this.listBox1 = new System.Windows.Forms.ListBox();
  36. this.label1 = new System.Windows.Forms.Label();
  37. this.SuspendLayout();
  38. //
  39. // listBox1
  40. //
  41. this.listBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192)));
  42. this.listBox1.ItemHeight = 16;
  43. this.listBox1.Location = new System.Drawing.Point(8, 16);
  44. this.listBox1.Name = "listBox1";
  45. this.listBox1.Size = new System.Drawing.Size(240, 196);
  46. this.listBox1.TabIndex = 0;
  47. //
  48. // label1
  49. //
  50. this.label1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
  51. this.label1.Location = new System.Drawing.Point(16, 224);
  52. this.label1.Name = "label1";
  53. this.label1.Size = new System.Drawing.Size(232, 112);
  54. this.label1.TabIndex = 1;
  55. this.label1.Text = "label1";
  56. //
  57. // MainForm
  58. //
  59. this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
  60. this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(0)));
  61. this.ClientSize = new System.Drawing.Size(320, 344);
  62. this.Controls.Add(this.label1);
  63. this.Controls.Add(this.listBox1);
  64. this.Name = "MainForm";
  65. this.Text = "MouseDown in a ListBox";
  66. this.Load += new System.EventHandler(this.MainFormLoad);
  67. this.ResumeLayout(false);
  68. }
  69. #endregion
  70.  
  71. // respond to mouse events in the listBox
  72. private void ButtonDown(object sender, MouseEventArgs mea)
  73. {
  74. // the 0 based index of the row/item clicked
  75. int nDx = 0;
  76.  
  77. if ( mea.Button == MouseButtons.Right )
  78. {
  79. label1.Text = "Right Button Clicked\n";
  80. }
  81. else if ( mea.Button == MouseButtons.Left )
  82. {
  83. label1.Text = "Left Button Clicked\n";
  84. }
  85.  
  86. // calculate the index of the selected item or show error
  87. nDx = mea.Y / listBox1.ItemHeight;
  88. label1.Text = label1.Text +
  89. "Item height = " + listBox1.ItemHeight.ToString()+ "\n" +
  90. "X position = " + mea.X.ToString() + "\n" +
  91. "Y position = " + mea.Y.ToString() + "\n" +
  92. "Index = " + nDx.ToString();
  93.  
  94. if ( listBox1.Items.Count <= nDx )
  95. {
  96. label1.Text = label1.Text + "\nRow clicked past items loaded";
  97. }
  98. else
  99. {
  100. label1.Text = label1.Text + "\nIndex = " + listBox1.Items[nDx];
  101. //highlight the selected row/item
  102. listBox1.SetSelected(nDx,true);
  103. }
  104. }
  105.  
  106. // load the listBox with some names
  107. void MainFormLoad(object sender, System.EventArgs e)
  108. {
  109. listBox1.Items.Add("Helmut");
  110. listBox1.Items.Add("Helga");
  111. listBox1.Items.Add("Andreas");
  112. listBox1.Items.Add("Volger");
  113. listBox1.Items.Add("Kurt");
  114. listBox1.Items.Add("Erich");
  115. listBox1.Items.Add("Bjorn");
  116. listBox1.Items.Add("Lena");
  117. listBox1.Items.Add("Kristina");
  118. listBox1.Items.Add("Ulrike");
  119. listBox1.Items.Add("Heidrun");
  120. }
  121. }
  122. }
  123.  
Message:
Previous Thread in C# Forum Timeline: getting bad token ( on second compile
Next Thread in C# Forum Timeline: Username / password using C#





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


Follow us on Twitter


© 2011 DaniWeb® LLC