AcceptButton not clicked when enter is pressed - c#

This seems very basic but have not been able to find any information on it.
I have a button in a Windows form that is the AcceptButton and should always have the AcceptButton behavior i.e. pressing the enter key when focus is on the form should always click the button and it should always be highlighted to show that action is available.
However, when a different button is clicked, that button is now highlighted and when enter is pressed, that button is clicked instead of the AcceptButton.
Here is my test application that shows the behavior:
public Form1()
{
InitializeComponent();
AcceptButton = btnAccept;
}
private void btnAccept_Click(object sender, EventArgs e)
{
MessageBox.Show("You clicked the accept button");
Debug.WriteLine(AcceptButton == btnAccept ? "true" : "false");
}
private void btnNothing_Click(object sender, EventArgs e)
{
Debug.WriteLine(AcceptButton == btnAccept ? "true" : "false");
}
private void btnBrowse_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
Debug.WriteLine(AcceptButton == btnAccept ? "true" : "false");
}
When the application launches initially - it works as I expect, btnAccept is highlighted and enter clicks that button.
However, when I click btnNothing or btnBrowse, that button is now highlighted and pressing enter clicks that button again. Additionally, the debug lines always show that btnAccept is still the AcceptButton even when the enter button is clicking a different button.
The CanFocus property that Button inherits from Control looks promising, but this property is read only.
What's going on here? Is there some property I can use to change this behavior?
Edit
Ended up going with this solution based on #AWinkle 's answer:
public Form1()
{
InitializeComponent();
AcceptButton = btnAccept;
}
private void btnAccept_Click(object sender, EventArgs e)
{
MessageBox.Show("You clicked the accept button");
}
private void btnNothing_Click(object sender, EventArgs e)
{
Button acceptButton = (Button)this.AcceptButton;
acceptButton.Select();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
Button acceptButton = (Button)this.AcceptButton;
acceptButton.Select();
}

Related

What's the proper event to use for onFocus button after 2nd form is closed?

Using: C# Forms VS2015
What I'm trying to do:
On form1, I have a textbox (tbJobTitle) and a button (bChooseJobTitle -> form2) for a "Job Title" of an employee.
The textbox(enabled=false) displays the chosen Job Title of an employee.
The button bChooseJobTitle opens another form (form2) that has a datagrid and 2 buttons (Choose & Cancel)
using System.Threading;
...
Thread the1;
...
private void bChooseJobTitle_Click(object sender, EventArgs e)
{
the1 = new Thread(OpenNew_tblJobTitle);
the1.SetApartmentState(ApartmentState.STA);
the1.Start();
}
private void OpenNew_tblJobTitle(object obj)
{
Application.Run(new form2());
}
...
I initially set a global string MyVar.Employee_Choose_idJobTitle (default "" ) to store the choosen index primary key if the user selected content and click the Choose button. If the Cancel button is click the MyVar.Employee_Choose_idJobTitle will remain = "".
//... at form2 "Choose" button
private void bChoose_Click(object sender, EventArgs e)
{
MyVar.idJobTitle = dataGridView1.CurrentRow.Cells[0].Value.ToString();
this.Close();
}
private void bCancel_Click(object sender, EventArgs e)
{
this.Close();
}
When form2 is closed either by "Choose" button or "Cancel" button, the focus goes back to form1's bChooseJobTitle button.
How do I trigger this event?
...so that can test if the content of MyVar.idJobTitle is not null and add the proper value to my textbox.
I was looking for the button events like onFocus or activate but could not find any. Do I use form events instead to do this?
Quite simply, use event Form Activate if you like.
private void form1_Activated(object sender, EventArgs e)
{
if (MyVar.idJobTitle != "")
{
tbJobTitle.Text = Choose_idJobTitle;
MyVar.idJobTitle = "";
}
}

C# WinForms: bring back focus to form after button click

my simple WinForms app consists of one Form which has 2 important methods:
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q)
{
qKeyPressed = true;
}
keyTimer.Start();
}
private void MainWindow_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q)
{
qKeyPressed = false;
}
keyTimer.Stop();
}
In form's constructor I use button1.TabStop = false; to set button1 focus to false, so my form can handle methods KeyUp and KeyDown. I want my form to be focused even after button1 click event. I tried:
button1.TabStop = false;
this.Focus();
in private void button1_Click(object sender, EventArgs e) but this doesn't seem to work and button after click looks like focused and methods KeyUp and KeyDown are disabled. How can my problem be solved? Thanks in advance
Run this in the button click function:
this.ActiveControl = null;
When you click the button, you set it as the active control. Removing this property will let you put focus on your form.

How to make a textbox invisible when radio button checked

I am working on C# application. I have 10 radio buttons in a group panel, so now if I only checked the radioButton10 then textBox1 will be visible, if I checked someone of the other radio Buttons (radioButton1 .... radioButton9) then textBox1 should be invisible.
I wrote the following code but the textBox1 is still visible. If the code is right where can I wrote it(form load, some function ... etc) if it's NOT, then Please Help.
public TeamInfoForm()
{
InitializeComponent();
showTeam();
if (radioButton10 .Checked)
textBox1 .Visible = true;
else
textBox1 .Visible = false;
}
I think you forgot to implement the event that occur when you check or uncheck the radiobutton. Try to imlpement "OnCheckChanged" event for the radiobutton and you have to set autopostback to true if you want that the event occurs, otherwise the event won't work.
Initially you have to set the Visible property of textBox1 to false in Forms Designer. Otherwise you can set it in FormInitialize() method. Next you have write code like below
public void ToggleTextBox()
{
textBox1.Visible = radioButton3.Checked;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
ToggleTextBox();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
ToggleTextBox();
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
ToggleTextBox();
}

Visual Studio C# Inconsistent Screen Behaviour Using Single Method

I have a small mystery on my hands. I have created a form that prompts the user for some input then displays a CancelOK MessageBox showing the input. The user can enter the input either by clicking on a Submit button or by hitting the Enter key. The trick is, if the user cancels, I want the MessageBox to close and the original text in the text field to be highlighted again. Here is the code:
namespace WindowsFormsManualAdditions
{
public partial class ManualAdditionsForm : Form
{
public ManualAdditionsForm()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
additionsAction();
}
}
private void button1_Click(object sender, EventArgs e)
{
additionsAction();
}
private void additionsAction()
{
DialogResult btn = MessageBox.Show(textBox1.Text + " additions are being added manually", "Confirm Manual Additions", MessageBoxButtons.OKCancel);
if (btn == DialogResult.OK)
{
MessageBox.Show("Hellow World!");
this.Close();
}
else
{
textBox1.SelectionStart = 0;
textBox1.SelectionLength = textBox1.Text.Length;
}
}
}
}
The mystery is that when I run this form two different things can happen when the additionsAction() Method is called:
1) User enters data, hits Enter key, chooses cancel, message box disappears and original text is highlighted.
2) User enters data, clicks the Submit button, chooses cancel, message box disappears but the original text is NOT highlighted.
But both actions call the same method, namely additionsAction(), so how can the behaviour be different?!
Any advice is appreciated.
Regards.
The behavior is different because the action of clicking the mouse on the button removes focus from the textbox, whereas pressing a key does not.
Try adding a call to Control.Focus() or Control.Select() when the DialogResult is not OK:
textBox1.SelectionStart = 0;
textBox1.SelectionLength = textBox1.Text.Length;
textBox1.Focus();
// Or
textBox1.Select();
When Button1 is clicked, textBox1 doesn't have the focus, so no text is selected.

How can I override the default button on a form when focus is in a TextBox

I have a winform form which has typical OK and Cancel buttons.
The OK button is set as the default button. When the user presses the ENTER key, the form is closed.
The form also has a text box which has a Search button beside it. The text box allows the user to enter search text and the Search button initiates the search function.
I would like the user to be able to press the ENTER key when the text box has the input focus and have the search function activate.
The problem is that the form is grabbing the ENTER key event before the text box's handler gets it.
EDIT: I've tried using the following event handlers, but they never get hit, and the form closes when the Enter key is hit:
private void txtFilter_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
e.Handled = true;
}
}
private void txtFilter_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
e.Handled = true;
}
}
I've also tried enabling the KeyPreview property on the form and implementing a KeyDown event handler for the form, but the Enter key never seems to cause any of these to be hit.
What is the best way to accomplish this?
Try handling the Enter and Leave events of the TextBox to clear out your form's AcceptButton property:
private void txtFilter_Enter(object sender, EventArgs e) {
this.AcceptButton = null;
}
private void txtFilter_Leave(object sender, EventArgs e) {
this.AcceptButton = closeButton;
}
Then you can just process your KeyUp event as your want:
private void txtFilter_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
}
}
Add a KeyDown event handler to the textBox and then add this to it
if (e.KeyCode == Keys.Enter)
{
btnSearch.PerformClick();
}

Categories

Resources