473,382 Members | 1,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Give RadioButton focus without selecting?

I know this is a bit of nonstandard behavior, but I would like the
Enter key to move focus through a group of radiobuttons without
selecting them. The user would then have to use Space or actually
click to select one of the radiobuttons to select.

The default behavior of Focus() seems to also select the radiobutton,
but I can't seem to find what is causing this to happen.

I would like very much to seperate the Focus and the selection. Any
ideas on how to accomplish this?

Thanks much,
Matt

May 31 '07 #1
5 11594
On Thu, 31 May 2007 11:42:35 -0700, Matt B <ro**********@gmail.comwrote:
I know this is a bit of nonstandard behavior, but I would like the
Enter key to move focus through a group of radiobuttons without
selecting them. The user would then have to use Space or actually
click to select one of the radiobuttons to select.

The default behavior of Focus() seems to also select the radiobutton,
but I can't seem to find what is causing this to happen.

I would like very much to seperate the Focus and the selection. Any
ideas on how to accomplish this?
If I understand the question correctly, you want the Enter key to behave
like the Tab or arrow keys, rather than like the Space key?

If so, you can special-case the Enter key in the appropriate control's
ProcessDialogKey() method. I haven't experimented with the radio-group
question you're specifically asking about, but it seems likely to me that
either the form, or possibly the group box containing the radio buttons,
will need to override this method. If the form, then you already have a
derived class in which you can put the override. If the group box, then
you will need to create your own derived class and then use that in your
form.

In the override, you can either handle the behavior explicitly (changing
focus rather than changing the state of the button), or perhaps better
would be to map the Enter key to the Tab key by passing Keys.Tab to the
base ProcessDialogKey() method when the Enter key is passed to your method.

As you note, this *is* non-standard behavior, and you should definitely
consider carefully whether your perceived user benefit really outweighs
the potential problems caused by changing the behavior of the keys in an
otherwise-standard UI.

Pete
May 31 '07 #2
On Thu, 31 May 2007 11:42:35 -0700, Matt B <ro**********@gmail.comwrote:
[...]
I would like very much to seperate the Focus and the selection. Any
ideas on how to accomplish this?
Sorry for my previous post. I had a few moments and decided to look at
the exact behavior of the radio group, and realized (as you probably
already knew) that the Tab and arrow keys don't really do what you're
asking for.

In playing with it, I found a couple of things:

* The basic idea of overriding the ProcessDialogKey() method to handle
the Enter key is sound (though, make sure you properly distinguish between
using the Enter key and the Return key...there are two different key
codes, corresponding to two different keys on standard keyboard).

* One thing you'll run into is that RadioButton controls are added to
the container's Controls property in reverse order from their tab order.
I had to set focus to the *previous* item in the container in order for
the *next* one in tab order to be selected.

* Simply changing focus is enough to get the state of the RadioButtons
to change. Unfortunately this is contrary and key to your desired
behavior. I haven't had enough time to look into where this happens or
how one might change it, but I'm curious so who knows...maybe I'll play
around with it one day. :) Of course, since this is pretty much the crux
of your question, I probably have offered nothing useful to this thread.
Sorry. :)

Anyway, on the off-chance this is useful, here's the ProcessDialogKey()
method I wrote that at least deals with handling the focus changing (in my
case, I had a "groupBox1" that contained the radio group...you can
generalize the code further if necessary, of course):

protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Return)
{
int ictl = groupBox1.Controls.IndexOf(ActiveControl);

if (ictl != -1)
{
groupBox1.Controls[(ictl + groupBox1.Controls.Count-
1) % groupBox1.Controls.Count].Focus();
}
}

return base.ProcessDialogKey(keyData);
}

Pete
May 31 '07 #3
On Thu, 31 May 2007 13:06:36 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
[...]
* Simply changing focus is enough to get the state of the
RadioButtons to change. Unfortunately this is contrary and key to your
desired behavior. I haven't had enough time to look into where this
happens or how one might change it, but I'm curious so who knows...maybe
I'll play around with it one day. :) Of course, since this is pretty
much the crux of your question, I probably have offered nothing useful
to this thread. Sorry. :)
For what it's worth, I did realize that you can set the
RadioButton.AutoCheck property to false to disable the auto-checking
behavior. However, if you do this, you'll have to explicitly handle all
of the related behavior, including treating a group of radio buttons as a
group.

This shouldn't be *too* hard, but there will be lots of little details to
make sure you've covered. IMHO, maybe a good enough reason to be happy
with the built-in, default behavior. :)

Pete
May 31 '07 #4
On Thu, 31 May 2007 14:24:33 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
For what it's worth, I did realize that you can set the
RadioButton.AutoCheck property to false to disable the auto-checking
behavior. However, if you do this, you'll have to explicitly handle all
of the related behavior, including treating a group of radio buttons as
a group.
I've got to say, I cannot explain why this question intrigues me so much..
I really ought to be doing other things, but I can't stop playing with it.

Anyway...see below for some code that does what I think you're asking
for. Some notes:

0) The code assumes that you have only radio buttons within the
containing parent. In this case a GroupBox named "groupBox1". If you
wanted to generalize the code, you would need to change the code that
figures out the next control in the tab order to deal with the possibility
of non-RadioButton controls in the group, and/or groups of controls for
which you don't want the Return key to behave this way.

1) The Click handler is required because for this to work you need to
disable the default functionality by setting the AutoCheck property of
each RadioButton instance to false. With that disabled, you need to
handle the checking behavior yourself.

2) I find the need to use a flag to disable behavior in the Click
handler to be a bit kludgy. But since it appears to me that the Click
event is auto-generated in response to a Control getting the focus, I
don't see any immediate way around that.

3) For some reason, the focus rectangle is not displayed until you
first use the Tab key to move focus to the group. However, this is the
default behavior even when AutoCheck is true, so at least my code isn't at
fault for that. But it does produce a kind of strange situation in which
you use the Return key to change the focus, but there's no UI feedback
indicating that. Normally it wouldn't be a problem because the checked
state of the buttons would change, providing that feedback. You will
probably want to figure out if there's a way to make the focus rectangle
show up. I presume the basic mechanism to do that is simple, though I
don't know what the magic incantation is off the top of my head. :)

On this point #3, I found a thread on MSDN that suggests that this is not
the only place one runs into this strange behavior. It appears that the
window controls (either in .NET or, more likely, Windows itself) have a
general problem that until you switch focus using the Tab key, the focus
rectangle is not shown. I think there may be a way to change the behavior
if you make a new radio button class that derives from RadioButton, and
then either set the ShowFocusCues property to "true" or provide a way to
call the OnChangeUICues() method to explicitly have the focus rectangle
shown. But in the interest of not investing even more time in someone
else's attempt to bypass the standard UI behavior :), I've decided to not
bother trying that. :)

4) I make no claims that I have in this code handled all of the subtle
issues that may exist with respect to the selection and focus-changing.
It *seems* to work fine for me, but I easily could have missed something..
The fact that I could have is one of the reasons I try to avoid doing this
sort of "change the standard UI to be non-standard" thing.

Here's the code I came up with (I promise I'm done fiddling with this
now...sorry for all the different replies :) ):

private bool _fEnterKey;

protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Return)
{
int ictl = groupBox1.Controls.IndexOf(ActiveControl);

if (ictl != -1)
{
Control ctlFocus = groupBox1.Controls[(ictl
+ groupBox1.Controls.Count - 1) % groupBox1.Controls.Count];

_fEnterKey = true;
ActiveControl.TabStop = false;
ctlFocus.TabStop = true;
ctlFocus.Focus();
_fEnterKey = false;
}
}

return base.ProcessDialogKey(keyData);
}

private void radioButton1_Click(object sender, EventArgs e)
{
GroupBox grp = (GroupBox)((Control)sender).Parent;

if (!_fEnterKey && grp != null)
{
foreach (RadioButton radio in grp.Controls)
{
if (radio != sender)
{
radio.TabStop = false;
radio.Checked = false;
}
}

((Control)sender).TabStop = true;
((RadioButton)sender).Checked = true;
}
}
May 31 '07 #5
On May 31, 3:30 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Thu, 31 May 2007 14:24:33 -0700, Peter Duniho

<NpOeStPe...@nnowslpianmk.comwrote:
For what it's worth, I did realize that you can set the
RadioButton.AutoCheck property to false to disable the auto-checking
behavior. However, if you do this, you'll have to explicitly handle all
of the related behavior, including treating a group of radio buttons as
a group.

I've got to say, I cannot explain why this question intrigues me so much.
I really ought to be doing other things, but I can't stop playing with it.

Anyway...see below for some code that does what I think you're asking
for. Some notes:

0) The code assumes that you have only radio buttons within the
containing parent. In this case a GroupBox named "groupBox1". If you
wanted to generalize the code, you would need to change the code that
figures out the next control in the tab order to deal with the possibility
of non-RadioButton controls in the group, and/or groups of controls for
which you don't want the Return key to behave this way.

1) The Click handler is required because for this to work you need to
disable the default functionality by setting the AutoCheck property of
each RadioButton instance to false. With that disabled, you need to
handle the checking behavior yourself.

2) I find the need to use a flag to disable behavior in the Click
handler to be a bit kludgy. But since it appears to me that the Click
event is auto-generated in response to a Control getting the focus, I
don't see any immediate way around that.

3) For some reason, the focus rectangle is not displayed until you
first use the Tab key to move focus to the group. However, this is the
default behavior even when AutoCheck is true, so at least my code isn't at
fault for that. But it does produce a kind of strange situation in which
you use the Return key to change the focus, but there's no UI feedback
indicating that. Normally it wouldn't be a problem because the checked
state of the buttons would change, providing that feedback. You will
probably want to figure out if there's a way to make the focus rectangle
show up. I presume the basic mechanism to do that is simple, though I
don't know what the magic incantation is off the top of my head. :)

On this point #3, I found a thread on MSDN that suggests that this is not
the only place one runs into this strange behavior. It appears that the
window controls (either in .NET or, more likely, Windows itself) have a
general problem that until you switch focus using the Tab key, the focus
rectangle is not shown. I think there may be a way to change the behavior
if you make a new radio button class that derives from RadioButton, and
then either set the ShowFocusCues property to "true" or provide a way to
call the OnChangeUICues() method to explicitly have the focus rectangle
shown. But in the interest of not investing even more time in someone
else's attempt to bypass the standard UI behavior :), I've decided to not
bother trying that. :)

4) I make no claims that I have in this code handled all of the subtle
issues that may exist with respect to the selection and focus-changing.
It *seems* to work fine for me, but I easily could have missed something.
The fact that I could have is one of the reasons I try to avoid doing this
sort of "change the standard UI to be non-standard" thing.

Here's the code I came up with (I promise I'm done fiddling with this
now...sorry for all the different replies :) ):

private bool _fEnterKey;

protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Return)
{
int ictl = groupBox1.Controls.IndexOf(ActiveControl);

if (ictl != -1)
{
Control ctlFocus = groupBox1.Controls[(ictl
+ groupBox1.Controls.Count - 1) % groupBox1.Controls.Count];

_fEnterKey = true;
ActiveControl.TabStop = false;
ctlFocus.TabStop = true;
ctlFocus.Focus();
_fEnterKey = false;
}
}

return base.ProcessDialogKey(keyData);
}

private void radioButton1_Click(object sender, EventArgs e)
{
GroupBox grp = (GroupBox)((Control)sender).Parent;

if (!_fEnterKey && grp != null)
{
foreach (RadioButton radio in grp.Controls)
{
if (radio != sender)
{
radio.TabStop = false;
radio.Checked = false;
}
}

((Control)sender).TabStop = true;
((RadioButton)sender).Checked = true;
}
}

Thanks a lot for the responses Peter! It seems like we both had very
similar ideas on how to get around this. What I've ended up doing is
setting AutoCheck to false and then handling all of the behavior of
the group myself. This seems to work just fine.

FYI, the users are used to this behavior already, this is just a re-
write. The questions they are answering are very sensitive and we
don't want the program answering these questions by default with an
accidental keystroke.

Jun 1 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: francisco lopez | last post by:
ok , first of all sorry if my english is not so good, I do my best. here is my problem: I donīt know much javascript so I wrote a very simple one to validate a form I have on my webpage. ...
3
by: Alpha | last post by:
I have 3 radio buttons for include, exclued or 'select all' from the listbox items. If a user selects the 'Select All' button' then all items in listbox is hi-lited as selected. Now, when user...
2
by: Brano | last post by:
HI all, I have two radioButtons on my form. They both have autopostback = true and they are both in the same group. They both have event handlers for CheckedChanged event. My problem is ont of...
3
by: jahuer1 | last post by:
On my Form I have several Panels with several RadioButtons on it. When I start the Form, on each Panel a RadioButton should be selected if it was selected when I called the Form some time ago. ...
3
by: LCAdeveloper | last post by:
Help! A trawl through the archives couldn't shed any light on this, so is there a way to handle DoubleClick events for RadioButtons in vb.NET? I'm recoding a VB4 application, which used the...
2
by: Rich | last post by:
Hello, Is it possible to assign a value to a groupbox by selecting a radiobutton contained in the groupbox? Say you assign a value of 1 to radbtn1, 2 for radbtn2, 3 for radbtn3 all contained...
1
by: osmarjunior | last post by:
I override the OnShown event of a form and call both methods Select() and Focus() of a RadioButton. The RadioButton is selected, but the focus rect is not visible. If I press Tab key to exit the...
0
by: BizEd | last post by:
I have a textbox that fires an autopostback when filled in. The next field after the textbox is a RadioButtonList which I set focus to. The radiobutton list does have focus and activates when you...
5
by: andersond | last post by:
I have a screen that begins with a radiobutton. For some reason it does not accept focus until the tab key is pressed. I use a js line to assign focus to the radiobutton but the radiobutton does...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.