Is there any way to capture the MouseDown even from the .NET 2.0 TextBox control?
I know the inherited Control class has the event, but it's not exposed in TextBox.
Is there a way to override the event handler?
I also tried the OpenNETCF TextBox2 control which does have the MouseDown event exposed, but no matter what I do, it doesn't fire the handler.
Any suggestions?
What kind of crazy mobile device do
you have that has a mouse? :)
Yes, windows mobile does not have an actual mouse, but you are mistaken that Windows Mobile .NET do not support the Mouse events. A click or move on the screen is still considered a "Mouse" event. It was done this way so that code could port over from full Windows easily. And this is not a Windows Mobile specific issue. The TextBox control on Windows does not have native mouse events either. I just happened to be using Windows Mobile in this case.
Edit: And on a side note...as Windows Mobile is built of the WindowsCE core which is often used for embedded desktop systems and Slim Terminal Services clients or "WinTerms" it has support for a hardware mouse and has for a long time. Most devices just don't have the ports to plug one in.
According to the .Net Framework, the
MouseDown Event Handler on a TextBox
is supported. What happens when you
try to run the code?
Actually, that's only there because it inherits it from "Control", as does every other Form control. It is, however, overridden (and changed to private I believe) in the TextBox class. So it will not show up in IntelliSense in Visual Studio.
However, you actually can write the code:
textBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseDown);
and it will compile and run just fine, the only problem is that textBox1_MouseDown() will not be fired when you tap the TextBox control. I assume this is because of the Event being overridden internally. I don't even want to change what's happening on the event internally, I just want to add my own event handler to that event so I can fire some custom code as you could with any other event.
I know this answer is way late, but hopefully it ends up being useful for someone who finds this. Also, I didn't entirely come up with it myself. I believe I originally found most of the info on the OpenNETCF boards, but what is typed below is extracted from one of my applications.
You can get a mousedown event by implementing the OpenNETCF.Windows.Forms.IMessageFilter interface and attaching it to your application's message filter.
static class Program {
public static MouseUpDownFilter mudFilter = new MouseUpDownfilter();
public static void Main() {
Application2.AddMessageFilter(mudFilter);
Application2.Run(new MainForm());
}
}
This is how you could implement the MouseUpDownFilter:
public class MouseUpDownFilter : IMessageFilter {
List ControlList = new List();
public void WatchControl(Control buttonToWatch) {
ControlList.Add(buttonToWatch);
}
public event MouseEventHandler MouseUp;
public event MouseEventHandler MouseDown;
public bool PreFilterMessage(ref Microsoft.WindowsCE.Forms.Message m) {
const int WM_LBUTTONDOWN = 0x0201;
const int WM_LBUTTONUP = 0x0202;
// If the message code isn't one of the ones we're interested in
// then we can stop here
if (m.Msg != WM_LBUTTONDOWN && m.Msg != WM_LBUTTONDOWN) {
return false;
}
// see if the control is a watched button
foreach (Control c in ControlList) {
if (m.HWnd == c.Handle) {
int i = (int)m.LParam;
int x = i & 0xFFFF;
int y = (i >> 16) & 0xFFFF;
MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 1, x, y, 0);
if (m.Msg == WM_LBUTTONDOWN)
MouseDown(c, args);
else
MouseUp(c, args);
// returning true means we've processed this message
return true;
}
}
return false;
}
}
Now this MouseUpDownFilter will fire an MouseUp/MouseDown event when they occur on a watched control, for example your textbox. To use this filter you add some watched controls and assign to the events it might fire in your form's load event:
private void MainForm_Load(object sender, EventArgs e) {
Program.mudFilter.WatchControl(this.textBox1);
Program.mudFilter.MouseDown += new MouseEventHandler(mudFilter_MouseDown);
Program.mudFilter.MouseUp += new MouseEventHandler(mudFilter_MouseUp);
}
void mudFilter_MouseDown(object sender, MouseEventArgs e) {
if (sender == textBox1) {
// do what you want to do in the textBox1 mouse down event :)
}
}
Looks like you're right. Bummer. No MouseOver event.
One of the fallbacks that always works with .NET, though, is P/Invoke. Someone already took the time to do this for the .NET CF TextBox. I found this on CodeProject:
http://www.codeproject.com/KB/cs/TextBox_subclassing.aspx
Hope this helps
Fair enough. You probably know more than I do about Windows Mobile. :) I just started programming for it. But in regular WinForms, you can override the OnXxx event handler methods all you want. A quick look in Reflector with the CF shows that Control, TextBoxBase and TextBox don't prevent you from overriding the OnMouseDown event handler.
Have you tried this?:
public class MyTextBox : TextBox
{
public MyTextBox()
{
}
protected override void OnMouseDown(MouseEventArgs e)
{
//do something specific here
base.OnMouseDown(e);
}
}
is there an 'OnEnter' event that you could capture instead?
it'd presumably also capture when you tab into the textbox as well as enter the text box by tapping/clicking on it, but if that isn't a problem, then this may be a more straightforward work-around
According to the .Net Framework, the MouseDown Event Handler on a TextBox is supported. What happens when you try to run the code?
Related
When i implement a KeyDown event on my Form, the event will never trigger. I've found posts saying i need to implement the CefSharp.IKeyboardHandler interface but i don't understand how i should implement this. can Someone give me an example of how to implement this or suggest another fix i could use to bypass this problem?
I'm using CefSharp version 49.0.1 and .Net framework 4.0.
i'm trying to implement keyboard shortcuts into my winform application and have KeyPreview on the form set to True. The problem i'm noticing is that it never triggers the KeyDown event of the form when i go over it with the debugger turned on.
This is the code i used to initiate the event on the mainForm:
public ChromiumWebBrowser browser;
//is called in the constructor of the class
public void InitBrowser()
{
Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser("www.google.com")
{
Dock = DockStyle.Fill,
};
this.Controls.Add(browser);
}
//trying to open up a messagebox when the "ctrl+w" keys are pressed
//whilst the form is active
private void MCMainForm_KeyDown(object sender, KeyEventArgs e)
{
if(e.Control && e.KeyCode.ToString() == "W")
{
MessageBox.Show("test");
}
}
So I have a windows forms program I am trying to design and I want the draw panel to be able to change colour based on a colour selected from the built in ColorDialog.
I need to detect the firing of the draw panels BackColorChanged event and then have other code happen then. Can anyone tell me how to create a handler for this, feel I may be missing something simple but cant quite figure it out.
To be notified when the BackColorChanged event is fired, you can subscribe to the BackColorChanged event when initializing the form:
public class YourForm : Form
{
public YourForm()
{
InitializeComponents();
somePanel.BackColorChanged += SomePanel_OnBackColorChanged;
}
public void SomePanel_OnBackColorChanged(object sender, EventArgs e)
{
//Back color has changed, do something
}
}
If you want to change the backcolor of a Panel by choosing a color from a ColorDialog, you don't need no events from that panel.
Open the ColorDialog, wait for it to be closed with "OK" and set the color accordingly:
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
panel.BackColor = colorDialog1.Color;
}
That's what I understood. If you really need to use an event from the Panel, why don't you just use the event every WinForms Control offers: BackColorChanged. See Isma's answer for that.
I have used the mouse down event of the treeview control. And I want to set the selected node as the node on which the mouse down event has happened. How can this problem be resolved?
The MouseDown event is fired before the node is selected. Try handling the AfterSelect event instead. If e.Action is set to TreeViewAction.ByMouse then the event was raised by the mouse.
A couple of options come to mind here. Both might well be overkill, but they still solve the problem.
Handle the MouseDown event and use the HitTest method to determine which node the user clicked on. If they clicked on a valid node, manually set the focus to that node via the SelectedNode property.
private void myTreeView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
myTreeView.SelectedNode = myTreeView.HitTest(e.Location).Node;
}
}
The bazooka-style solution is to override the WndProc method and listen for WM_RBUTTONDOWN messages. I've done this in my own extended version of the TreeView control because it allows me to fix some really minor stuff that normal, non-obsessive people probably wouldn't notice. I go into excruciating detail in my answer here.
Basically, you're doing the same thing as the code above does, but at a lower level, which stops the native control from pulling some shenanigans with the focus. I don't remember if they actually apply here (hence the potential overkill), but I'm too lazy to fire up Visual Studio to see for sure.
public class FixedTreeView : System.Windows.Forms.TreeView
{
protected override void WndProc(ref System.Windows.Forms.Message m)
{
const int WM_RBUTTONDOWN = 0x204;
if (m.Msg == WM_RBUTTONDOWN)
{
Point mousePos = this.PointToClient(Control.MousePosition);
this.SelectedNode = this.GetNodeAt(mousePos);
}
base.WndProc(ref m);
}
}
The first method should work just fine for you. Try that before breaking out bigger weapons.
Basically, I have a form with a custom control on it (and nothing else). The custom control is completely empty, and the form has KeyPreview set to true.
With this setup, I am not receiving any KeyDown events for any arrow keys or Tab. Every other key that I have on my keyboard works. I have KeyDown event handlers hooked up to everything that has such events, so I'm sure I'm not missing anything.
Also of note is that if I remove the (completely empty) custom control, I DO get the arrow key events.
What on earth is going on here?
EDIT:
I added this to both the form and the control, but I'm STILL not getting arrow keys:
protected override void WndProc(ref Message m) {
switch (m.Msg) {
case 0x100: //WM_KEYDOWN
//this is the control's version. In the form, it's this.Text
ParentForm.Text = ((Keys)m.WParam).ToString();
break;
}
base.WndProc(ref m);
}
I also checked with Spy++, and determined that the form itself is not getting any WM_KEYDOWN messages, they're all going to the control. However, that said, the control IS getting the arrow key WM_KEYDOWN messages. Sigh.
Edit 2: I've also updated the ZIP file with this version. Please look at it, if you want to help...
Edit 3:
I've figured this out, sort of. The form is eating the arrow keys, probably in an attempt to maintain focus amongst its children. This is proven by the fact that I DO get the events if the form is empty.
Anyway, if I add this code to the form, I start getting the events again:
public override bool PreProcessMessage(ref Message msg) {
switch (msg.Msg) {
case 0x100: //WM_KEYDOWN
return false;
}
return base.PreProcessMessage(ref msg);
}
When I override this, the form doesn't get a chance to do its dirty work, and so I get my KeyDown events as I expect. I assume that a side effect of this is that I can no longer use my keyboard to navigate the form (not a big deal in this case, as it's a game, and the entire purpose of this exercise is to implement keyboard navigation!)
The question still remains about how to disable this "properly", if there is a way...
I've done some extensive testing, and I've figured everything out. I wrote a blog post detailing the solution.
In short, you want to override the ProcessDialogKey method in the form:
protected override bool ProcessDialogKey(Keys keyData) {
return false;
}
This will cause the arrow keys (and tab) to be delivered as normal KeyDown events. HOWEVER! This will also cause the normal dialogue key functionality (using Tab to navigate controls, etc) to fail. If you want to retain that, but still get the KeyDown event, use this instead:
protected override bool ProcessDialogKey(Keys keyData) {
OnKeyDown(new KeyEventArgs(keyData));
return base.ProcessDialogKey(keyData);
}
This will deliver a KeyDown message, while still doing normal dialogue navigation.
If focus is your issue, and you can't get your user control to take a focus and keep it, a simple work-around solution would be to echo the event to your user control on the key event you are concerned about. Subscribe your forms keydown or keypress events and then have that event raise an event to your user control.
So essentially, Form1_KeyPress would Call UserControl1_KeyPress with the sender and event args from Form1_KeyPress e.g.
protected void Form1_KeyPress(object sender, KeyEventArgs e)
{
UserControl1_KeyPress(sender, e);
}
Otherwise, you may have to take the long route and override your WndProc events to get the functionality you desire.
I am using the webbrowser control in visual studio. I think it is a wrapper around internet explorer. Anyway all is going well I am using it in edit mode however I can't get he document's keydown event to fire (in order to catch ctrl+v) anyone had similar problems with it?
Anyone have a solution?
Indeed the webbrowser control is just a wrapper of the IE browser control.
Is your problem that the controls PreviewKeyDown not working? Seems to be working for me as long as the control has focus.
webBrowser1.PreviewKeyDown += new PreviewKeyDownEventHandler(webBrowser1_PreviewKeyDown);
....
private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
Console.WriteLine(e.KeyCode.ToString() + " " + e.Modifiers.ToString());
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.V) {
MessageBox.Show("ctrl-v pressed");
}
}
but perhaps I am not completely understanding?
You should override a "WndProc()" method in derived class from WebBrowser control or in form, which contains a webbrowser. Or you can catch the keys with custom message filter ( Application.AddMessageFilter ). With this way you can also filter a mouse actions.
I had same problems years ago, but I don't remember which way i used.
You mentioned the KeyDown event of the 'document'. If you are referring to the WebBrowser control's Document property (type HtmlDocument) it only has events for MouseUp, MouseDown, etc but not keyboard events. You want to register your event handler with the WebBrowser control's PreviewKeyDown delegate. You may also want to set the value of the WebBrowser control's WebBrowserShortcutsEnabled property to false if you don't want standard Internet Explorer shortcuts to have their usual effect. You should also make sure that the WebBrowser control is in focus by manually calling its Focus() method and setting the TabStop property of other controls on the form to false. If this is not possible because you have other controls on the form that need to accept focus, you also might want to add an event handler for the KeyDown event of the Form itself.
How to trap keystrokes in controls by using Visual C#
e.g.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
const int WM_KEYDOWN = 0x100;
const int WM_SYSKEYDOWN = 0x104;
if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
switch(keyData)
{
case Keys.Down:
this.Parent.Text="Down Arrow Captured";
break;
...
}
}
return base.ProcessCmdKey(ref msg,keyData);
}