Getting the mouse_click event from any control OTHER than a specif

G

Guest

I tried using the 'Leave event' on my ListView to deselect the selected
record, but the control only triggers the 'Leave event' when the user clicks
on another interactive control. What I want is when the user clicks the mouse
at ALL, for my program to check what control it clicked on and if its NOT
within my listBox, then the current selected record of ListBox should be
deselected. I tried using the frmMain_MouseDown event, but that only works
when the mouse clicks specifically on the form. I need to use the event
called when the mouse clicks ANYWHERE within that window... on any control.
Here is the code I used...

private void frmMain_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(!sender.Equals(listOne))
this.listOne.FocusedItem.Selected = false;
}


...listOne is my listview control

It works only when I click on the background of my window. What can I do?
 
G

Guest

I believe that you can successfully do this in the Form’s Load event. First
create an event handler to do what you want it to do. For instance:

private void evt_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
// Determine which mouse button was clicked and
// update the lblNote on the form.
switch (e.Button)
{
case MouseButtons.Left:
lblNote.Text = "L";
break;
case MouseButtons.Right:
lblNote.Text = "R";
break;
case MouseButtons.Middle:
lblNote.Text = "M";
break;
default:
break;
}
}
Now add code to the Form’s Load event to dynamically add event handlers to
the controls on the form. You can do this by looping through the Controls
collection.

private void Form1_Load(object sender, System.EventArgs e)
{
//add the event handler for the form...
this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.evt_MouseDown);
//now loop through the Controls collection and add an event handler for
each control
foreach(Control c in this.Controls )
c.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.evt_MouseDown);
}

When you run the application (no matter how many controls you add to the
form) your code will update the text box with the mouse click because the
event handlers are added dynamically each time the application is run. Hope
that this helps!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top