Allow button click even if validation on another control fails - c#

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.

Related

Cannot set focus on Text Box

I have a form with 4 textboxes. This form is viewed inside a split container panel.
I can enter the values inside the text box but after the value, I click on the text I entered to modify a value but it is not letting me click, there is no cursor coming on the text box, the only thing I can do is backspace or select all and delete.
I cant click anywhere on the middle of the text entered.
If I check the focus I see it is false.
Can someone tell me what could be the problem and how to set the focus to the text box?
To show the form on the panel this is what I am doing
splitContainerControl1.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Both;
splitContainerControl1.Panel2.Controls.Clear();
myform.TopLevel = false;
myform.FormBorderStyle = FormBorderStyle.SizableToolWindow;
splitContainerControl1.Panel2.Controls.Add(myform);
myform.Show();
I have tried the below, but not working, I see focus still false.
myform.Focus();
myform.textbox_latitude.Select();
Thanks,
Try using this.ActiveControl:
this.ActiveControl = textbox_latitude;
Or if you want to focus it on the child form:
myform.ActiveControl = textbox_latitude;

web page doesn't recognize text has been entered in textbox

So I'm setting the value of a textbox, then I'll click a button to submit the search. The value in the textbox is showing what I want, but when I trigger the click of the button, the website gives me an error message on the page as if the textbox value is empty (even though I'm looking at it and it's showing what I wanted).
Now if I manually click on the button, it still thinks the textbox is empty. But if I manually click on the textbox (just click, don't change anything), then manually click on the button, it works.
So apparently there's something happening behind the scenes that requires the textbox to have had the focus before the page will recognize what's been typed in.
private void SetText(string elementID, string text)
{
foreach (HtmlWindow hw in this.webBrowser1.Document.Window.Frames)
{
HtmlElement element = hw.Document.GetElementById(elementID);
if (element != null)
{
element.SetAttribute("value", text);
// here's where I'm trying things
break;
}
}
}
Here's what I've tried (not sure all of these were even valid things, but I tried them anyway:
I've tried setting the focus on the textbox and then removing it by setting it on the button -- element.Focus() and then button.Focus() before clicking the button.
I've tried invoking the changed event -- element.InvokeMember("change"), element.InvokeMember("changed"), element.InvokeMember("onchange"), element.InvokeMember("textchanged").
I've tried invoking the focus event -- element.InvokeMember("focus").
I've tried invoking the lostfocus event -- element.InvokeMember("lostfocus").
I have no idea what it is that the webpage is expecting to be triggered when someone actually types into the textbox, but it's apparently preventing the page from "seeing" what's been placed in there by my code.
Any ideas?
Turns out there were 3 textboxes (each with a slightly different name) that are somehow interconnected. The value of all 3 must be set before the button will work properly. I thought I'd tried that, but perhaps I didn't do all 3 before, or maybe I had them in the wrong order. Ugh.

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

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

Can I fire a Text Changed Event for an asp.net Text Box before it loses focus?

I have an asp.net TextBox in which I want to check if the text entered into the TextBox is > 0. It works once I tab out or click out of the TextBox, but if I keep focus on the TextBox, it won't fire the Text Changed Event, so I have the following scenario, I want to enable something if and only if the TextBox.Text.Length = 0. Now, if I put my caret in the TextBox and delete all the characters and then leave the caret in the TextBox so it still has focus and take my mouse and click a button, it will not do what it was supposed to do because it never fired the Text Changed Event. How would something like this be handled?
friend, keyup, keydown and keypress are your friends
The best idea is to write some client-side javascript to do what you want. The TextChanged event handler requires a postback to the server, and posting back to the server before a text box loses focus is impossible. Unless that is what you intend, I would suggest the former.
you can also use the setInterval javascript method to check for a change in the value of the textbox on a timed basis. just remember, you need to use the form name followed by control name and value to reference the control.
setInterval(MethodName, 100);
function MethodName()
{
if(formname.controlid.value.length > 0)
{
//do something here
{
}

Categories

Resources