473,379 Members | 1,255 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Mouse Event, opposite of action Click?

The subject line sounds a little funny, let me quickly explain:

I have created a custom control using ComboBox. But inside my class, I need
to know when the user does NOT click my control.

Of course, I can capture the event Click when someone clicks on the actual
control, but not when they click off of it.

Right now I am using the event handler myCustomControl.LostFocus, but that
requires someone to click on another button or text field before it will fire
that event. I want to be able to have the user be able to click on some
blank space anywhere outside the control and the event to fire. I have also
set up a Click event on my parent control, and that works as long as the user
clicks inside that parent control, but doesn't if they click on the parent of
the parent.

Is there some kind of all encompassing Click event I can capture no matter
where the mouse is clicked in my app?

Does anyone know how to do this? It sounds simple enough, but it is causing
a big headache. Thank you all for reading this.

Rob K
Mar 8 '06 #1
7 3610
Is this for a WebApplication?
Since it is your own custom control, just add an EventHandler called
OnBlurEventHandler, which you will set to fire as ...

string postback = Page.GetPostBackEventReference(this);

protected override void Render(HtmlTextWriter writer)
{
writer.WriteAttribute("onblur", postback);
}

Which will cause your custom control to postback when the user clicks
and changes its value of when the user tabs out of the control.

Sincerely,
Bobby

Mar 8 '06 #2
One possibility would be to override the global windows click message.
This would let you know when the user has clicked ANYWHERE. Then check
if the mouse coordinates of that click are in your application and
respond accordingly. I am not positive what the function is that you
have to override to find out whenever the mouse is clicked, but I do
know it exists. If you're having trouble finding info just reply to
this and I'll see what I can find.

Best of luck ~ Justin

Mar 8 '06 #3
Bobby, Thank you very much for responding. Actually, I am just using C# in a
Windows Application. I wish there was an OnBlur action. Seems like that
would work. But the best I can find is LostFocus.

this.myCustomControl.LostFocus += new
System.EventHandler(this.HideOtherControls);

The user has to actually focus something else before this fires :-(. Ahh,
the quest continues. Do you have any more ideas?

Thanks again,

Rob K

"bo*********@gmail.com" wrote:
Is this for a WebApplication?
Since it is your own custom control, just add an EventHandler called
OnBlurEventHandler, which you will set to fire as ...

string postback = Page.GetPostBackEventReference(this);

protected override void Render(HtmlTextWriter writer)
{
writer.WriteAttribute("onblur", postback);
}

Which will cause your custom control to postback when the user clicks
and changes its value of when the user tabs out of the control.

Sincerely,
Bobby

Mar 8 '06 #4
Hi RobKinney1,

On Wed, 8 Mar 2006 12:31:24 -0800, RobKinney1 wrote:
The user has to actually focus something else before this fires :-(. Ahh,
the quest continues. Do you have any more ideas?


What need to do is intercept all the Windows messages sent to your
application before they are being dispatched to whatever control has the
input focus. You can do that by implementing the IMessageFilter interface
(which has only one method: PreFilterMessage) and adding your message
filter to the application using Application.AddMessageFilter().

In the PreMessageFilter method, intercept the WM_LBUTTONDOWN message, which
indicates that the user has clicked the left mouse button somewhere on your
application's form. You can then get the mouse position in screen
coordinates using the MousePosition property of you user control. That's
you pretty much all done. What you do next is up to you. You could for
example transform the mouse coordinates from screen coordinates to client
coordinates using the PointToClient() method of your user control. Using
these coordinates to determine whether the user has clicked inside or
outside your user control should be a straitforward affair.
Mar 8 '06 #5
Mmm, how about MouseLeave? If the mouse isn't over the Control it certainly
did *not* click it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"RobKinney1" <Ro********@discussions.microsoft.com> wrote in message
news:4F**********************************@microsof t.com...
The subject line sounds a little funny, let me quickly explain:

I have created a custom control using ComboBox. But inside my class, I
need
to know when the user does NOT click my control.

Of course, I can capture the event Click when someone clicks on the actual
control, but not when they click off of it.

Right now I am using the event handler myCustomControl.LostFocus, but that
requires someone to click on another button or text field before it will
fire
that event. I want to be able to have the user be able to click on some
blank space anywhere outside the control and the event to fire. I have
also
set up a Click event on my parent control, and that works as long as the
user
clicks inside that parent control, but doesn't if they click on the parent
of
the parent.

Is there some kind of all encompassing Click event I can capture no matter
where the mouse is clicked in my app?

Does anyone know how to do this? It sounds simple enough, but it is
causing
a big headache. Thank you all for reading this.

Rob K

Mar 9 '06 #6
Well, that worked! Thank you very much for the info. I do enjoy using C#
over C++ for reasons like these. Last time I did system hooking in C++, I
had to make a dll file. But this method here you suggested works great.
Thanks again.

Rob K

"Mehdi" wrote:
Hi RobKinney1,

On Wed, 8 Mar 2006 12:31:24 -0800, RobKinney1 wrote:
The user has to actually focus something else before this fires :-(. Ahh,
the quest continues. Do you have any more ideas?


What need to do is intercept all the Windows messages sent to your
application before they are being dispatched to whatever control has the
input focus. You can do that by implementing the IMessageFilter interface
(which has only one method: PreFilterMessage) and adding your message
filter to the application using Application.AddMessageFilter().

In the PreMessageFilter method, intercept the WM_LBUTTONDOWN message, which
indicates that the user has clicked the left mouse button somewhere on your
application's form. You can then get the mouse position in screen
coordinates using the MousePosition property of you user control. That's
you pretty much all done. What you do next is up to you. You could for
example transform the mouse coordinates from screen coordinates to client
coordinates using the PointToClient() method of your user control. Using
these coordinates to determine whether the user has clicked inside or
outside your user control should be a straitforward affair.

Mar 9 '06 #7
Here is the final solution I used. It was made possible in part by you
people, the people at Experts Exchange, and also by Code Project:

/* File: DropTextBox.cs **** Class: DropTextBox */
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace DropTextBox_NS
{
public class DropTextBox : ComboBox
{

// DLLs for mouse capture, Idle_Mind, Experts-Exchange
[DllImport("user32.dll")]
public static extern int SetCapture(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern IntPtr GetCapture();

[DllImport("user32.dll")]
public static extern int ReleaseCapture();

public TextBox myTextBox;
public Panel myPanel;
public Label myLabel;
private String cannotContain; // keeps track of what
characters cannot be accepted in the text box or drop-down
// * * * * * * * * * * * * CLASS Accessors * * * * * * * * * * * * *
* //
public int WidthOfTextBox
{
get
{
return myTextBox.Width;
}

set
{
myTextBox.Width = value;
myPanel.Width = value;
myLabel.Width = value;
}

} // end WidthOfTextBox

public int HeightOfTextBox
{
get
{
return myTextBox.Height;
}
set
{
myTextBox.Height = value;
}
} // end HeightOfTextBox

public String CannotContainChars
{
get
{
return cannotContain;
}
set
{
cannotContain = value;
}
} // end CannotContainChars

// * * * * * * * * * * * * Constructors * * * * * * * * * * * * * *
* //
public DropTextBox()
{
this.DropDownHeight = 1;

this.cannotContain = "";

myLabel = new Label();
myLabel.Text = "Press TAB to Close This Window";

// must create a text box and place it under the drop-down
myTextBox = new TextBox();
this.myTextBox.Multiline = true;
this.myTextBox.Name = "textBox1";
this.myTextBox.Size = new System.Drawing.Size(120, 43);
this.myTextBox.TabIndex = 2;
myTextBox.Visible = false;

//create instructional panel
this.myPanel = new Panel();
this.myPanel.Name = "APanel";
this.myPanel.Size = new System.Drawing.Size(120, 14);
this.myPanel.Visible = false;
this.myPanel.BorderStyle = BorderStyle.FixedSingle;
this.myPanel.BackColor = System.Drawing.Color.AliceBlue;
this.myPanel.Controls.Add(myLabel);

this.myTextBox.LostFocus += new
System.EventHandler(this.HideTextBox);
this.myTextBox.MouseMove += new
MouseEventHandler(this.TextBoxMouseMove); // experts-exchange
this.myTextBox.MouseDown += new
MouseEventHandler(this.TextBoxMouseDown);

// set up filtering for the text box
this.myTextBox.TextChanged += new
System.EventHandler(this.FilterTextBox);

// set up filtering for the drop-down box
this.TextChanged += new System.EventHandler(this.FilterComboBox);

} // end constructor
// * * * * * * * * * * * * OVERRIDEN event handlers * * * * * * * *
* * * * * * * //
// at this point, we can legaly add the control to the parent form
protected override void CreateHandle()
{
base.CreateHandle();

this.Parent.Controls.Add(this.myTextBox);
this.Parent.Controls.Add(this.myPanel);

} // end CreateHandle ()

// this was the last thing we needed to implement in order for the
box to close correctly
protected override void OnDropDownClosed(EventArgs e)
{
base.OnDropDownClosed(e);

myTextBox.Capture = true;
}

// override the drop down so that it brings up our text box instead
protected override void OnDropDown(EventArgs e)
{
base.OnDropDown(e);

// if the text box is already visible, just hide it
if (myTextBox.Visible)
{
myTextBox.Visible = false;
myPanel.Visible = false;
myTextBox.Capture = false;
}
else // if not, show it
{
this.myTextBox.Text = this.Text;

this.myTextBox.Location = new
System.Drawing.Point(this.Location.X, this.Location.Y + this.Size.Height);
this.myPanel.Location = new
System.Drawing.Point(this.Location.X, this.Location.Y + this.Size.Height +
myTextBox.Height + 1);

myTextBox.Visible = true;
myPanel.Visible = true;
myTextBox.Capture = true;

this.myTextBox.BringToFront(); // ensure that it is visible
to the user
this.myTextBox.Focus(); // bring the cursor down to
the text box
}


} // end OnDropDown ()

// * * * * * * * * * * * * * * This section by Idle_Mind, Experts
Exchange * * * * * * * * * * * * * * * //
private void TextBoxMouseMove(object sender, MouseEventArgs e)
{
myTextBox.Capture = true;
}
private void TextBoxMouseDown(object sender, MouseEventArgs e)
{
if (e.X < 0 || e.X > myTextBox.Width || e.Y < 0 || e.Y >
myTextBox.Height)
{
myTextBox.Capture = false;
myTextBox.Visible = false;
}
}

// * * * * * * * * * * * * Helper Procedures * * * * * * * * * * * *
* * * //

//private void HideTextBox_mouseClick(object sender,
System.EventArgs e)
public void HideTextBox_mouseClick()
{
myTextBox.Visible = false;
myPanel.Visible = false;
this.Text = myTextBox.Text;
myTextBox.Capture = false;
}

// hide the text box from view
private void HideTextBox(object sender, System.EventArgs e)
{
myTextBox.Visible = false;
myPanel.Visible = false;
this.Text = myTextBox.Text;
myTextBox.Capture = false;

} // end HideTextBox ()
// called when the text box is modified so we can filter out
unwanted characters
// Adapted from
http://www.codeproject.com/useritems...sp#xx1333295xx , Hamed JI
12 Jan 2006
private void FilterTextBox(object sender, System.EventArgs e)
{
// if we do have a filter applied, start filtering
if (this.cannotContain.Length > 0)
{
// for each character in the must not contain field
foreach (char character in this.cannotContain.ToCharArray())
{
// if we find a character contained in the forbidden
string, remove it
if (myTextBox.Text.Contains(character.ToString()))
{
myTextBox.Text =
myTextBox.Text.Replace(character.ToString(), ""); // get rid of the offending
character
myTextBox.SelectionStart = myTextBox.Text.Length;
// put the cursor back to where it was
}
} // end FOREACH character in the forbidden string
} // end IF forbidden string

} // end FilterTextBox ()
// called when the comboBox text is modified so we can filter
unwanted characters
private void FilterComboBox(object sender, System.EventArgs e)
{

// if we do have a filter applied, start filtering
if (this.cannotContain.Length > 0)
{
// for each character in the must not contain field
foreach (char character in this.cannotContain.ToCharArray())
{
// if we find a character contained in the forbidden
string, remove it
if (this.Text.Contains(character.ToString()))
{
this.Text = this.Text.Replace(character.ToString(),
""); // get rid of the offending character
this.SelectionStart = this.Text.Length; // put the
cursor back to where it was
}
} // end FOREACH character in the forbidden string
} // end IF forbidden string

} // end FilterComboBox ()
} // end class DropTextBox ()
} // end _NS

I know the indenting probably will not look right after I post this, but you
get the picture. (Experts-Exchange has such a nice way of displaying code
that was pasted into their forms... why not here?)
Mar 10 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Champaign | last post by:
Hi all, I'm working on an educational applet for a child with special needs. He's got a bit of a trick to make my life more difficult... To interact with the applet he needs to click on...
1
by: suhas | last post by:
Hi, I have an MDI app that allows user to create flowcharts. User can drag flowchart objects (squares, rectangles, elipses, etc) from the toolbar and put on a form. They should be able to move...
5
by: Nick | last post by:
Hey guys, I have 2 events on a windows forms datagrid, the mouse move as well as the double click events. What's happening is that when I double click on a row in the grid, the mouse move event...
1
by: scott_gui | last post by:
Creating a Windows application: <Double-Button-1> mouse event has a conflict when there is also a binding to the <Button-1> event. It seems like a silly oversight that performing a double click...
2
by: scott_gui | last post by:
I am creating a Windows application: The mouse event <Double-Button-1> has a conflict when the <Button-1> event also has a binding. Double clicks will first perform the single click action. This...
2
by: Ryan Liu | last post by:
Can someone give a sample to prevent a row from being deleted in a datatable? I tried e.Row.RejectChanges(); in dt_RowDeleting() but seems does not work. I need verify if there other data...
0
by: lechatthierry | last post by:
Is it possible to block a mouse event on an Hyperlink with a general script event? This is quite troublesome for me. I am trying to find a way to block the windows shortcut SHIFT + MOUSE LEFT...
1
by: Neko | last post by:
Is it possible to block a mouse event on an Hyperlink with a general script event? This is quite troublesome for me. I am trying to find a way to block the windows shortcut SHIFT + MOUSE LEFT...
5
by: Adeel | last post by:
Hi group! I'm trying to learn C# on my own... I'd appreciate it very much if someone can help me out here. I have a basic form without any controls on it... I want to catch mouse clicks (both...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.