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);
}
Related
I have got a virtual Keyboard and I wanna fill textbox only with that applet and not with keyboard.
how can I restrict textbox to don't get filled by keyboard?
You could solve that problem by handling the textbox-KeyPressed event.
If you use the following code every KeyPress gets ignored by the TextBox.
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
I hope that this helps you but I wouldnt recommend using it for the reasons Malte R. mentioned before.
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 want to select all the contents of a MaskedTextBox when the clicks (or tabs onto) the control, so they can easily replace the old content. I tried calling SelectAll() in the Enter event, but that didn't work at all.
I switched to using the GotFocus event, which works great when tabbing through controls, but doesn't work when I click on it with the mouse. I would only want to select all the contents when first entering/focusing on the control (subsequent clicks might be used to position the cursor to edit the existing text).
I added a button and tried calling SelectAll() in the button click event, but that didn't do anything either. What's going on? Is this a bug?
How can I get around this?
Steps to reproduce
Create a new Windows Form Application in .NET 4.0 in Visual
Studio 2010.
Add a TextBox, MaskedTextBox, and Button to the default form
Change the Mask property on the MaskedTextBox to "_____".
Add some event handlers:
private void maskedTextBox1_GotFocus(object sender, EventArgs e)
{
Debug.WriteLine("GotFocus");
maskedTextBox1.SelectAll();
}
private void button1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Click");
maskedTextBox1.SelectAll();
}
Run the program, entered some data into the MaskedTextBox, tab through controls back to it. It selects the contents of the MaskedTextBox.
Select the other TextBox. Try clicking on MaskedTextBox. Output shows that GotFocus event was called, but text doesn't get selected.
Try clicking on button in form. Text doesn't get selected.
Tested in Visual Studio 2010 with .NET 4.0 in a Windows Forms Application project
Why this isn't a duplicate of TextBox.SelectAll() does not work with TAB
If you notice, the title says "SelectAll doesn't work with TAB". In my case, it does work with Tab, it doesn't work with the mouse - completely opposite scenario. The answer for that question is to use the GotFocus event. I'm already using the GotFocus event, but it doesn't work. That answer does not answer this question. It is clearly not a duplicate. If I'm wrong, please explain in the comments.
Your SelectAll() is being overwritten by the default functionality of the masked textbox select. I would use the Enter event, it allows for tabbed entry or mouse click entry to the masked text box. You will most likely need to use the BeginInvoke method. Try the code below. It worked for me when I tried...
private void maskedTextBox1_Enter(object sender, EventArgs e)
{
BeginInvoke((Action) delegate { SetMaskedTextBoxSelectAll((MaskedTextBox) sender); });
}
private void SetMaskedTextBoxSelectAll(MaskedTextBox txtbox)
{
txtbox.SelectAll();
}
Executing Focus before Select All worked for me:
private void Masked_Enter(object sender, EventArgs e) {
((MaskedTextBox)sender).Focus();
((MaskedTextBox)sender).SelectAll();
}
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.
The following code is to test the LWin keyup function in C# when the form is active. It's working fine and now when the form is active I need only the function alone has to take place and whenever I click on Lwin button start menu should not open. How can I achieve this?
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.LWin)
{
MessageBox.Show("Function working!");
}
}
What have you tried?
Try setting e.Handled to True.
Try the same thing with the KeyPress event. And the KeyDown event.
If these options don't work (and I suspect they won't), you've exhausted the options available to you in managed Windows Forms. I can't immediately think of a solution using P/Invoke, but I think that's the avenue you'll have to explore.