Excluding autocompleting textbox from form accept property? - c#

I have a form in which I've linked the accept property to a button called submit on the form so that whenever enter key is pressed on any control in the form it fires the submit button's click event. I also have an auto completing textbox (to/from) on the form with auto complete mode set to SuggestAppend. I want to move to the next control(textbox amount) when the user presses the enter key. I tried the textbox's keyup event like this:
private void tb_to_from_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Return)
{
tb_amount.Focus();
}
}
This moves the control to the other textbox, however, the submit button is also pressed. How do I exclude tb_to_from from the accept property of the form so that it just moves to the next control?

Related

Assign a Keyboard key to a button click WINFORM

I am developing a small application with some buttons and textbox. What I am having problem is assigning a keyboard key (e.g. F3) to a button click.
For example if the user click the button Cash the code I wanted it's executed fine, but I want to make more easier instead clicking the button with mouse, I want the user be able to press the key on keyboard. I used the keydown event, also keypress event of that button, but still nothing.
I tried this keydown event
if (e.KeyCode == Keys.Enter)
{
btncash.PerformClick()
}
But still nothing
Do not use F3 function button it's used by OS for activating search. Enter key is fairs click event on focused control so do not use this also. Implement as suggested below.
In your Main form
Set KeyPreview to True in form load event.
Add KeyDown event handler with the following code
private void Form1_KeyDown(object sender,
KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.H)
{
btncash.PerformClick();
//btncash_Click(null, null);
}
}

Programmatically change AcceptButton property and trigger ButtonClickEvent inside a KeyPress event

I have a page with two independent forms; users would only ever fill out and submit one or the other. When the user presses enter, I would like to trigger the click event of the button of the form that currently has focus. How can I accomplish this?
Trigger Button click using .PerformClick() method on KeyPress event of the TextBox after verifying whether pressed key is Enter
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Check if pressed key is `Enter` key
if (e.KeyChar == (char)Keys.Return){
button1.PerformClick();
}
}

Simulate mouse click on a focused TextBox

Is this possible? I have a button with onClick() Method. If i click the Button i want to simulate a mouse click event inside a TextBox. With Focus() i can focus inside the TextBox but how to simulate left mouse click?
EDIT I have an AutoCompleteExtender that should show a list of names in a certain TextBox after the user clicks a Button. The TextBox can work both:the user writes a character and the list appears or he clicks the button and a whole list appears.
EDIT I have used AjaxControlToolkit and from my experience i have to click on the TextBox if i want the list to appear even though i have a focis on the TextBox and MinimumPrefixLength=0
Create a "logic" method where you do your work. When the user clicks on the TextBox, call your logic method. When you want to simulate a click, call your logic method also.
// Attach to the event
myTextBox.Click += new EventHandler(myTextBox_Click);
// Simulate a "click" event
MyLogic();
protected void myTextBox_Click(object o, EventArgs e) {
MyLogic();
}
private void MyLogic() {
// Do work
}

How to defocus a button when barcode scanner sent data ending with newline

I am writing a C# barcode application. I have a EAN-13 regex to detect barcodes in "Form1_KeyPress" function. I have no mechanism to detect where the input comes from. Here is my problem:
I have a reset button in the form which clears all fields and barcodes listed in a dataGridView. When I clicked on it, it gets focus as normal. When it has focus, if I read a barcode via barcode scanner, the newline at the end of each barcode reading causes this button to be clicked thus clearing all fields. So barcodes read are added to dataGridView but immediately deleted due to activation of reset button.
My current solution is to focus on a read-only textbox at the end of each "button_Click" function, but I don't want to write an irrelevant line at the end of each "click" function of buttons. What do you recommend? (by the way I cannot prevent surpress enter key in form's keydown function)
You can't capture the Enter key in the form's keystroke events because it is handled by the button.
If you add:
private void button_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.IsInputKey = true;
}
}
to a button then the Enter key won't cause the button to be clicked, and you will see a Form_KeyDown event for it.
You don't want to add this to every button, so create a simple UserControl which is just a button with this code added.
Update
This doesn't work for the space bar. If you set form.KeyPreview = true and add:
private void form_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
e.Handled = true;
}
}
then the space bar won't press the button but it will still work in text boxes.
I don't know why Space and Enter behave differently.

create textbox one by one on enter keypress event

am using c# vs-2005
am on project to create textbox one by one on form1 and am success on button click event my code is below.
// declare location point of textbox on Global Area.
private point txtboxstartpoint=new point(10,10);
private void button_click (Object sender,EventArgs)
{
Textbox tbx = new TextBox();
tbx.Location= txtboxstartpoint;
tbx.size=new size (200,20);
this.panel1.control.add(tbx);
txtboxstartpoint.y += 25;
}
this works fine on button click event but problem is on keypress event like on enter
i wants to create textbox on enter one by one. and for that i assume that any method have
to create and call enter keypress event on newly created control like textbox to create
another new textbox below the previous one.
Kindly help me. suggest proper code for the same.
It's very hard to understand your question, but let's try some guessing:
You have a form, and if a user presses some specific key, you'd like to create a new TextBox and show it on your form regardless which control has currently the focus in your form.
If this statement is true, you can set Form.KeyPreview to true. And add an event handler to Form.KeyDown.
Due to the fact, that you set the preview to true you'll get every keyboard hit before it will be give further to the control that has currently the focus. So here you can check if the key that was pressed is the one you're listening for. And if yes, just call your TextBoxFactory and set the e.Handled to true to prevent that this key stroke will additionally reach the currently focused control.
I use the KeyDown event, to intercept the 'F1' key to provide my small help in a very small programm. Here is the code:
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
//Your Code here
}
}

Categories

Resources