VB.net One sub for many events - vb.net

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.

Related

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

Enable/Disable a form while in an another form

I'm trying to figure out how to disable my form(Form1.vb) without hiding it then enabling it after I'm done with the other form(Form2.vb).
I've searched on youtube but it says C#. I've tried it but somehow it was indicated as an error in VS 2015. I tried messing around with the syntax because I really can't figure it out. The syntax that I have tried is "LandingForm.ActiveForm.Owner.Enabled = True".
Indicated below are the codes of my system. The first one is form1.vb/LandingForm.vb and the second one is form2.vb/AcctSettings.vb.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Enabled = False
AcctSettings.Show()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
LandingForm.ActiveForm.Owner.Enabled = True
Me.Hide()
End Sub
Am I missing something? Can somebody help?
On your form1 just have a simple line like
Form2.Show()
wherever/however you wish to open the other form (button etc.)
then in the code for that form2 in the formload handeler have
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form1.Enabled = False
End Sub
This will keep form1 open but basically grey it out.
Then have a simple button click like so to have access to form 1.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Form1.Enabled = True
End Sub
And you are done! And if you wish just add another button or a IF statement to the button2 sub and say form1.enabled = false if you want to be able to enable/disable etc.
To enable/disable your userform :
To disable your userform you will need yo enable it before :
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form1.Enabled = True
Form1.Enabled = False
End Sub
To disable it will be easier you just got to set your userform.enabled as False
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Form2.Enabled = True
End Sub
If you want to close your userform useroform.Unload might be the solution.
In your code it would be like this :
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Enabled = False
AcctSettings.Show()
form1.Unload
End Sub
and
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
LandingForm.ActiveForm.Owner.Enabled = True
Me.Hide()
form2.Unload
End Sub
an other option is to Hide your user form : it will just hide the userform and will not release the objects and variables from the memory. Where as Unload method will release the objects and variables from the memory.
UserForm1.Hide
To conclude : Hide method will be used when we want to hide the form temorarly and to show after sometime to the user. Where as unload will be used when it completes the task.
Note that this is YourUserForm_Name.Unload

Working sample of Control.VisibleChanged Event in vb.net

I'm struggling to make the MSDN code sample for the Control.VisibleChanged event work: I don't see the MsgBox.
Private Sub Button_HideLabel(ByVal sender As Object, ByVal e As EventArgs)
myLabel.Visible = False
End Sub 'Button_HideLabel
Private Sub AddVisibleChangedEventHandler()
AddHandler myLabel.VisibleChanged, AddressOf Label_VisibleChanged
End Sub 'AddVisibleChangedEventHandler
Private Sub Label_VisibleChanged(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Visible change event raised!!!")
End Sub 'Label_VisibleChanged
You need to "wire up" the events to the event handlers.
To start with, to get the code in HideLabel_Click to be called you need it to respond to a click on the button named "HideLabel".
There are two ways to do that: you can use AddHandler or the Handles clause.
To demonstrate the latter:
Option Strict On
Public Class Form1
Private Sub HideLabel_Click(sender As Object, e As EventArgs) Handles HideLabel.Click
myLabel.Visible = False
End Sub
Private Sub myLabel_VisibleChanged(sender As Object, e As EventArgs) Handles myLabel.VisibleChanged
MessageBox.Show("Visible change event raised!!!")
End Sub
End Class
However, you will notice that the message is shown even before the form appears. That is because of what goes on behind the scenes to create the form.
To avoid that happening, you can add the handler after the form has been shown:
Option Strict On
Public Class Form1
Private Sub HideLabel_Click(sender As Object, e As EventArgs) Handles HideLabel.Click
myLabel.Visible = False
End Sub
Private Sub myLabel_VisibleChanged(sender As Object, e As EventArgs)
MessageBox.Show("Visible change event raised!!!")
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
AddHandler myLabel.VisibleChanged, AddressOf myLabel_VisibleChanged
End Sub
End Class
Another way, in VB2015 and later, is to use a "lambda expression" instead of a separate method, although then you cannot disassociate the handler from the event with RemoveHandler:
Option Strict On
Public Class Form1
Private Sub HideLabel_Click(sender As Object, e As EventArgs) Handles HideLabel.Click
myLabel.Visible = False
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
AddHandler myLabel.VisibleChanged, Sub() MessageBox.Show("Visible change event raised!!!")
End Sub
End Class
Craig was kind enough to [and I quote verbatim] call attention to the importance of Option Strict when you add handlers manually using AddHandler. Without it, the "relaxed delegate convention" may allow adding handlers which don't exactly match the event signature that you won't be able to remove later.
Having said that, Option Strict On isn't a complete safeguard: notice how my last example compiles and works even with the wrong method signature for the handler.
[I suspect that the MSDN code sample was first created in C# as part of a larger example, so some parts have been lost in the translation and excerption.]
I get this is old but came across this post when looking for more information on VisibleChanged and couldn't help but notice that the accept answer may be misleading. If you are using a designer to create your Form and place objects on it, then the accepted answer will be fine. In fact you can get rid of the addHandler because the designer handles that for you. All you would need to do is use a handles clause with your label.
Private Sub Button_HideLabel(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
myLabel.Visible = False
End Sub 'Button_HideLabel
Private Sub Label_VisibleChanged(ByVal sender As Object, ByVal e As EventArgs) Handles myLabel.VisibleChanged
MessageBox.Show("Visible change event raised!!!")
End Sub 'Label_VisibleChanged
Where the issue lies with the accepted answer is if you arn't using a designer. Adding handle clauses to "wire up" simply won't work (we can make it work and if anyone is interested in that I'll be happy to post a code snippet of that, but it's not how the accepted answer lays it out). In your case all you need to do is call AddVisibleChangedEventHandler() to set up the handler. that's it. you could have done this by calling it in MyBase.Load
Private Sub Load_Form(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
AddVisibleChangedEventHandler()
End Sub
Private Sub Button_HideLabel(ByVal sender As Object, ByVal e As EventArgs)
myLabel.Visible = False
End Sub 'Button_HideLabel
Private Sub Label_VisibleChanged(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Visible change event raised!!!")
End Sub 'Label_VisibleChanged
Private Sub AddVisibleChangedEventHandler()
AddHandler myLabel.VisibleChanged, AddressOf Label_VisibleChanged
End Sub
Once again I know this is dated but couldn't help but notice that (more or less assuming) that you are trying to get a msgBox to appear when you click a label. That is you click a label and then toggled the visibility of another label. The other label is the one where the event handler is on for visibility change. So that inevitably gets called when clicking the original label. IF you only want this msgBox to appear when clicking that label and not when the form loads as well, you should change the addHandler statement so that you are adding a handler on the click event.
Private Sub Load_Form(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
AddVisibleChangedEventHandler()
End Sub
Private Sub Label_VisibleChanged(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Visible change event raised!!!")
End Sub 'Label_VisibleChanged
Private Sub AddVisibleChangedEventHandler()
AddHandler otherLabel.Click, AddressOf Label_VisibleChanged
End Sub 'AddVisibleChangedEventHandler
Also Option Strict On has nothing to do with addhandler (From my understanding, could be wrong. please enlighten me if that is the case). Option Strict On is only checking to see that you arn't implicitly typecasting. So for example:
Dim a As Double
Dim b As Integer
a = 10
b = a
results in an error when Option Strict is On but is totally legal if it is off. So in the case of you leaving off the handles clause, you'll never be implicitly typecasting and therefore is not needed.
Hope this helps anyone who sees this question

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

Trying to enter a name in a textbox, hit ok, and have that form close and pass the info to another forms label

***THE CODE FROM THE FIRST WINDOW GOES INTO A TEXT BOX AND YOU HIT THE BUTTON HERE.
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Me.Close()
End Sub
***THE CODE ON THE FORM WHERE I WANT THE INFO TO BE PLACED IS HERE.
Private Sub Loan1Form_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.lblCompanyName.Text = DataEntryForm.txtCompanyNameInput.Text
End Sub
Anyway's that's what I found on a youtube video and im having trouble getting it to work. Any help would be appreciated.
Thanks.
Pass the data to an instance of the form:
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Dim f As New OtherForm
f.lblCompanyName.Text = txtCompanyNameInput.Text
f.Show()
Me.Close() 'make sure this form is not the one that closes the app if it closes
End Sub