Preventing user from uncheking a checkbox - c#

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

Related

Allow button click even if validation on another control fails

I have an interesting problem that I don't know how to solve. I wrote a form which does a password change. The form displays the current password too in a read-only TextBox (not for validation purposes; this isn't important in this case.) Each password TextBox has a button in it that when clicked, masks or unmasks the password (replacing the password characters with bullets and vice versa.) Here's an image of the whole thing:
Notice how the mask/unmask buttons are inside the text boxes, not outside of them. The buttons have been placed inside the text boxes with:
var button = new Button();
button.Width = 20;
button.Cursor = Cursors.Default;
button.FlatStyle = FlatStyle.Flat;
button.Image = SystemIcons.Shield.ToBitmap();
button.Dock = DockStyle.Right;
button.CausesValidation = false;
textBox.Controls.Add(button);
The last TextBox has validation enabled. Now the problem is, that the user is unable to click the passwork unmask button on the other text box, because the validation event fails. Thus, the user is unable to see the current password without entering a new one.
I need a way to have the password mask/unmask button be clickable even if validation is failing in the text box. I can't think of anything. Moving those buttons outside the text boxes is not an option.
The unmask buttons themselves, as well as the parents of the text boxes, all have CausesValidation set to false. Only the text boxes themselves have it set to true.
This is a .NET 2.0 C# project in Visual Studio 2010.
It's not easy to do it with the Validating event of the text box. The click event will not even reach the button when the text box loses focus. I'm thinking that you could either create your own TextBox (by extending TextBox or TextBoxBase) and hack the validation behavior there or override the form's DefWndProc and catch the mouse events + associated info (coordinates) there and still dispatch them (could get ugly) when validation fails.
One easy way out is to not rely on the Validating event anymore. Instead do the validation in the Leave event of the text box and if it fails just mark the text box as such. The user will still see there is a problem.
One more thing you need to take care of is the OK button of the dialog. You need to make sure the user won't be able to close the dialog if there are unvalidated controls on the form. Since you don't have validation support anymore, maybe you can use the Tag property to store a False (for example) when the data is not valid. On OK just iterate over all textboxes and check their tags.
The behavior for the end user will ultimately be the same, you just have to write a bit more code.
Well, I found a way where I can keep using the Validating event and still be able to click the buttons in the other text boxes. It turns out that the Button.MouseUp event is sent even if a button doesn't have focus.
So the solution is to handle both the MouseUp as well as the Click event and perform the password masking/unmasking depending on whether the text box that contains the button currently has keyboard focus or not:
button.MouseUp += (sender, e) =>
{
if (button.Parent.ContainsFocus || e.Button != MouseButtons.Left
|| !button.ClientRectangle.Contains(e.Location))
{
return;
}
textBox.UseSystemPasswordChar = !textBox.UseSystemPasswordChar;
};
button.Click += delegate
{
if (!button.Parent.ContainsFocus) {
return;
}
textBox.UseSystemPasswordChar = !textBox.UseSystemPasswordChar;
};
We do want to handle both events, because if we only handle MouseUp, then clicking the buttons with the keyboard (Tab to switch to the button, and Enter or Space to click it) would not work anymore.

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

WPF TextBox stealing focus after neighboring CheckBox is clicked

I've had an annoying issue with focus for controls on a ToolBar in WPF. My toolbar has a CheckBox and a TextBox control next to eachother. If you click and edit the text in the text box, then click the check box, the text box steals the focus back after clicking the check box, and the check box state is not changed when the user clicks on it.
I have similar issues with text boxes all over my application, but I believe this is the simplest case to explain, and I hope that the problem will be a common issue across all my text boxes.
Does anyone know what might be going on here?
Hook up a handler to the checkbox's Click event (or even the PreviewMouseUp event), and set the event's Handled property to 'true'.
Did you try that?:
private void MyCheckBox_Click(object sender, RoutedEventArgs e)
{
MyCheckBox.Focus();
}

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

Radio Button validation event

I have a simple form with one text box and a panel with three radio buttons in it. I have used a validation event with an error provider to force the user to place a number in the text box. My problem is with the group of radio buttons. The user needs to select a radio button. I found that you can't validate on the panel containing the radio buttons, instead I had to write a validation event for each button. Is there a simple way to make sure that the user has selected a radio button? Thank you.
A simple approach is to simply use this code:
bool selectionMade = radioButton1.Checked || radioButton2.Checked || radioButton3.Checked;
You don't need to put this code in any validation event, you can put it wherever you need to make sure a RadioButton is checked. This can be in the click event for a button that saves the current record, etc. If selectionMode is false, trigger whatever user notifications you need to.
If you really need to use one of the RadioButton's validating event, you could create just one such event and wire all your RadioButtons to use that one event. You can the the event's object sender argument to find out which RadioButton trigger the validating event.
I would use a GroupBox, put your RadioButton controls inside it and use the Validating event on your GroupBox.
You could use a Custom Validator
Specify a ClientValidationFunction with code along the lines of
<script language="javascript">
function ClientValidate(source, arguments)
{
if ($('#button1').checked || $('#button2').checked || $('#button3').checked ) {
arguments.IsValid = true;
} else {
arguments.IsValid = false;
}
}
</script>

Categories

Resources