When any of the textboxes changes (VB) - vb.net

I'm a beginner to VB and I'm looking to optimize my code a little. I have 9 textboxes, and every time one of them changes I run a Subroutine based on it's content. Is there a way to make it, perhaps with a for loop, go through all of the 9 textboxes and register when any of them changes. Here's ..my code looks like at the moment...
Private Sub tbBox1_TextChanged(sender As Object, e As EventArgs) Handles tbBox1.TextChanged
checkInput(tbBox1, 0, 0)
End Sub
Private Sub tbBox2_TextChanged(sender As Object, e As EventArgs) Handles tbBox2.TextChanged
checkInput(tbBox2, 0, 1)
End Sub
Private Sub tbBox3_TextChanged(sender As Object, e As EventArgs) Handles tbBox3.TextChanged
checkInput(tbBox3, 0, 2)
End Sub
..etc

You don't need a separate handler for each text box. A single handler method can handle all of them. If you need a value for each text box to pass into your checkInput method, just use the Tag property of each text box.
Private Sub TextBoxChanged(sender As Object, e As EventArgs) Handles tbBox1.TextChanged, tbBox2.TextChanged, tbBox3.TextChanged 'etc.
checkInput(sender, 0, sender.Tag)
End Sub

Try multiple Handles:
Private Sub tbBox_TextChanged(sender As Object, e As EventArgs) Handles _
tbBox1.TextChanged, _
tbBox2.TextChanged, _
tbBox3.TextChanged
checkInput(sender, 0, 0)
End Sub

Related

VB.net One sub for many events

I'm working on a function in VB.Net where a user can select a supplier from a list.
The idea is that the user will filter the list until the right supplier is visible in a datagridview
the user can then either double click on the row header, the cell content or select a supplier and then click an OK button
I am wondering though, how do I avoid building one Sub for each of the above three events, Can I create one sub that catches all three events?
Private Sub supplierSearchOkButton_Click(sender As Object, e As EventArgs) Handles supplierSearchOkButton.Click
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
Private Sub supplierSearchDataGridView_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles supplierSearchDataGridView.CellContentDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
Private Sub supplierSearchDataGridView_RowHeaderMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles supplierSearchDataGridView.RowHeaderMouseDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
Personally, I would go with the extra method option mentioned in the comments but, if you want to, you should be able to do this:
Private Sub SetSupplier(sender As Object, e As EventArgs) Handles supplierSearchOkButton.Click,
supplierSearchDataGridView.CellContentDoubleClick,
supplierSearchDataGridView.RowHeaderMouseDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
or even this:
Private Sub SetSupplier() Handles supplierSearchOkButton.Click,
supplierSearchDataGridView.CellContentDoubleClick,
supplierSearchDataGridView.RowHeaderMouseDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
If you're not using any properties of the other e parameters then you can use the most general EventArgs for all three events and if you're not using the parameters at all then you can ditch them altogether. I didn't test this specifically but I'm fairly sure both will work.

I have multiple panels on same form and I want Passing a value between 2 panel

I have multiple panels on the same form and I want Passing a value between 2 panels
I want to enter Username in the first panel and Show as Label in next panel
please not in between 2 Form but 2 Panels.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Guna.UI.Lib.GraphicsHelper.ShadowForm(Me)
End Sub
Private Sub GunaButton1_Click(sender As Object, e As EventArgs) Handles GunaButton1.Click
pnLogin.BringToFront()
GunaTransition1.Hide(pnLogin)
End Sub
Private Sub GunaButton2_Click(sender As Object, e As EventArgs)
End Sub
Private Sub GunaGradientButton2_Click(sender As Object, e As EventArgs)
End Sub
Private Sub GunaTextBox1_Click(sender As Object, e As EventArgs) Handles GunaTextBox1.Click
End Sub
End Class
If all the controls are in the same Form, then the container they are within is irrelevant.
The code would look something like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.Text = TextBox1.Text
End Sub
You need to change the control names to what you have on your form for the Button, TextBox and Label.
You can simply declare the variable in form class scope and use it! can you explain more details?
Update:
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
Lable.text = TextBox.text
EndSub

Continuous update feature/event

Is there some kind of update feature like in C# for Visual Studio 2012 in vb.net? (so that it will continuously check to see if an if statement is fulfilled)
Try something like this:
Dim iClicks As Integer = 0
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
iClicks += 1
Label1.Text = iClicks.ToString()
If iClicks = 1000 Then
MessageBox.Show("1000 reached!")
End If
End Sub
It's better to have a integer counter than checking the string value and performing conversion each time. As you can see, the code checks the click counter each time you click the label.
If you want to check the value periodically, add a Timer and perform the check in its event:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If iClicks = 1000 Then
MessageBox.Show("1000 reached!")
End If
End Sub
But consider the need to do this, because maybe it's not necessary and will decrease the performance comparing it with the other way.
There is probably a better way of doing this, but what I've done when I need a constant "update" function, is just use a Timer, then handle the Ticks.
Easiest way of implementing it is to go to the form designer, in the Toolbox, under Components, drag a timer onto the form. Double-click it to add a handle for its Tick event.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If WhateverYoureLookingFor = True
'Do stuff
End If
End Sub
This is obviously VB, but easy to convert to C#.

Changing button name if file exists?

I'm working on a custom GUI, my last step is for the button to change automatically check if the file exists or not on startup. The method below is my download/open button. Any help is appreciated!
Private Sub Button7_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button7.Click
If Dir("DownloadedFile.zip") <> "" Then
Process.Start("DownloadedFile.zip")
Else
WC.DownloadFileAsync(New Uri("http://download852.mediafire.com/3a688rz1a6ig/dk71cs34ihs3v6x/Devil+went+down+to+georgia.rar"), "DownloadedFile.zip")
MsgBox("Starting Download")
End If
End Sub
A subroutine is a seperate method that does not return a value. I would take the If statement out of your click eventhandler and put it in its own method as I stated like this.
Private Sub CheckForFile()
If Dir("DownloadedFile.zip") <> "" Then
Process.Start("DownloadedFile.zip")
Else
WC.DownloadFileAsync(New Uri("http://download852.mediafire.com/3a688rz1a6ig/dk71cs34ihs3v6x/Devil+went+down+to+georgia.rar"), "DownloadedFile.zip")
MsgBox("Starting Download")
End If
End Sub
Call it from your button like this.
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
CheckForFile()
End Sub
Then handle the Shown Event and call it from there. It will run as soon as the initial form is shown
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
CheckForFile()
End Sub
Responding to your comment You need to use the WebClient DownloadFileCompleted Event and the WebClient DownloadProgressChanged Event

vb.net combine two or more events

I have four textbox for an IP Address. Each textbox is for each of the octate.
When the control loses focus or when user presses ENTER, it will check if the value is greater than 255.
Private Sub txtParametersIpFirst_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtParametersIpFirst.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
'Check if Value is more than 255
End If
End Sub
Private Sub txtParametersIpFirst_LostFocus(sender As Object, e As EventArgs) Handles txtParametersIpFirst.LostFocus
'Check if Value is more than 255
End Sub
Is there something that these two events be combined?
The best way to combine two events is by calling a method..
Private Sub Logic()
'Check if Value is more than 255
End Sub
Private Sub txtParametersIpFirst_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtParametersIpFirst.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
Logic()
End If
End Sub
Private Sub txtParametersIpFirst_LostFocus(sender As Object, e As EventArgs) Handles txtParametersIpFirst.LostFocus
Logic()
End Sub
However, if you're to combine two same events, for example two Lostfocus event of two different textboxes, then you can just add handles;
Private Sub txtText1_LostFocus(sender As Object, e As EventArgs) Handles txtText1.LostFocus, txtText2.LostFocus,
' Code similar for both textbox
' You can use the Sender param to verify who's triggering the event
End Sub
Why dont you go for masking like this ###.###.###.### and use the Validation event to confirm that the value entered is a valid IP address.By this way there is no need for four different text boxes
There is some good material on This link.and MSDN