c# WinForms TextBox_Leave - c#

I have a small problem. I have a TextBox myTxt. I set the myTxt_Leave event to check the database if input value already exists. If value exists, I display a MessageBox and set myTxt.Focus(). That wont let user to leave this TextBox, until another value (which does not exist in the database) is input.
But here is the catch...I would like to allow user to click on Cancel button on the form even if the value does not change. Is that possible?
Hope someone understands what I wanted to tell.
Thank you for any help.
EDIT:
I decided to change the interface a little. I put red label under the textbox "Value already exists" and disabled the Submit button until the value is correct.
But still, I'm curious if solution for my previous problem is possible?

Instead of binding the event to the Leave interaction, just Highlight the box in red and disable the submit. When the when the error is resolved or the action is cancelled, clear the red / disabled submit button.

Related

Textbox leave event unless tab changes

Using c# winforms vs2008
I'm using a TabControl on a form. Each tab has a different set of input fields. Some of those fields has to be completed. Such as Username. Multiple users will use the same PC, so a the username must remain static. I have a leave event on the require fields which triggers when the textbox loses focus to check if a value was added. In the case of Username, a popup would then presents possible values. That works pretty awesome to ensure accuracy and speed. To problem comes in when the user clicks on another tab, the textbox leave event triggers. How can I prevent the leave_event code from running if the focus changes to a control not on the current Tab?
I have tried different event handlers, but the leave event seem to occur first. I tried textbox validating event handler, but also no success. I tried adding a If-statement in front of the code to check the tab number, or tabcontrol state - no joy - as before anything else changes, the leave event fires.
The only remaining thing I can think of is to loop through all the controls on the tab to check if they have focus, but that just feels messy.
Any other suggestions?
I have found a partial solution. Using a Mouse_Enter and _Leave event on the tab, I set a flag to determine whether the mouse was clicked in the form or outside. If the flag is set to false, the leave event dont run. This will not solve short cut key presses though. But that I can handle later.

How To Enter A TextBox On Click

Im trying to create a basic example sign up page.
I want the mouse pointer to enter the textbox only on click however it seems to enter the first textbox by default. What is the process to change this?
You could use the opacity and sets it to 0 together with the IsEnabled attribute which I would set to false.
Then you put in your click event this simple code.
textbox.Opacity="1";
textbox.IsEnabled="true";
I hope this helps you.

what is happening when by default when I databind to windows form control.

So I was asked to fix an issue with an old windows form utility that has been around a little while (least before any of my coworkers showed up). The form has a numericUpDown control that is databound. The issue was, when you clicked the up or down arrow the values would change and save OK however, if you just typed in a number and clicked save it wouldn't save. It was like the databinding never saw the change, so coming from a WPF background I guessed that changing the following
TaskDaysToComplete.DataBindings.Add("Value", taskTemplate, "DaysToComplete");
To this
TaskDaysToComplete.DataBindings.Add("Value", taskTemplate, "DaysToComplete", false, DataSourceUpdateMode.OnPropertyChanged);
would solve my problem and it did. You can now either type in a number or use the up/down arrows on the control to set the "Value" property.
My question is this, what was happening in the first place? I am guessing the default DataSourceUpdateMode was OnValidation but when does this happen and why was it OK for when using the up/down arrows but never seemed to happen when typing things in.
Thanks!
numericUD validation
validation is done on losing focus, so when you press the up/down key the textbox loses focus - triggering the validation routine.
when editing text you can make the control lose focus by clicking another control, this will cause it to validate.
the reason that the default is set to onValidate is that on value changed will cause it to validate on each character typed, which can be problematic both for performance and for correct validation.

Cancel checkbox checking, when input not corret

(Using WPF application)
This is the situation:
There are 3 user input fields. 1 for hours, 1 for minutes and 1 for seconds.
Behind it is a checkbox for "divide time by half" that does nothing more than devide the timespan created by the user input by half.
But when the checkbox gets checked, i first make a check in the code if the user had put in any numbers in the fields: hours/minutes/seconds.
If all are 0, a messagebox pops up telling the user he needs to put in something.
But at this point, i want to "cancel" the checking of the checkbox.
Should i use something as e.Cancel or something ?
Or should i set the IsChecked back to false together with the messagebox ?
Im wondering wat the correct way of dealing with this is.
I think your approach is wrong.
The checkbox should enable only when there is valid input.
You could set a LostFocus event on your textboxes and inside that event check the all the input values.
If input is right then enable the checkbox else disable it. No need to cancel the checkmark
The action of clicking the checkbox has a two step behaviour:
Validating the time
Dividing it by half
If you want to maintain those two step in an unique operation the action of checking the checkbox in fact triggers the event, the validation step is performed, then you uncheck it. I think that from this perspective it is more correct to set checkbox.IsChecked to false.
As pointed out by Steve I would separate the validation step from the operation. Disable the checkbox when the input is not valid putting a check function on the TextChanged event of the textboxes. If everything is fine you can enable the CheckBox. If not disable it and uncheck it if it is checked.

C# Auto Clearing Winform Textbox

I have a user that want to be able to select a textbox and have the current text selected so that he doesn't have to highlight it all in order to change the contents.
The contents need to be handle when enter is pushed. That part I think I have figured out but any suggestions would be welcome.
The part I need help with is that once enter has been pushed, any entry into the textbox should clear the contents again.
Edit: The textbox controls an piece of RF hardware. What the user wants to be able to do is enter a setting and press enter. The setting is sent to the hardware. Without doing anything else the user wants to be able to type in a new setting and press enter again.
Hook into the KeyPress event on the TextBox, and when it encounters the Enter key, run your hardware setting code, and then highlight the full text of the textbox again (see below) - Windows will take care of clearing the text with the next keystroke for you.
TextBox1.Select(0, TextBox1.Text.Length);
OK, are you sure that is wise? I am picturing two scenarios here:
There is a default button on the form, which is "clicked" when enter is pushed".
There is no default button, and you want the user to have to press enter, regardless.
Both of these raise the same questions:
Is there any validation that is taking place on the text?
Why not create a user control to encapsulate this logic?
If you know the enter button is being pushed and consumed fine, how are you having problems with TextBoxName.Text = string.Empty ?
Also, as a polite note, can you please try and break up your question a bit? One big block is a bit of a pain to read..

Categories

Resources