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();
}
Related
The title says it all: "How do I prevent TextBox or RichEditBox from losing focus after clicking on a disabled button?"
I tried setting AllowFocusOnInteraction to false, but that only works when the button is enabled. That property works well if, say, I have a button for setting text to bold or italic. The editor will not lose focus and everything works superb. But if I were to disable that button and then click on it, the editor loses focus. This causes several new issues for me. Would love some help with this. Thanks.
Also note that I have a UWP app. Not sure if it matters, though.
Okay, so I figured out the CORRECT way to fix the issue.
So the bold, italic, and underline format buttons are all within my custom toolbar. What I had to remember is that most xaml elements can be clicked on and therefore trigger a PointerPressed event so long as the element (a Grid element for example) has a background for the click to intercept. If the background is transparent the PointerPressed event will not fire.
Sooo, I made sure my toolbar had a solid background set and then I set a PointerPressed event on it. So whenever you click on the toolbar that event will fire and I simply set the e.Handled property to true;
So now because the button within the toolbar is disabled the pointer click will go to the next clickable element within the visual tree that is underneath the button that you clicked, which is my toolbar (which now has a background). When the toolbar is reached, the e.Handled event being set to true within the event handler will tell the system to do nothing further and so the RichEditBox retains its focus.
I know my writing here is very sloppy but I am sort of in a rush right now and so I will most likely come back and clean my answer up. Hope this helps someone.
wrap the disabled button in a Border or a Grid.
<Grid Tapped="DisabledButtonTapped">
<Button IsEnabled="false"/>
</Grid>
now with help of tapped method on this grid you can set focus back to your RichEditBox.
private void DisabledButtonTapped(object sender, object args)
{
MyRichEditBox.Focus(FocusState.Programmatic);.//use the x:Name of your richeditbox in place of "MyRichEditBox".
}
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.
I have a problem .. I have an error list form (works as validation summary screen) that displays validation of controls that require to save data but have no values.
This form opened when validation occurs on controls in another form that has tab control contains all controls that have validation.
The problem is when I double click on Error List form, I need cursor focus on tab control that have this control and focus on the control itself
The result : focus happened on tab control only .. but I need to focus on the control also
Use Control.Focus() in your tab selected event handler.
Call Focus() to focus on the next control.
Step 1 : You need to handle the Enter event of the TabPage Control to perform the operations when TabPage gains the focus.
Step 2: You can call Select() function on Required control to gain the Focus.
Try This: if you want to gain the Focus of TextBox control in TabPage2 use this code
tabPage2.Enter += new System.EventHandler(this.tabPage2_Enter);
private void tabPage2_Enter(object sender, EventArgs e)
{
textBox1.Select();
}
I think the trick is to set socus on the tab page first, then set focus on the actual control you want to focus on.
What I was seeing is if the tab page was already selected setting focus to the control works fine. However, if the tab was programmatically activated then setting focus on the control alone does not work.
So this works for me reliably:
// first select and focus the tab
TabsResult.SelectedTab = tabRequest;
TabsResult.SelectedTab.Focus();
// then focus the control
txtRequestUrl.Focus();
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).
How to change dropdown button in a ComboBox control (C#, Windows Forms)? I have a custom button, and I want to use it in the ComboBox instead of the default dropdown button.
I think Hans Passant solution is the way...
From here:
http://social.msdn.microsoft.com/forums/en-US/winformsdesigner/thread/5d65f987-834c-465f-a944-622831d4cfb0
You can create a UserControl, drag a
ComboBox and a Button onto it, make
the Button right over the ComboBox's
arrow button to make the arrow button
invisible, handle the Button's Paint
event to draw an arrow on it, this can
be done by calling
ComboBoxRenderer.DrawDropDownButton()
method (Notice: this method has a
limit, it needs the visual style being
enabled on the OS) or by drawing an
icon on it, or just drawing a small
triangle on it.
Then handle the Click event of the button to show the ComboBox's
DropDown, this can be done by
something like this
private void button1_Click(object sender, EventArgs e)
{
this.comboBox1.DroppedDown = true;
}