Tab index is not working on radio buttons - c#

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

Related

How does groupbox.Controls[i] or panel.Controls[i] work?

I am new to C# and was wondering how groupbox.Controls[i] and/oror panel.Controls[i] works?
I have a panel with a bunch of buttons in it to represent a keyboard. I change the color of the keyboard button when the key is pressed.
However, when I do keyboardPanel.Controls[2].BackColor = Color.Red;, my spacebar becomes red.
My spacebar is button55 and at TabIndex 54. Why is it my spacebar that turns red?
The reason that yoor space-bar gets red is simple: keyboardPanel.Controls[2] is the space-bar. please pay attention that the buttons are not necessarily added to the keyboardPanel.Controls list in the order they are named. Meaning: keyboardPanel.Controls[1] is not necessarily button1 and also button55 is not necessarily keyboardPanel.Controls[55].
Now, If you want to extract buttons by name you should use this:
keyboardPanel.Controls.Find("Button55" , true);
where "button55" is the name of that control and true goes for the option to search all the children.
But I think there is a simpler way to change the color. Using the Sender:
private void button_Click(object sender, EventArgs e)
{
Control btn = sender as Control;
btn.BackColor = Color.Red;
}
and make this method as the event handler of all the buttons' click event.
EDIT:
If you really want to re-arrange the controls in the GroupBox you should visit the the designer. The simple way to get to the designer is to right-click on InitializeComponent() in the constructor method of your form and choose Go To Definition.
There you will find the order that controls are added. something like below:
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button3);
this.groupBox1.Controls.Add(this.button1);
I my case Controls[0] is button2, Controls[1] is button3 and so on. You can re-arrange them like below:
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button3);

how to make a tabpage inaccessible in tabcontrol

I currently have a tabcontrol with 3 tabpages (lets call them A,B, and C) the thing is I want the user to only be able to click certian tabs (if on tabA only can navigate to tabB, if on tabC only can navigate to tabA...) is there a way to do this? I'm a bit stumped, any help is appreciated thanks!
--C#2.0
--Windows Visual Studio 2005
You could hook up to the Selecting event on the TabControl and inside the event handler, you could check some class variable specifying which tab(s) are allowed to be clicked. If the one you're selecting doesn't match the variable, you can cancel the event.
In order to control which TabPages you can navigate to at a time, you can use the Enabled property on the TabPage. Set it to false in order to prevent any user from being able to interact with it.
In order to dynamically decide which tabs are enabled based on what tab is open you can use the Selected event on the TabControl (detailed here: http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.selected.aspx). This will fire whenever you change the current tab on the TabControl. In here, you can determine what the current TabPage is and then use that to enable or disable TabPages as appropriate.
Add a handler to the TabControl.Selecting event to check whether you want to allow the tabpage selection.
Maybe something like
If (SelectedIndex == 1) //tab a
{
tabC.enabled = false;
tabB.enabled = true;
}

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.
}
}

Preventing user from uncheking a checkbox

I have 3 checkboxes in my winforms program. I managed to make it somehow that only one of them can be selected by user. That is if user clicks one of the unchecked buttons, ofcourse that button will be checked and also the check will be removed from last checked button!
Now I want to do it somehow that user can not uncheck the checkboxes, so the only way to checkk a box will be clicking on it. is this possible? is there any property for this?
Sorry for using too much check & box :P
In the checkedchanged event of the checkbox write the following code.
if (!checkBox1.Checked)
{
checkBox1.Checked = true;
}
If you want a radio button functionality but a different look, change the appearance of a radio button.
From http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton(v=vs.80).asp
private void InitializeMyRadioButton()
{
// Create and initialize a new RadioButton.
RadioButton radioButton1 = new RadioButton();
// Make the radio button control appear as a toggle button.
radioButton1.Appearance = Appearance.Button;
// Turn off the update of the display on the click of the control.
radioButton1.AutoCheck = false;
// Add the radio button to the form.
Controls.Add(radioButton1);
}
Just listen for change events and if the event tells you the checkbox has been unchecked, recheck it.
But I agree with others, this behavior is the one of RadioButtons, so use a radio button instead. You don't want to suit your personal feeling but to provide a unified user experience to the end user. That's part of the guidelines of Microsoft (and every other framework).

Detect if mouse click hits a item not in listbox [C#]

If the user click on a item in the listbox, the listboxItems_SelectedIndexChanged is called. But, even if the user miss an item and randomly clicks inside the listbox (not on items) the listboxItems_SelectedIndexChanged is still called.
How can I change this? I only want action on item click.
Note: removing the ability to navigate the application with keyboard is not a option.
I guess that in some cases you don't have enough list items in your control, therefore you have some space that you can click on and then SelectedIndexChanged is fired.
I guess you cannot dynamically resize the control to always fit the number of list items or else you wouldn't be asking this question.
Now, what should happen when the user click (selects) the same list item? Should some logic happen even though the selected index is the same (so when it was clicked the first time the same logic happend)?
If you require that selecting the same index more than once should be ignored then you could use the following hack:
Keep a variable at the form scope (the form containing the listbox control) and each time the selection index changes set that variable. Then use it later to check if the same selection has been made to ignore handling the event. Here is an example:
private int _currSelIdx = -1; // Default value for the selected index when no selection
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == _currSelIdx)
return;
Console.WriteLine(listBox1.SelectedIndex);
_currSelIdx = listBox1.SelectedIndex;
}
It ain't pretty, but hey...whatever works!
Maybe SelectedIndexChanged is not the right place to put your logic, since it is triggered even when you change the selection with the keyboard.
I would use MouseClick instead, checking if the click occurred over the selected item, i.e. something like this:
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
if (listBox1.SelectedIndex < 0 || !listBox1.GetItemRectangle(listBox1.SelectedIndex).Contains(e.Location))
MessageBox.Show("no click");
else
MessageBox.Show("click on item " + listBox1.SelectedIndex.ToString());
}
This link may help, instead of double click, implement the same for single click
i want to detect an item double click in a winforms listbox control. [how to handle click on blank area?]

Categories

Resources