//--------------------------------------------- // Example program to test mouse buttons in listboxes // Bill Nolde 2003 // billnolde@ieee.org //------------------------------- using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices ; namespace MouseButtonEvents { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { [DllImport("user32.dll")] public static extern bool MessageBeep(int Sound); [DllImport("user32.dll")] public static extern int MessageBox(int hWnd, String text, String caption, uint type); public int i = 0; public int j = 0; public bool up=false; public bool down = false; private System.Windows.Forms.ListBox m_listbox; private System.Windows.Forms.TextBox m_mouse_event; private System.Windows.Forms.Button OKButton; private System.Windows.Forms.Button AboutButton; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.ComponentModel.IContainer components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.OKButton = new System.Windows.Forms.Button(); this.m_listbox = new System.Windows.Forms.ListBox(); this.m_mouse_event = new System.Windows.Forms.TextBox(); this.AboutButton = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // OKButton // this.OKButton.BackColor = System.Drawing.SystemColors.Menu; this.OKButton.Location = new System.Drawing.Point(427, 7); this.OKButton.Name = "OKButton"; this.OKButton.Size = new System.Drawing.Size(62, 20); this.OKButton.TabIndex = 0; this.OKButton.Text = "OK"; this.OKButton.UseVisualStyleBackColor = false; this.OKButton.Click += new System.EventHandler(this.OKButtonClick); // // m_listbox // this.m_listbox.Items.AddRange(new object[] { "111111111111111111", "222222222222222222", "333333333333333333", "444444444444444444", "555555555555555555"}); this.m_listbox.Location = new System.Drawing.Point(20, 83); this.m_listbox.Name = "m_listbox"; this.m_listbox.Size = new System.Drawing.Size(273, 160); this.m_listbox.TabIndex = 1; this.m_listbox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ButtonUp); this.m_listbox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ButtonDown); // // m_mouse_event // this.m_mouse_event.Location = new System.Drawing.Point(340, 90); this.m_mouse_event.Multiline = true; this.m_mouse_event.Name = "m_mouse_event"; this.m_mouse_event.Size = new System.Drawing.Size(133, 160); this.m_mouse_event.TabIndex = 2; this.m_mouse_event.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // // AboutButton // this.AboutButton.ForeColor = System.Drawing.SystemColors.HotTrack; this.AboutButton.Location = new System.Drawing.Point(427, 42); this.AboutButton.Name = "AboutButton"; this.AboutButton.Size = new System.Drawing.Size(62, 20); this.AboutButton.TabIndex = 3; this.AboutButton.Text = "About"; this.AboutButton.Click += new System.EventHandler(this.AboutButtonClick); // // label2 // this.label2.Location = new System.Drawing.Point(113, 55); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(54, 20); this.label2.TabIndex = 6; this.label2.Text = "ListBox"; // // label3 // this.label3.Location = new System.Drawing.Point(380, 62); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(83, 20); this.label3.TabIndex = 7; this.label3.Text = "output"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(632, 359); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.AboutButton); this.Controls.Add(this.m_mouse_event); this.Controls.Add(this.m_listbox); this.Controls.Add(this.OKButton); this.Name = "Form1"; this.Text = "Moue Button Events Bill Nolde 2003"; this.ResumeLayout(false); this.PerformLayout(); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } //--------------------------------------------------------------------- private void OKButtonClick(object sender, System.EventArgs e) { Application.Exit(); } //--------------------------------------------------------------------- private void ButtonDown(object sender, MouseEventArgs mea) { int nDx = 0; //This will be the 0 based index of the row clicked on if (mea.Button == MouseButtons.Right) { //Check for right button click MessageBeep(256); i++; up = false; m_mouse_event.Text = "Bla Bla!\r\n"+"i = "+i.ToString(); } else if (mea.Button == MouseButtons.Left) { //Check for Left button click MessageBeep(256); j++; up = false; m_mouse_event.Text = "BLA BLA!\r\n"+"j = "+j.ToString(); } nDx = mea.Y / m_listbox.ItemHeight; //Get Index of item selectem /* m_mouse_event.Text = m_mouse_event.Text + "Item height = " + m_listbox.ItemHeight.ToString()+ "\r\n" + "X position = " + mea.X.ToString() + "\r\n" + "Y position = " + mea.Y.ToString() + "\n" +"\r\n"+ "Index = " + nDx.ToString(); */ if ( m_listbox.Items.Count <= nDx ) m_mouse_event.Text =// m_mouse_event.Text + "\r\n" + "Yes, Indeed\r\n"; else m_listbox.SetSelected(nDx,true); //highlight row selected } //------------------------------------------------------------------- private void ButtonUp(object sender, MouseEventArgs mea) { up = true; } //------------------------------------------------------------------- private void AboutButtonClick(object sender, System.EventArgs e) { MessageBox(0,"Mouse Button Example in C#" + "\n for the .NET Framework" + "\n Bill Nolde" + "\n billnolde@ieee.org","",0); } //------------------------------------------------------------------- } }