I have a WPF MainWindow and try to react to a certain key combination (CTRL + F4). I registered the following methods for testing purposes:
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
Log.AsInfo("PreviewKeyDown");
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
Log.AsInfo("KeyDown");
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
Log.AsInfo("KeyUp");
}
private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
{
Log.AsInfo("PreviewKeyUp");
}
Crazy thing is, those methods are only triggered after I interact with the application for the first time:
How it does not work:
I start the application
I enter CTRL + F4 on the keyboard
Nothing happens
How it works:
I start the application
I click on a random menu item with no functionality
I enter CTRL + F4 on the keyboard
Everything works, log messages are written
Any ideas? I am not able to even debug the situation, because none of the handler methods is called in the first place. I even tried this.Focus() in the MainWindow constructor, but this did not help either.
Found the solution:
The browser control I used is the EO.WebBrowser. It seems, that this browser control swallows every first time key interaction. I implemented the shortcut combination by binding the functionality I needed to a hotkey of the WebBrowser control as mentioned here. So it was more of a third party than a WPF problem.
Related
sorry for my bad english.
I am writing a keyboard application for the touchscreen. but,
for example, when I press the A key, sometimes it's writes 5 times A key . I am using basically button click event.
private void button42_Click(object sender, EventArgs e)
{
SendKeys.Send("A");
Thread.Sleep(100);
}
there is no problem with the mouse_click event. just touch
how can i do it?
1.Try handling it in a mouse_up or touch_up event instead of using a delay here. Actual implementation and events will depend on you framework.
mouse_up /touch_up will make sure that you even only fires when touch is released.
To add repetition on touch and hold, you may have to add additional logic however.
Try declaring a field to toggle the value of event fired.
private bool handling=false;
private void button42_Click(object sender, EventArgs e)
{
if(!handling)
{
handling=true;
SendKeys.Send("A");
Task.Delay(100).Wait();
handling=false;
}
}
I have been developing a windows form application and ran into a problem.
After trying various things (Listed below) I have come to seek your knowledge to help point me in the right direction.
I have replicated a much simpler version of my program:
As you can see, I have two textboxes. I want to be able to click on the textbox on the bottom (textbox1) and call some form of an event, in this case, for simplicity, pop up a message box.
I have been through the events listed here:
https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox_events(v=vs.110).aspx
And implemented them into my code as I expected one of them to work. However, this is not the case.
private void textBox1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("TextBox Entered");
}
//Above - Will pop message box when text entered.
private void textBox1_GotFocus(object sender, EventArgs e)
{
MessageBox.Show("TextBox Entered");
}
private void textBox1_Enter(object sender, EventArgs e)
{
MessageBox.Show("TextBox Entered");
}
private void textBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("TextBox Entered");
}
Does anybody know what I am missing? I presume what I am trying to achieve is actually possible?
Kind Regards,
B.
Ensure the event is subscribed to the methods you have written. You can do this in the design view using the Events tab of the property window (looks like a lightning bolt). As mentioned by others, a double click in the events window will generate the event's method for you, and subscribe to it automatically.
Another way is to subscribe directly using code; you could write this in the form constructor for example:
textBox1.TextChanged += textBox1_TextChanged;
I am very new with the C# UserControl. I have problems with the event Leave. This is my situation: I would like to go from usercontrolA to userControlB. Before going to userControlB, usercontrolA_Leave event is called.
private void usercontrolA_Leave(object sender, EventArgs e)
{
MessageBox.Show("you are leaving.....");
}
After MessageBox is shown, the program will not proceed to my userControlB. HOWEVER when there is no MessageBox in the code, the program can proceed to userControlB.
private void usercontrolA_Leave(object sender, EventArgs e){//anything but MessageBox}
In my case, I need MessageBox.
I need MessageBox(or other thing) for me to decide whether staying or leaving.. .
msdn Control.Leave Event
I heard about setting set focusor lost focus. Is that possible to use this?
I hope you guys could understand what I have written. Thank you in advance.. :)
You will not be able to preserve mouse movements once a MessageBox is created. This is a modal box that comes up on top of the existing window and takes the focus away from the current form and interrupts the mouse.
Consider an option that does not interrupt the mouse, such as writing out to a textbox. Create a TextBox will the multi-line and scrollbar options enabled. Then write to it.
private void usercontrolA_Leave(object sender, EventArgs e)
{
textBox1.Append("you are leaving A...\r\n");
}
private void usercontrolB_Enter(object sender, EventArgs e)
{
textBox1.Append("you are entering B...\r\n");
}
I've searched the Internet and this web site for any clues to fix my issue, but haven't found one.
I have a method that expects a string and then does SendKeys.SendWait(str). Everything works like passing in "{ENTER}" or just typing normal text.
But! If I pass in "{SUBTRACT}" it just doesn't work. I've also tried passing in the ASCII presentation of the key, but it threw exception that its unsupported.
I've also tried just doing SendKeys.Send("{SUBTRACT}") - no results what so ever.
Its just not doing anything. However, when I press the minus button on the keypad or on the top of the keyboard - functionality works.
Please note that this is using windows Automation Framework. May be this is what causing the problem. Has anyone had the same issues?
I have tried to show messagebox on Subtract KeyDown event. I have sent Subtract key on my button click event. But, make sure you have enabled KeyPreview property of your windows form.
private void button1_Click(object sender, EventArgs e)
{
SendKeys.Send("{SUBTRACT}");
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Subtract)
this.UltraGrid1.Rows.CollapseAll(true);
}
I'm writing a Hangman program in C# and when I press a keyboard button I want the button on the form to be clicked. Where should I write this? In form1.load()?
No, you should select the Form on which you want the event to be triggered, then go to the properties pane, select the event tab and go down to KeyPress event, double click it and add some code.
Normaly something like this would do what you want, just google the KeyChar value to determine the keyPress you want to control, you can add more if statements:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
Button1.PerformClick();
}
//Other if statements if you want to keep an eye out for other keyPresses
}
[edit] I just remembered you might be also considering the shortcuts, in wich case the Button1.Text property should be marked &Button1, this way the "B" would be underlined and accept the alt+B shortcut to execute the button click event.
The & symbol is set before the letter you want for the shortcut, make sure you dont use the same letter for various buttons.
You wouldn't write the code in Form.Load() if you want it to happen in response to a keyboard event. That event occurs (and the code inside of it is executed) when your form first loads (appears on screen).
How about handling the KeyPress event and writing the code in that method, instead? Your form has one of those events, too.
Sample code:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
Button1.PerformClick();
}
The PerformClick method will generate a Click event for a Button control. You can handle that Click event in a similar way:
private void Button1_Click(object sender, EventArgs e)
{
// do something in response to the button being clicked
// ...
MessageBox.Show("Button clicked!");
}
If this event-handling stuff is confusing to you, make sure that you pick up a good book on programming in C# and/or the .NET Framework so that you learn it well. It's very important and not something to skip!
You have to enable KeyPreview property on the form, then you have to implements the KeyPress event directly on the form :
Form1.KeyPress +=new KeyPressEventHandler(Form1_KeyPress);
private void Form1_KeyPress(object sender, KeyPressEventArgs e) {
if(e.KeyCode == someKey) {
button1.performclick();
}
}
private void Form_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Left)
{
// Do your hang job
Button_Click(sender, EventArgs.Empty);
}
}
You need to subscribe to the events of interest and process them after. But before you need to read and study how to do that. It's a not a difficult issue in C#.
http://msdn.microsoft.com/en-us/library/ms171534.aspx
One time subscribed to the events create a function that you can call from button click and from keydown.