I have a WinForm with 3 group boxes, one with combo boxes, and two with radio buttons. I set all of them and their children controls to "TabStop = false", but when I cycle with TAB, the currently selected radio button in each of the last two group boxes gets focused.
If there's no way to change this behavior, what would be a good event to catch and move the focus away? I can't find an "OnFocus" event.
The solution is to set one method (code below) to handle the "Enter" event of every radio button in the form (if that's what you wish).
Actually, I only did it for the radio buttons of the first group box and it worked, the second group box's radio buttons don't get focus, even though their "Enter" events are not handled. This is not the behavior you would have expected.
private void radiobuttonXGroup1_Enter(object sender, EventArgs e)
{
SomeOtherControl.Focus();
}
In the *.Designer.cs file you edit every Enter event (for each radio button) to point to one event handler (the above method).
this.radiobutton1Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
this.radiobutton2Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
this.radiobutton3Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
Setting the TabStop to False on a RadioButton to prevent tabbing to the control works until you actully select the radio button without any additional overrides like suggested by #msergeant.
EDIT
The following code prevents the code from getting a tab key event:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
radioButton1.TabStop = false;
}
Radio buttons behave differently with respect to Tab from other controls in that they work in sets or groups based on setting the tab index or placing then radio buttons in a group box.
The MSDN documentation for RadioButton.TabStop states "This API supports the .NET Framework infrastructure and is not intended to be used directly from your code". Which basically means, "This isn't going to work how you expect it to".
With that said, the Enter event will fire when the button receives the focus. You can try to use that to move focus to another control.
Related
My combobox allows typing into the text portion so the only way to get the dropdown list is to click the button. However, since this will be used on touchscreen devices, it is hard to click it when it is this 'thin'.
Is there any way to increase the width of the combobox button?
One alternative to resizing the button would be to set the DroppedDown property to true inside the Click event. This will show the drop down list when the user clicks inside the edit area of the ComboBox, effectively extending the button area to the whole control:
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox1.DroppedDown = true;
}
If you want to customize dropdown button (the size of the arrow and the size of the button are completely in our control), there is a class called ComboBoxRenderer.
Here you have complete example.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.comboboxrenderer?view=netframework-4.7.2
It is supported from .Net2.0.
You should create a custom ComboBox control and call DrawDropDownButton of the comboboxrenderer in the paint event.
In the above link(example) arrowSize and arrowRectangle are two variables which helps in deciding your dropdown button size, along with below static function (of ComboBoxRenderer) call in overridden paint event.
ComboBoxRenderer.DrawDropDownButton(e.Graphics, arrowRectangle,arrowState);
I'm a beginner in C# language, so I need some help from the geniuses with this scheme: I need to add a radio button for a menu strip. I've already changed the, CheckOnClick property to true, but I need an option for radio button selection. You can see it from the Windows calculator menu bar (click View).
How can I get to it via the MenuStrip property?
I know this is a near-ancient post, but I thought it worth mentioning that although there's no native support for a RadioButton MenueItem, it's easy enough to coax their checkboxes into behaving that way. Start by setting the CheckOnClick property of each MenueItem to FALSE. Then apply the same MouseDown event handler to each item:
private void ToolStripMenueItem_MouseDown(object sender, MouseEventArgs e)
{
var thisTsmi = (ToolStripMenuItem)sender;
foreach (ToolStripMenuItem tsmi in thisTsmi.GetCurrentParent().Items)
{
tsmi.Checked = thisTsmi == tsmi;
}
}
You could use the Click event instead, but I prefer MouseDown because it provides some visualization to the user that the checked item has changed while leaving the Click event open for coding the individual items if needed.
If you navigate to
msdn.microsoft.com/en-us/library/ms404318.aspx
you will see how it's done ;)!
I'm having a Picture box in a user control window(Windows custom control library). and some functionality in the Form's Enter event and leave event.
Now my sample application is having two instances of the control. So when i run my sample application the fist control got selected and the enter event is triggered, and when i select the second control the first's leave and second's enter events are getting triggered.
Now, problem is that when i select(click) the second control's picturebox, the events are not triggering, i.e the control form is not getting the event.
So if i click whereever in the control(in the picturebox or in the control) the enter event should be triggered.
How to do this?
A picture box can't get focus. So clicking on it won't take the focus away from the previous control thus not triggering the events.
You need to add a click handler on the picture box in which you manually give focus to the associated focusable control.
private void PictureBox_Click(object sender, EventArgs e)
{
focusableControl.Focus();
}
I'm a beginner in C# language, so I need some help from the geniuses with this scheme: I need to add a radio button for a menu strip. I've already changed the, CheckOnClick property to true, but I need an option for radio button selection. You can see it from the Windows calculator menu bar (click View).
How can I get to it via the MenuStrip property?
I know this is a near-ancient post, but I thought it worth mentioning that although there's no native support for a RadioButton MenueItem, it's easy enough to coax their checkboxes into behaving that way. Start by setting the CheckOnClick property of each MenueItem to FALSE. Then apply the same MouseDown event handler to each item:
private void ToolStripMenueItem_MouseDown(object sender, MouseEventArgs e)
{
var thisTsmi = (ToolStripMenuItem)sender;
foreach (ToolStripMenuItem tsmi in thisTsmi.GetCurrentParent().Items)
{
tsmi.Checked = thisTsmi == tsmi;
}
}
You could use the Click event instead, but I prefer MouseDown because it provides some visualization to the user that the checked item has changed while leaving the Click event open for coding the individual items if needed.
If you navigate to
msdn.microsoft.com/en-us/library/ms404318.aspx
you will see how it's done ;)!
I have an application where I am trying to mimic the "soft description" textboxes like those found for the tags and title locations on this site.
The way I've done this is essentially to create my textbox, and depending on what happens when the mouse pointer enters or leaves the control, it updates the content of the textbox to get the effect.
The problem is what when my form is first shown, the mouse cursor immediately jumps into the first textbox, which removes the title telling the user what the textbox is for.
If I turn off AcceptTab on the textbox, then everything works as expected, but the user loses the ability to tab into the textbox.
Is there a way to turn off this automatic selection of the textbox?
Could you this.Focus() on the form itself, or on some label control?
Bit late but a perfect solution is to select the form on load of form.
Adding this line to the constructor will give the expecting result.
this.Select();
But while using multi thread controls like OpenFileDialog if u want to unfocus/deselect text-box this.Select() was not working so I selected a button in the form using.
button1.Select();
The TabIndex property controls what order things will tab in, and on load, focus goes to the first control (ordered by TabIndex) that has AcceptTab as true. You can change the ordering so that the control that you want the user focus to start in is lowest (and have tabs work cycle through controls as you'd expect).
Alternatively, as Jason suggested, you could simply call Focus() on whatever control or the form itself in the FormLoad event.
I used a variant on Jason's technique. First, I created a dummy textbox with tabindex 0. That way, when the form is shown, that textbox will be selected. Next, I made the dummy textbox have zero width, so that it has no visible component.
However, once the form is loaded, I don't want the user to be able to tab over to the "nonexistant" textbox. Therefore, I added these two bits:
//These functions prevent the textboxes from being implicitly selected.
private void dummyBox_Leave(object sender, EventArgs e)
{
dummyBox.TabStop = false;
}
private void Main_Enter(object sender, EventArgs e)
{
dummyBox.TabStop = true;
dummyBox.Select();
}
Where Main is the name of my form.
Hope this helps someone.
Billy3