Better to use Validating or Leave event to validate textbox data? - vb.net

When coding validation logic for a VB .NET textbox, which event handler is better to use: Validating or Leave?
From what I understand, they both occur at the same time. However, according to this article: MSDN: Control.Leave Event, the Leave event occurs right before the validating event. This would initially make me think I would rather use the Leave event, as it occurs first.
However, for code readability, it would make sense to place all validation code in the Validating event.
So, which is the better option, in terms of both efficiency and industry-standards?

You should always use the Validating event, it was made to support validation. If not to prevent the focus change then at least for the CausesValidation property. Which you set to False on, say, the Cancel button of a dialog. No point in validating anything when the user decides to dismiss the dialog.

The Validating event is designed for validation. If the text isn't valid, set e.Cancel = True, and focus stays on the text box. Leave is just a notification.

Related

Is there an equivalent to an "On Record Changed" event for Access?

I am trying to set permissions for my form fields (continuous form) and permissions need to be re-evaluated every time a different record gains focus/selection. Right now the only way I've found is to have an OnFocus event for each editable control, but there's gotta be a better way...
I've already tried MouseMove, OnClick, etc. but they don't seem to work when clicking/moving from a control in one record to the next without clicking over empty space first. Also MouseMove seems to have a limit to how frequently it responds.
I would also appreciate something equivalent to an "On Focus Changed" event, if "On Record Changed" is not possible.
Try OnCurrent, or maybe OnDirty, and can also try OnAfterUpdate.
I usually put stops in all of the candidate events when I'm not sure which one to use. There are differences, but running your application and seeing when the events fire can help you decide which one to use.
The event you are looking for is : Form_Current

Reset focus to control with lowest tab index

I have a form that is re-used. That is, instead of creating a new instance of the form each time, the form is kept hidden, and is made visible when needed. (A design I inherited; I presume this was a performance optimization.)
The problem: The second time that the form is used, the focus is on the OK or Cancel button, from the first use of the form.
The user wants the focus to start the way it did the first time the form appears - on the control with lowest tab index.
If there were just one such form, I would hack it: add a line of code hardwired to the desired control.
But there are many such forms, and the visibility logic is in a common base class.
So it would make more sense to do this right, and tell the form to focus on its first (lowest tabindex) control.
Is there an easy way to do so?
(I could iterate through all the controls, but then I have to correctly handle nested controls. Since the GUI has to do this the first time it shows a form, I am hoping there is some method I can call that does it for me.)
(Coded in VB.net, but a C# answer would be fine.)
It is a one-liner, the logic to find the next control is exposed as a method, SelectNextControl(). You should start at the Form object, the one that can never get the focus, and ask it to find the next one in the tabbing order. Which is the child with the lowest TabIndex, whatever value it might have.
So something like this:
public void ShowAgain() {
this.Show();
this.SelectNextControl(this, true, true, true, true);
}
And do consider that a Form object that isn't visible is a rather major resource hog, using up lots of operating system resources for a small convenience. Surely you can also Close/Dispose it and recreate it when needed. YMMV.
You can try to set ActiveControl property before making form visible:
_frm.ActiveControl = null;
This should clear the active control for the form and remove focus from its controls.

.NET determining control's value has changed and finalized

All controls offer some kind of event to indicate that the value has been modified. For example, when you enter a textbox and begin typing with each keystroke there is a TextChanged event. When opening a combobox and selecting a new item in the dropdown, you get a SelectedIndexChanged event.
The trouble is that in many cases the change events signal a series of changes that represent some interim, unfinished state. e.g. When a user is typing his zipcode there's no reason to lookup the city and state or even attempt to validate it until the user finishes typing. When the user has focus on the shipping method combo and presses the up/down keys to navigate through the combo values as he attempts to find the one he wants there is no reason to assume the shipping method has been specified and thus apply it to the invoice. It may make sense to wait until he exits the combobox after paging through the values to signal that a shipping method has been selected. We don't want to be bothered by a series of interim states.
In our shop, we implemented a Finalized event that is only triggered when the user starts making some manual change (as opposed to programmatic changes like setting the zipcode from the DB) and then later finishes up. This Finalized event had to be handled differently for different controls; I'm not aware of any .NET features that would make providing this easier.
The only idea I now have is to try to use something like Rx (Functional Reactive Programming) to accommodate this. Any idea for a simple .NET approach that would facilitate this?
For the zip code textbox, you can use the leave event(occurs when the textbox loses focus), or in the textchanged event you can use an if statement:
if ziptextbox.text.length = 5 then
'Hooray! do stuff here
end if
And you can also just use the leave event for the combobox
The Validating and Validated events work just fine; however, the names were misleading and so it never occurred to me that these were appropriate.
All credit to Hans Passant for his comment that solved this for me. (Hans, will redirect credit to you if you answer.)

Using combo boxes in Visual Basic

What is the best way to deal with a combo box event? In other words, what is the best event handler to use for the case that the user makes a selection from the combo box? I am using a textchanged event, but it seems a bit sloppy. Is there a better way? By the way, my program that I am using it for is a unit converter that converts length.
The textchanged event fires whenever the text inside the combobox changes. every character added to the combobox triggers it, which makes it sloppy.
To avoid performance issues, use either Lostfocus (which fires if the control isn't selected anymore) or the SelectedValue /Selectedindex changed events.
To answer your other question, manipulate the keypress event.
Go to the combobox's keypress event, and type this:
e.handled = true
this will reject any input from user.

how to prevent checkbox change event in vb.net desktop application

I have a checkbox in my vb.net desktop application,on check change event iam performing some action.i want that on pageload check change event should not fire and it shows checkbox.checked on gui.
Windows sometimes fires an event during form load, for whatever reason. It's really irritating, as you may have noticed. This is "by design" according to Microsoft.
You can initialize a variable (global or static), such as a boolean called "Loading" for example, to true. Then, in the form Shown event reset it to false. In the beginning of the event handler, check that variable and exit whenever Loading is true.
I bet you are creating the checkbox and after that you are setting it to checked.
You should create it already checked.
would be nice if you post the code how you create that checkbox and set it true.
You could use the Click Event of CheckBox and 100% safe every time the checkBox is changing state