Focus buttons with same access key instead of firing the first - c#

Pressing Alt+B fires the first button though button 2 has the same access key.
How can I manage to focus button 1 on a first Alt+B and to focus button 2 on a second Alt+B without handling keydown event or similar?
private void button1_Click(object sender, EventArgs e)
{
System.Media.SystemSounds.Beep.Play();
}
private void button2_Click(object sender, EventArgs e)
{
System.Media.SystemSounds.Hand.Play();
}

You can override ProcessMnemonic to customize the behavior of your button when a mnemonic pressed. When processing mnemonic, when you detected the control is in the state that should execute an action based on mnemonic character, return true, otherwise return false.
In below implementation, the control first checks if the mnemonic belongs to the control, then if it is not Focuded then it calls Focus and returns true, otherwise (it's focused or it should not handle the mnemonic) returns false. This way it allows the focus move between controls with the same mnemonic:
using System.Windows.Forms;
public class MyButton:Button
{
protected override bool ProcessMnemonic(char charCode)
{
if (this.UseMnemonic && this.Enabled && this.Visible &&
Control.IsMnemonic(charCode, this.Text))
{
if (!this.Focused)
{
this.Focus();
return true;
}
}
return false;
}
}

Related

Detecting change source (user or program) in radio buttons

I'm trying to set a flag saying whether the last change on the Checked property was caused by the user or the program.
I'm using a custom RadioButton:
public class MyRadioButton : RadioButton
{
ValueChanger valueChanger = ValueChanger.Program;
public MyRadioButton()
{
this.Click += OnButtonClickedByUser;
this.CheckedChanged += OnCheckChange;
}
public void setChecked(bool val)
{
this.valueChanger = ValueChanger.Program;
this.Checked = val;
}
void OnButtonClickedByUser(Object sender, EventArgs e)
{
this.valueChanger = ValueChanger.User;
}
void OnCheckChange(Object sender, EventArgs e)
{
// do stuff depending on 'this.valueChanger'
}
enum ValueChanger
{
User,
Program
};
}
I call setChecked whenever the value was changed because of a message received from a serial connection, and I expect OnButtonClickedByUser to be called by the Click event whenever the value is changed through the UI.
My problem is that the CheckedChanged event fires before the Click event, which makes OnCheckChange unreliable.
Is there any way to fix that ?
User can change the value of the RadioButton by click on the control or by moving the focus to the control (arrow key, tab, mnemonic key combination).
Both OnEnter and ProcessMnemonic try to call PerformClick which calls OnClick which is responsible to checking the control. So you can override OnClick method:
protected override void OnClick(EventArgs e)
{
// Here CheckedChanged event has not been raised yet
base.OnClick(e);
}
To find out more about how RadioButton works internally, take a look at its source code.

WinForms listbox keydown event won't fire

I am trying to make a WinForm ListBox in which you can loop trough using the arrow keys. I also have two buttons on which you can click to go up and down the list. The buttons do produce the desired effect. The problem is that the ListBox's keyDown event is never triggered
public MainForm()
{
InitializeComponent();
if (this.clipboardHistoryList.Items.Count > 0)
this.clipboardHistoryList.SetSelected(0, true);
clipboardHistoryList.Select();
}
private void goUpButton_Click(object sender, EventArgs e)
{
goUpList();
}
private void goDownButton_Click(object sender, EventArgs e)
{
goDownList();
}
private void goDownList()
{
if (clipboardHistoryList.SelectedIndex == clipboardHistoryList.Items.Count - 1)
{
clipboardHistoryList.SetSelected(0, true);
}
else
{
clipboardHistoryList.SetSelected(clipboardHistoryList.SelectedIndex + 1, true);
}
}
private void goUpList()
{
if (clipboardHistoryList.SelectedIndex == 0)
{
clipboardHistoryList.SetSelected(clipboardHistoryList.Items.Count - 1, true);
}
else
{
int l_currentlySelected = clipboardHistoryList.SelectedIndex;
clipboardHistoryList.SetSelected(l_currentlySelected - 1, true);
}
}
private void clipboardHistoryList_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up) //Brekpoint is never reached
{
goUpList();
}
else if (e.KeyCode == Keys.Down)
{
goDownList();
}
}
I have put the MainForm's keypreview proprety to true.
The arrow keys do work by default on a listbox but they won't let you go from last to first element if you press the down arrow on the last element --hopes this makes sense.
EDIT
I have seen on Microsoft's documentation that I need to override the ProcessDialogKey method but I am not exactly sure of what I need to do.
Perform special input or navigation handling on a control. For example, you want the use of arrow keys in your list control to change the selected item. Override ProcessDialogKey
Is there already a built-in way to enable this behaviour?
What did I miss?
Thanks!
From looking at the code in your Designer.cs file, it doesn't look like you've actually got your clipboardHistoryList control wired into your clipboardHistoryList_KeyDown event handler. You can do that through the "Events" subtab of the Properties window in your visual studio form designer (look for the little lightning bolt icon) and wire up the event through the designer that way, or alternatively you can do it in code:
public MainForm()
{
InitializeComponent();
if (this.clipboardHistoryList.Items.Count > 0)
this.clipboardHistoryList.SetSelected(0, true);
clipboardHistoryList.Select();
clipboardHistoryList.KeyDown += clipboardHistoryList_KeyDown;
}

How to validate textbox in C# WF?

I have two text boxes in windows form.
Also one disabled button.
How I can do validation text box:
if field is empty then disable button
if value inside field is less then 5 then disable button
other case - enable button
I tried this on event TextChange, but when I tried to enter value 43 I get notification, because event TextChange works after each typing symbols.
Code:
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(textBox2.Text))
{
button6.Enabled = true;
}
}
If you don't want to validate each time a key is pressed but would rather validate when the user leaves the field, instead of hooking into the TextChanged event, hook into the Leave event.
private void textBox2_Leave(object sender, EventArgs e)
{
button6.Enabled = !(string.IsNullOrEmpty(textBox2.Text)) && textBox2.Text.Length >= 5;
if (!button6.Enabled)
{
textBox2.Focus();
}
}
Update your event handle like this :
private void textBox2_TextChanged(object sender, EventArgs e)
{
button6.Enabled =
!String.IsNullOrEmpty(textBox2.Text) && textBox2.Text.Length > 5
}
As for disabling the button on start up, you just set button6 to be disabled by default.
Or, invoke your validation in your constructor :
textBox2_TextChanged(null, null);
Neither TextChanged nor Leave events are appropriate for this. The proper event is called (surprise:-) Validating. You need to set e.Cancel = true if validation is wrong. More info: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating(v=vs.110).aspx

Put focus back on previously focused control on a button click event C# winforms

I have made a custom Number Keypad control that I want to place in my winform application. All of the buttons have an OnClick event to send a value to the focused textbox in my form where I have placed my custom control. Like this:
private void btnNum1_Click(object sender, EventArgs e)
{
if (focusedCtrl != null && focusedCtrl is TextBox)
{
focusedCtrl.Focus();
SendKeys.Send("1");
}
}
focusedCtrl is supposed to be set on the MouseDown event of the button like this:
private void btnNum1_MouseDown(object sender, EventArgs e)
{
focusedCtrl = this.ActiveControl;
}
where this.ActiveControl represents the active control on the form.
My problem is that the button always receives the focus before the event detects what the focused control was previously. How can I detect which control had the focus before the button got the focus? Is there another event I should be using? Thanks in advance!
EDIT: Also, I would rather not use the GotFocus event on each textbox in the form to set focusedCtrl since that can be tedious and because I would like to have all the coding of my custom control be in the control itself and not on the form where it is placed. (I will do this, though, if there is no other practical way to do what I am asking)
Your requirement is fairly unwise, you'll want some kind of guarantee that your button isn't going to poke text into inappropriate places. You really do need to have the form co-operate, only it knows what places are appropriate.
But it is not impossible, you can sniff at input events before they are dispatched to the control with the focus. In other words, record which control has the focus before the focusing event is fired. That's possible in Winforms with the IMessageFilter interface.
Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing your existing buttons.
using System;
using System.Windows.Forms;
class CalculatorButton : Button, IMessageFilter {
public string Digit { get; set; }
protected override void OnClick(EventArgs e) {
var box = lastFocused as TextBoxBase;
if (box != null) {
box.AppendText(this.Digit);
box.SelectionStart = box.Text.Length;
box.Focus();
}
base.OnClick(e);
}
protected override void OnHandleCreated(EventArgs e) {
if (!this.DesignMode) Application.AddMessageFilter(this);
base.OnHandleCreated(e);
}
protected override void OnHandleDestroyed(EventArgs e) {
Application.RemoveMessageFilter(this);
base.OnHandleDestroyed(e);
}
bool IMessageFilter.PreFilterMessage(ref Message m) {
var focused = this.FindForm().ActiveControl;
if (focused != null && focused.GetType() != this.GetType()) lastFocused = focused;
return false;
}
private Control lastFocused;
}
Control focusedCtrl;
//Enter event handler for all your TextBoxes
private void TextBoxesEnter(object sender, EventArgs e){
focusedCtrl = sender as TextBox;
}
//Click event handler for your btnNum1
private void btnNum1_Click(object sender, EventArgs e)
{
if (focusedCtrl != null){
focusedCtrl.Focus();
SendKeys.Send("1");
}
}
you have an event called lostFocus you can use
button1.LostFocus +=new EventHandler(dataGridView1_LostFocus);
and in the event:
Control lastFocused;
void dataGridView1_LostFocus(object sender, EventArgs e)
{
lastFocused = sender as Control;
}
in that way you can always know what is the Control that was focused previously
now, correct me if i'm wrong, but you do it for the SendKeys.Send("1"); to know which textBox need to receive the number. for that you can use GotFocus event and register only the textBoxs to it.
you can also do what windows is doing and use just one textbox like here:
if it's fits your needs
What about using this with the parameter forward = false?
Control.SelectNextControl Method
You'd probably call it on your "custom Number Keypad control".

Tabpage control leave

I have a tab control and 3 tabpages in it. ( C#)
if i am in tab 2, and edit a text box value
and then click tab 3, i need to validate what was enetered in the text box.
if correct i should allow to to switch to tab 3 else should remain in tab 2 it self
how do i achieve this?
iam curently handling the "leave" event of the tabpage2,
i validate the text box value there and if found invalid
i set as tabcontrol.Selectedtab = tabpage2; this does
the validation but switches to new tab! how could i restrict the navigation.
I am a novice to C#, so may be i am handling a wrong event!
Here is the relevant code:
private void tabpage2_Leave(object sender, EventArgs e)
{
if (Validatetabpage2() == -1)
{
this.tabcontrol.SelectedTab =this.tabpage2;
}
}
While the other approaches may work, the Validating event is designed specifically for this.
Here's how it works. When the SelectedIndex of the tab control changes, set the focus to the newly selected page and as well as CausesValidation = true. This ensures the Validating event will called if the user tries to leave the tab in any way.
Then do your normal validation in a page specific Validating event and cancel if required.
You need to make sure to set the initial selected tab page in the Form Shown event (Form_Load will not work) and also wire up the tab page specific validating events.
Here's an example:
private void Form_Shown(object sender, System.EventArgs e)
{
// Focus on the first tab page
tabControl1.TabPages[0].Focus();
tabControl1.TabPages[0].CausesValidation = true;
tabControl1.TabPages[0].Validating += new CancelEventHandler(Page1_Validating);
tabControl1.TabPages[1].Validating += new CancelEventHandler(Page2_Validating);
}
void Page1_Validating(object sender, CancelEventArgs e)
{
if (textBox1.Text == "")
{
e.Cancel = true;
}
}
void Page2_Validating(object sender, CancelEventArgs e)
{
if (checkBox1.Checked == false)
{
e.Cancel = true;
}
}
private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
{
// Whenever the current tab page changes
tabControl1.TabPages[tabControl1.SelectedIndex].Focus();
tabControl1.TabPages[tabControl1.SelectedIndex].CausesValidation = true;
}
You can use the TabControl Selecting event to cancel switching pages. Setting e.Cancel to true in the event stops the tabcontrol from selecting a different tab.
private bool _cancelLeaving = false;
private void tabpage2_Leave(object sender, EventArgs e)
{
_cancelLeaving = Validatetabpage2() == -1;
}
private void tabcontrol_Selecting(object sender, TabControlCancelEventArgs e)
{
e.Cancel = _cancelLeaving;
_cancelLeaving = false;
}

Categories