Open a form from another form and get a value and then pass it back to the first form [duplicate] - vb.net

This question already has answers here:
VB.Net Passing values to another form
(2 answers)
Closed 5 years ago.
I have 2 forms with text boxes and buttons and I want the first form to open the second form. I then proceed to type a value in the text box in the second form so that when I click the button on the second form it must close and pass the value of the text box to the first form text box.
I have been trying to do this with no luck so far...
I am currently using vb.net in visual studio 2015
Here is my code:
Public Class Form1
WithEvents fr2 As New Form2
Private Sub btngetvalue_Click(sender As Object, e As EventArgs) Handles btngetvalue.Click
fr2.Show()
End Sub
Private Sub fr2_passvalue() Handles fr2.passvalue
Me.txtform1.Text = fr2.txtform2.Text
End Sub
End Class
Public Class Form2
Event passvalue()
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnpassvalue_Click(sender As Object, e As EventArgs) Handles btnpassvalue.Click
RaiseEvent passvalue()
Me.Close()
End Sub
End Class
I have tried accessing the controls directly but it does not work as vb.net now uses classes for everything. I cannot seem to figure out how to get around this.

Redefine your event to pass a string. Then use the Form2 event to pass the value to the handler.
Public Class Form2
Public Event passvalue(text As String)
Private Sub btnpassvalue_Click(sender As Object, e As EventArgs) Handles btnpassvalue.Click
RaiseEvent passvalue(txtform2.Text)
Me.Close()
End Sub
End Class
Handle it in Form1, updating Form1's TextBox.
Public Class Form1
WithEvents fr2 As Form2
Private Sub btngetvalue_Click(sender As Object, e As EventArgs) Handles btngetvalue.Click
fr2 = New Form2()
fr2.Show()
End Sub
Private Sub fr2_passvalue(text As String) Handles fr2.passvalue
Me.txtform1.Text = text
End Sub
End Class
As you had it before, Form1 was accessing controls on Form2 directly, which defeats the purpose of the event.

You could simply define a global variable (Public), which would be accessible from both forms.

Related

VB.NET Set text value of a textbos of another form

what i need is when form2 is opened from form1 cliking on a button then on form2 i click on anothe butto and i set the value of a textbox of form1.
if i set the type of application as windows form application it is all ok but if i set as class library i have the error reference to a non-shared member requires an object reference.
if i reference to Dim frm = New form2 it open a second form2 and i dont want it.
how can resolve this?
thank you.
here is the code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.ShowDialog()
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.TextBox1.Text = "aaaa"
Me.Close()
End Sub
End Class
When you build vb.net winforms project, some extra code is generated in the project. For example static instance of the Form class, so you can access instance methods through the class name Form1.ShowDialog().
This were done for VB6 programmers to make shifting from VB to VB.NET easier.
When you change project to be a library project, this code not generated anymore and Form1 is just a class and you can not access instance methods directly, but need to instantiate an instance "manually".
Instead of using this "hidden" shared instance, create form instances manually. You can pass instance of form1 to the constructor of form2 and update form1 from there.
Because you are using ShowDialog, I would suggest to make Form2 not depend on the Form1 and instead of updating Form1 textbox directly(you would like to avoid making Form controls public), return value as result of the dialog.
Public Class Form2
Public Property ResultValue As String
Private Sub Button1_Click(s As Object, e As EventArgs) Handles Button1.Click
ResultValue = "Value from Form 2";
DialogResult = DialogResult.OK; ' This suppose to close the form
End Sub
End Class
Public Class Form1
Private Sub Button1_Click(s As Object, e As EventArgs) Handles Button1.Click
Using form As New Form2()
Dim result As DialogResult = form.ShowDialog()
If result = DialogResult.OK Then
TextBox1.Text = form.ResultValue
End If
End Using
End Sub
End Class
Add the following to form1
Public static sub changeTitle(myTextBox as Object,title as string)
myTextBox.Text=title
End sub
Call above function in form2
Form1.changeTitle(Form1.TextBox1, "new title")

How can I refresh One Form when other Form is closed VB.NET

I want to refresh Form1 when Form2 is closed,
I have searched a lot but none of those queries answered my question.
I want to detect the Form2 closing event in Form1.
Why not refresh Form1 in the Closed event of Form2?
Private Sub Form2_Closed(sender As Object, e As EventArgs) Handles Me.Closed
Form1.Refresh()
End Sub
You must have an instance of Form2, and you AddHandler to subscribe to its Closed event.
I don't know how you create your Form2. So here is a solution which should work in your case. Just replace _myForm2 = New Form2() with however yours is created
Public Class Form1
Public ReadOnly Property MyForm2 As Form2
Get
Static _myForm2 As Form2
If _myForm2 Is Nothing Then
_myForm2 = New Form2() ' replace with how your Form2 is created
End If
Return _myForm2
End Get
End Property
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler MyForm2.FormClosed, AddressOf myForm2_Closed ' add event handler
MyForm2.Show()
End Sub
Private Sub myForm2_Closed(sender As Object, e As FormClosedEventArgs)
Me.Refresh()
End Sub
End Class
If you are using a default Form2 instance, you shouldn't be. But if you must, you would use _myForm2 = Form2

vb.net code to close another form if it is open

This is pretty rudimentary, but I can't figure it out.
I want to programmatically close form1 when form2 closes, if form1 is still open. Form2 was opened by a command button on form1.
The code on Form1 that opens Form2 is:
Dim frm As New form2
frm.Show()
What's the best way when Form2 closes to close any open copies of Form1 that are open, also?
If you want to handle your two forms independently, you need to watch over them from a third form or class. So my suggestion would be to create both of them in this third class, and pass a reference of the second form to the first form so it can then open it. This way:
Public Class MyHelper
Public Sub CreateForms()
Dim form2 as New Form2()
AddHandler form2.Closed, AddressOf Form2_OnClosed
‘ Create as many copies as you need
Dim form1 as New Form1(form2)
form1.Show()
End Sub
Protected Sub Form2_OnClosed(sender as object, e as EventArgs)
‘ Same code for each form1 that has been created and opened.
If (form1.IsOpen) Then form1.Close()
End Sub
End Class
Public Class Form1
Private _form2 as Form2
Public Property IsOpen as Boolean = false
Public Sub New(form2 as Form2)
_form2 = form2
End Sub
Protected Sub MyButton_Click(sender as object, e as EventArgs) handles MyButton.Click
‘ You open your form here or wherever you want (even on the constructor)
_form2.Show()
End Sub
Protected Sub Me_OnClosed(sender as object, e as EventArgs) handles Me.Closed
Me.IsOpen = false
End Sub
Protected Sub Me_OnShown(sender as object, e as EventArgs) handles Me.Shown
Me.IsOpen = true
End Sub
End Class
Add this reference to make it work.
Imports System.Linq
If Application.OpenForms().OfType(Of Form1).Any Then
MsgBox("Form1 is open")
End If
Supposing you have 3 forms and want to close the other two on button click
Private Sub EMPLEADOToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EMPLEADOToolStripMenuItem.Click
If Application.OpenForms().OfType(Of BUSCAR_INDEX).Any Then
BUSCAR_INDEX.Close()
ElseIf Application.OpenForms().OfType(Of MIEMBROS_INDEX).Any Then
MIEMBROS_INDEX.Close()
End If
EMP_INDEX.Show()
EMP_INDEX.EmpIDTextBox.Text = EmpIDTextBox.Text
End Sub

automatically click a button in a different form in vb.net

I have 2 forms in my VB application, I am populating a textbox with some text which is working fine but i then want to automatically click a button in another form and run the actions for that button click
i have this so far:
Form1.TextBox5.Text = "C:\folder\file1.csv"
Form1.Button8.PerformClick()
but its not clicking the button and performing the actions for Button8 on Form1
How can i make my other form click Button8 on Form1 and run its actions/events?
You can do it like this. The forms will be different from yours because I coded it up as an example. Basically I have added a public method that allows another class to call PerformClick on its button. I believe that's what you were asking for.
' Form1 has two buttons, one for showing the Form2 object and another for performing the click on Form2.Button1
Public Class Form1
Private form2 As Form2
Private Sub ShowFormButton_Click(sender As System.Object, e As System.EventArgs) Handles ShowFormButton.Click
form2 = New Form2()
form2.Show()
End Sub
Private Sub PerformClickButton_Click(sender As System.Object, e As System.EventArgs) Handles PerformClickButton.Click
If form2 IsNot Nothing Then
form2.PerformClick()
End If
End Sub
End Class
' Form2 has a button and a textbox
Public Class Form2
Public Sub PerformClick()
Button1.PerformClick()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TextBox1.Text &= "Clicked! "
End Sub
End Class

Editing button properties from a module

I am just starting out with Visual basic .Net.
I can't seem to figure what's the scope of button properties like button.text. Can they be used outside the button_click event sub? And if so, how?
How can I modify button properties from a module in real time when a certain condition is met?
I'd surely appreciate some guidance and an example, if possible. Thanks.
Just as quick sample, I don't suggest doing something like this
I have 2 forms open, Form2 and Form3. Each form has a button on it.
I also have a Module, called MyModule
Public Class Form2
Public Sub ChangeButtonText(ByVal s As String)
Button1.Text = s
End Sub
End Class
.
Public Module MyModule
Sub ChangeForm2Btn()
Form2.ChangeButtonText("LOL")
End Sub
End Module
From my Form3 I click the button and call the module function to change Form2 button's text
Public Class Form3
Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
MyModule.ChangeForm2Btn()
End Sub
End Class
You could pass a reference to the button to a sub in the module, and then call that sub from the form.
i.e.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ChangeButtonText(Me.Button1, "Changed")
End Sub
End Class
Module modButton
Public Sub ChangeButtonText(ByRef Button As Button, ByVal Text As String)
Button.Text = Text
End Sub
End Module