Using combo boxes in Visual Basic - vb.net

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.

Related

VB.NET ComboBox Not Recognising Text Change

I have an application whereby I have the behaviour of 2 groupboxes enable/disable based on the value in a combobox.
This combobox however the behaviour for the group boxes only works when I leave the field by tab or click, not when the text changes.
I have tried using the Leave, TextChanged, Validated Events and everytime I have to actually "Leave" the field for it to perform the enabled/disabled of the groupboxes and their subsequent controls.
I need it so that when they choose 1 of the 3 options it immediately changes and doesn't work when they leave the field but when they choose the correct word.
Any assistance would be great I'm tearing my hair out here.
I think you are looking for the event on your combo boxes
SelectedIndexChanged
To stop the user from leaving an invalid field use the event
Validating
set the e.cancel to True if the validation fails

Clearing number in a Label on Text Box Change

Just have a simple question. I have a program and in it I have a label that displays results once you calculate them, and I want the label to clear once you change the text in the input boxes. Assuming the label is named like lblResult or something, how would I go about doing this?
If you are using Windows Forms, you would handle the TextChanged event of your input box. In the event handler, you would then set your lblResult.Text = ""
If you are using WPF / XAML, the concept it the same, you'd just handle the event a little differently.
You can learn more about VB.NET and Event Handlers on MSDN.

Highlight textbox text with two separate textboxes at once?

Is this possible to do or are you only allowed to .focus one textbox on each form? I'm looking to highlight more than one textbox at one time if data is empty.
Only one control can have the focus every time (and thus what you request is not possible). In any case, note that the focus is meant for actions (not for visuals) and that only one control can perform actions every time in the GUI thread (e.g., text written in a TextBox or Button being clicked). On the other hand, you might provoke situations similar-enough to various controls getting the focus simultaneously (e.g., text written in various textboxes, coordinated via TextChanged Events: the actions are not performed simultaneously but the user will not realise about it).
If your intention is just highlighting the given control, you shouldn't rely on the focus. The focused control does get somehow highlighted, although this visual effect is not too relevant and, some times, not even perceptible. The best thing you can do is provoking the highlighting effect "manually". For example: Panel surrounding the TextBox, whose dimensions/visibility are affected; or just simply changing the BackColor property of the TextBox.

What is another alternative to `TextChanged` event

I am working in windows form and have a few textboxes on a form.
These textboxes from top to bottom are connected, so if the text changes in textbox1, then txtbox2 and txtbox3 have to comply, if something changes in textbox2 then 3 has to comply;... and so on ... not going to bore you.
Now I was looking for an alternative, more efficient way of calling the textchanged event on the textboxes so it doesn't do everything everytime, even if one charater has changed....
Something like focus off textbox event ... or some other alternative .
This would be in vb.net and windows form.
You can use the TextBox.LostFocus event inherited from Control.LostFocus.
This event is fired when the control looses focus, like Tab or clicked on another control.
TextBox1.LostFocus perhaps? Or enable validation on the controls.
Control focus event order:
http://msdn.microsoft.com/en-gb/library/system.windows.forms.control.lostfocus.aspx
CausesValidation:
http://msdn.microsoft.com/en-gb/library/system.windows.forms.control.causesvalidation.aspx

Better to use Validating or Leave event to validate textbox data?

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.