Next Textbox on Enter with 2 group boxes - c#

I have two group boxes, in the first group box I have 3 textboxes and in the second group box I have 1 textbox. I added this code:
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyData == Keys.Enter))
{
SelectNextControl(ActiveControl, true, true, true, true);
}
}
But enter only works in the first group box and jumps over the button and skips the 2nd group box.
What should i do?

As the MSDN mentioned:
The SelectNextControl method activates the next control in the tab order if the control's Selectable style bit is set to true in ControlStyles, it is contained in another control, and all its parent controls are both visible and enabled.
You can find your Controls (Textbox's) tab order number on the Property TabIndex in the Designer.

Related

C# Windows Form Application - How to get Radio Button to prompt text to appear in a Listbox upon clicking a Button?

I have a Windows Form Application in Microsoft Visual Studio 2017, and for the design of that Form, I have three Radio Buttons (in a Group Box), a Button, a Textbox and a List Box. It looks like this:
How can I make it so that, when one of the Radio Buttons are selected, it will display the name of it in the Textbox next to where it says "Required Room Size", and display in the Listbox along with a price next to it. Don't worry, I already have the prices allocated to each Radio Button (Single = $80.00, Double = $110.00, Triple = $140.00), and I know that you are supposed to use "t\t\" in order to create a column in the Listbox, I'm just not sure how you are supposed to get text to display in said Listbox once prompted to do so by clicking a Button.
TL;DR - How do you make it so that when you select a Radio Button and then press a standard Button, it will display in both a Textbox and a Listbox?
A radiobutton doesn't know that it is clicked unless you add an eventhandler to it.
This can be accomplished by doing the following.
RadioButton1.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
Everytime a RadioButton gets checked or unchecked, the radioButtons_CheckChanged method will execute.
In the method you can set the TextBox.Text property.
TextBox1.Text = RadioButton1.Text;
Another way to do this is, is to loop through the radiobuttons and look for the radiobutton that has been checked.
When the selected radiobutton has been found you can copy the text of the radiobutton into the textbox.
Button1_Click(RoutedEventArgs e, object o)
{
if(RadioButton1.Checked) # Single Room
{
TextBox1.Text = RadioButton1.Text;
}
if(RadioButton2.Checked) # Double Room
{
TextBox1.Text = RadioButton2.Text;
}
if(RadioButton3.Checked) # Triple room
{
TextBox1.Text = RadioButton3.Text;
}
}
It's up to you how you want to check which of radiobuttons has been checked.
But these are some of the ways it can achieved.

Winform Combo box: Control shifts to end of the text [duplicate]

I'm using winforms with a combobox that has a wider drop down width than it's size. when a user selects something from there, it displays just the ending of the text instead of the beginning. how do i default it to show text starting with the start of the string?
ie. combobox has items
Atlanta Georgia
Athens Georgia
Miami Florida
....
and the user picks one and all they see in the box afterwards is "a Georgia"
no, i unfortunately don't have the realestate to make the combobox bigger, and the order of the words in the list won't be changed.
Thanks!
The trick is to call the select after the SelectedIndexChanged event happens:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
this.BeginInvoke(new Action(() => { comboBox1.Select(0, 0); }));
}
Select position zero by force:
comboBox1.Select(0, 0);
...after the selection is complete and locked in, just call Select.
You could also use:
comboBox1.SelectAll();
...if you want it all highlighted.

How can i totally disable tabbing on DataGridView but keep ability to select rows?

How can I totally disable tabbing on DataGridView so it won't go through cells at all?
I use DataGridView as music playlist in my application and I don't need that annoying windows default selection frame around cells. I want be able to select rows normally. I managed to hide selection border on buttons with SetStyle(ControlStyles.Selectable, false) but this does not disable tabbing on DataGridView.
Handle the KeyDown event of the DataGridView and call the parent (or grandparent) control's SelectNextControl method.
private void dataGridView1_KeyDown( object sender, KeyEventArgs e )
{
if ( e.KeyCode == Keys.Tab )
{
SelectNextControl( dataGridView1, true, true, true, true );
// or Parent.SelectNextControl() if the grid is an only child, etc.
e.Handled = true;
}
}
This will cause the whole grid to behave like tabbing among text boxes and buttons - you tab into the grid, and another press of the tab key will tab out and onto the next control. This retains navigation within the grid by the cursor keys. Refer to the linked MSDN documentation for options on the direction of tabbing, etc., which are what all those terrible Boolean parameters configure. The first parameter sets which control the "next" tab search begins from, so you can set that to a parent or sibling or grandparent.
if you want to DataGrid don't focus, you can set it's Enable property to false, this control on the form doesn't get focus, but in this way you must add or delete rows in DataGridView with specific button (it means a button for add and another for delete)
but if you want their cells don't focus, you should following this: in KeyDown event of your form, type this code
if (e.KeyCode == Keys.Tab)
{
dgvMain.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
[other component of your form like a textbox or a button].Focus();
}
with this, your DataGridView only highlight the whole selected row
OK. I've managed to do that. This ARTICLE helped me a lot. I used form's OnActivated and OnDeactivated events to disable and enable TAB key. Here you have sample code:
protected override void OnActivated(EventArgs e) {
ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule;
objKeyboardProcess = new LowLevelKeyboardProc(captureKey);
ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);
base.OnActivated(e);
}
protected override void OnDeactivate(EventArgs e) {
UnhookWindowsHookEx(ptrHook);
objKeyboardProcess = null;
ptrHook = IntPtr.Zero;
base.OnDeactivate(e);
}
There were a couple of problems that came up while i was trying to make it work but that's different story. Happy coding! :)
You can also set:
dataGridView.TabStop=false;
This will skip the grid when the tab button is hit.

Tab index is not working on radio buttons

This is the part of my form that I am asking about
This is the tab index:
The problem that the tab goes from Farmer Audi Status to Yes, then to Ownder Bank Name instead of going to No
please notice that the yes and no already have 0.1.6.0 and 0.1.6.1 respectively.
could you help me please?
Notice
both radio buttons has TabStop property to True
From How to: Set the Tab Order on Windows Forms (MSDN):
A radio button group has a single tab stop at run time. The selected button (that is, the button with its Checked property set to true) has its TabStop property automatically set to true, while the other buttons have their TabStop property set to false.
In other words, what you're seeing is normal. Those "Yes/No" radio buttons are in the same group, and you can't tab between radio buttons in the same group. As you tab, you'll only focus on the currently selected one, then move to the next control on the form (in your case, a TextBox).
To work around this, you could place each radio button in its own container (such as a Panel), which means you'd have two "groups" each with one radio button. But then you lose the built-in functionality that automatically deselects one radio button when you select the other. Your user will be able to select both radio buttons, so you'd need to add some logic that disables the other. If you decide to try that, experiment with the radio buttons' CheckedChanged or Click / MouseClick events.
As Steve said, and as stated in the answer he linked to, the way it works out-of-the-box is expected behavior for Windows, so think twice before overriding it unless you have a good reason for doing so.
It worked for me!
first you have to create a method like this:
private void TabStopChanged(object sender, EventArgs e)
{
((RadioButton)sender).TabStop = true;
}
and then, put this in your Form_Load event:
private void Form_Load(object sender, EventArgs e)
{
foreach (var item in this.Controls)
{
if (item.GetType() == typeof(RadioButton))
((RadioButton)item).TabStopChanged += new System.EventHandler(TabStopChanged);
}
}
For radio buttons, you don't have to use Tab to navigate. Just use right and left keys to traverse radio buttons.
Check out this link to read more - https://www.csun.edu/universal-design-center/web-accessibility-criteria-tab-order

How to require ComboBox selection?

Working in Windows Forms (C#), creating a Wizard, I'd like to require the user to select an option in a combobox before being allowed to click "Next" to the next page in the form.
I thought I saw where to do this in the past, but I cannot find anything now.
Thx for any help...!
J
There are multiple ways of doing this. And different application use their preferred way.
One way to have an empty or 'Select Value' option at the top of the list of your combo box. Then when the user click the 'Next' button, check whether this is the value which is selected. If so, don't allow to go next. Otherwise allow to proceed.
My way is to set 'SelectedValue' property to -1 (means select nothing) and check whether is it -1 when the user press 'Next'. (If any valid value is selected, then this property should have a value higher than -1.)
Trigger on the selection changed event for the combo box, and then set the button enabled property:
private void comboBoxSelectionChanged(obj sender, EventArgs e)
{
nextButton.enabled = true;
}
There are many ways you can validate the selection ...or force a selection ...2 off the top of my head:
set the combobox to CausesValidation (IIRC) to true and handle xxxValidating(o,e) and xxxValidated(o,e) events
handle Next button's OnClick event and check the combobox SelectedItem or SelectedIndex properties:
/* sudo */
(o, e) => {
if(fooCombo.SelectedIndex == {...}) {
// show dialog, etc.
}
}

Categories

Resources