Best Practice For Error Checking Controls On A Form - vb.net

So I have a form with a variety of different controls (combobox, textboxes, listboxes, etc).
My first thought is to create a If, Else, End If statement. Well while that would work, it could also get pretty long, depending on the amount of controls and combinations.
Validation could include if a listbox is filled, checkbox is checked, etc pertaining to WinForms.
Is there a better solution to check all possiblities than an If statement?

It might be worthwhile to do the error checking as the user fills out the form. This could be implemented with the LostFocus event. Ex:
Private Sub btnTest_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yourbutton.LostFocus
Dim txt = yourbutton.Text
If txt = "yourtest" Then
'do stuff
EndIf
End Sub

As above. It depends on the Validation you are trying to do. Are you validating user input, datatype lenght range, etc. Are you validating business rules. Should such and such a value equal something else. There's all kinds of possibilities.

Related

Search data inside Datagridview without database - VB.net

I've searched around and couldn't find the one I've been looking for.
I have a DataGridView in VB.NET with thousands of records from MySQL Database. Now, I want to SEARCH item_description that matches or something LIKE the inputted text. I am using TEXTCHANGED
I didn't use search to database since its taking time to load.
Sample is
Search: orange
DataGridView must filter and display only with the words like "orange"
ITEM_ID
ITEM_DESCRIPTION
120
Orange Juice
832
Orange Fruit
I am thinking of getting the array list names of the column "item_description" and filter it but I don't know where to start and what codes to use. Thank you
Assuming that you have taken the advice in my comment and populated a DataTable and then bound that to the DataGridView via a BindingSource, filtering the data is a simple matter of setting the Filter property of that BindingSource. In your case, it should be something like this:
myBindingSource.Filter = "ITEM_DESCRIPTION LIKE 'orange*'"
One issue with filtering on the TextChanged event of a TextBox is that you will filter multiple times when the user types multiple characters when you only need the last one. To alleviate that, I suggest using a Timer that you start/restart each time a character is typed and then filter when it Ticks. That will enable the user to type multiple characters without filtering but also not have a significant wait after they stop typing for the filtering to be done. I recommend an interval of around 250 milliseconds but you can experiment to see what you think is best. E.g.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
'Start/restart the Timer.
Timer1.Stop()
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
'Perform the filtering.
BindingSource1.Filter = $"ColumnName LIKE '{TextBox1.Text}*'"
End Sub

winforms vb 2008 textbox validation issue

I have vb 2008 Winforms application.
In my Branch form I have 11 textbox fields and I want to validate 8 of them, either mandatory, or required format such as UK postcode, UK tel nos etc.
My issue now is that when the validation starts, it is validating the last text field first ( or seems to be)
here is my code
For Each oCtrl As Control In Me.Controls
If TypeOf oCtrl Is TextBox Then
oCtrl.Focus()
If Validate() = False Then
Exit Sub
End If
End If
Next
what's wrong please?
what's wrong please?
The controls collection isn't sorted or grouped. Your loop will access them in whatever order the collection has them.
Without more code it's hard to say how to fix it. However a tip may be in order. Use the same handler to handle the validate event for each textbox. This way you can keep the user at that textbox until the input is valid.
is it possible to add the items to the collection in the order of their tab indexes on form Shown event, how would I do that please?
A List(Of TextBox) and a custom sorter would probably be the way to go
Dim AllTB As New List(Of TextBox)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
AllTB.AddRange(Me.Controls.OfType(Of TextBox))
AllTB.Sort(Function(x, y) x.TabIndex.CompareTo(y.TabIndex))
End Sub
To loop through the textboxes use:
For Each tb As TextBox in AllTB
Because the TextBoxes are in the list by reference you can get or set any of the properties in the textboxes and any changes, will be reflected on your form. You could also use sequential names for the textboxes, tag property, etc. and sort by that.

Visual Basic/Studio Text Input issue (disabling certain characters)

I'm creating a form with three text boxes with a 'subtotal', 'amount tendered' and 'change' box. Both subtotal and change boxes are disabled which means the user can only input numbers in the tendered box. I'm using the following code which I found from http://www.vbdotnetheaven.com/blogs/5908/restricting-user-input-in-vb-net.aspx to limit the input:
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not Char.IsDigit(e.KeyChar) Then
e.Handled = True
End If
End Sub
My concern is that that specific coding works for limiting the input of numbers, but it also disabled the backspace, dot and delete key. I'm fairly new to this (we only started discussing things about VB/Studio less than two weeks ago, I have no prior experience about using Keypresses but I wanted to try)
I've tried manipulating the if statement (and also adding if statements) by excluding the spacebar's ASCII value, only to end up with a warning about incompatible types. I can replace text by double clicking the text input when the form is running, but my app will most likely be utilizing the keyboard mainly because it is a transaction app, so the backspace and more importantly the dot is really needed. How do I exclude both from being disabled?
Take a look at the sample for Control.KeyDown in the MSDN documentation, it's about 95% of what you want.
That said, be aware, there's more work to do. What happens if someone enters 124.34.22? You might consider a MaskedTextBox, but if this is an exercise for learning Visual Basic, that's pretty much cheating :)

VB.Net - How do you "dynamically" select an object?

I'm not sure if the question properly asks what I want, because I'm a bit new, but what I am trying to do is write a sub or function that I can run when a label is clicked, and I want to to be reusable on multiple labels. There will be 12 in total, and I don't wish to write the same thing over and over, with slightly different characters. A programmer never wants to write the same thing twice, right? Also, something else is making it a necessity to do it dynamically.
So, how I was trying to accomplish that was by using a string, and adding the name of the label to the string on click.
click1 = "Label1"
As it turns out, you can't just say click1.Text and return the text of Label1. The reason it's important to do it implicitly is because I need to somehow remember the one I clicked before, to compare the first click and second click, and if they match, do A, and if they don't match, do B.
The first parameter (it is called sender) to the event handler your wrote to respond to the click event is the object which sent the event.
If you assign the same routine to respond to on click of all your labels, it will be called for every one of them, but the sender parameter will point to the actual label which was clicked
HTH
mfeingold is correct, if you're unsure of the syntax:
Private Sub LabelClicked (ByVal sender As Object, ByVal e As EventArgs) Handles Label1.Click, Label2.Click, Label3.Click
sender.Text = "I've been clicked."
End Sub

How can I refer to a control from within a control's method (like the "me" for classes)?

How can I refer to the control while I am inside a control's method in VB.NET?
For example, I want in a textbox to show a message box with that textbox's text every time the text changes. The code would be something like:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
msgbox("The text is:"+ Me.text)
' ok the line above wont work i already know that, because "Me" refer to the form,
' not the control textbox1
' how i will refer to the textbox1's text???
' i dont want to use "textbox1.text" is there a way similar like the "Me" is for forms?
' because i want to copy-paste a code like this in a lot of controls and do not want to
' have to change in every copy the name to each control name
End Sub
I hope I made myself clear; my English needs some improvement :D
No, there's no keyword that allows you to do that. However, every event raised by a control passes in a sender parameter that you can use to determine which particular control raised that event.
Note that this parameter is always typed as a basic Object (because it can represent any possible control), so you'll need to downcast to a more specific control class if you need to access any of the unique members that it exposes. Since you're handling an event raised by a TextBox control, you know that the sender must be of type TextBox, so you can simply use DirectCast to handle the upcasting. You don't have to worry that an InvalidCastException will be thrown.
For instance, your above example would become:
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
Dim textBox As TextBox = DirectCast(sender, TextBox)
MessageBox.Show("The text is: " & textBox.Text)
End Sub
That being said, there are a couple of concerning things that jump out at me in your question:
Any time that your approach to solving a problem is "copy-pasting" code, you should stop, take a step back, and try to figure out if there's any better way to achieve your ultimate goal.
For example, if you need every textbox on your form to react in the same way whenever a particular event is raised, you should consider subclassing the existing TextBox control and consolidating all of your code in one place. Remember that you can inherit off of most of the standard controls to add custom functionality. This is often a far better solution than copying and pasting code to multiple places in your project. If you ever need to track down a bug or modify that functionality, you'll only have to change it one place in your code, rather than several. As a somewhat cheekier benefit, you'll be able to use Me to refer to that control when you're editing its subclass.
You should always prefer to concatenate (combine) strings using the & operator in VB.NET, rather than the + sign. Or perhaps even better, the String.Concat or String.Format methods.
There is no reason to use MsgBox in VB.NET, as opposed to MessageBox.Show. No, this won't improve performance of your application, but it's a good practice to get into for .NET languages.
The sender variable contains the TextBox instance you want to access. You only need to convert the sender to TextBox.