VB.NET Set text value of a textbos of another form - vb.net

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")

Related

Controlling non-startup form from another class

How do you control a form from another class if that form is not the "Startup form:"?
Scenario:
I have a simple application with 2 forms (Form1 & Form2) and a class (Class1). Form1 has two controls (TextBox1 and Button1). Form2 has one control (TextBox1).
Class1 code:
Public Class Class1
Public Sub ChangeTextOnForm1()
Form1.TextBox1.Text = "Success"
End Sub
Public Sub ChangeTextOnForm2()
Form2.TextBox1.Text = "Success"
End Sub
End Class
Form1 code:
Public Class Form1
Dim Class1 As New Class1
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Class1.ChangeTextOnForm1()
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Form2 As New Form2
Form2.Show()
End Sub
End Class
Form2 code:
Public Class Form2
Dim Class1 As New Class1
Public Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Class1.ChangeTextOnForm2()
End Sub
End Class
If Form1 is set as the Startup Form it calls Class1.ChangeTextOnForm1() on Load and the text in Form1.Textbox1 is changed.
If Form2 is set as the Startup Form it calls Class1.ChangeTextOnForm2() on Load and the text in Form2.TextBox1 is changed.
If Form1 is the Startup Form and I click on Button1, Form2 opens but the text in Form2.Textbox1 is not changed. How do you control a form from another class if the form is not the startup form?
The solution is to open the default instance of Form2 instead of a new instance.
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Forms.Form2.Show()
End Sub

Lifetime of class instance passed from one form to another

My project uses several forms to collect information. Since child forms cannot access variables from the method in which they were instantiated, I decided to create a custom Class that can hold gathered information from the child forms in order to use that information in the parent form. However, I am unsure of when the instance of my custom class will be disposed.
Form1 collects information and uses that to determine which child form to launch next.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim customClass As New CustomClass
Dim form2 As New Form2(customClass)
form2.Show()
Dim form3 As New Form3(customClass)
form3.Show()
End Sub
End Class
Form2 collects data and assigns values to shared variables in CustomClass, as well as making several function calls.
Public Class Form2
Private _CustomClass As CustomClass
Public Sub New(ByRef instance As CustomClass)
InitializeComponent()
_CustomClass = instance
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
_CustomClass.MySharedVariable = TextBox1.Text
_CustomClass.MyCustomMethod()
Me.Close()
Me.Dispose()
End Sub
End Class
When is customClass destroyed? Is it disposed after End Sub from its parent method, or is it disposed after all the forms using that instance have closed?

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

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

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.

Changing Controls of a form inside a panel

I am facing a weird problem
I have 3 forms: MainForm, Form1, Form2
MainForm has 1 Panel: Panel1
Form1 has 1 Label: NameLbl and Button: ChangeBtn
Form2 has 1 textbox: NameTxt and Button: SaveBtn
I used the following code to open form1 inside Panel1 in mainform
Panel1.Controls.Clear()
Dim FormInPanel As New Form1()
FormInPanel.TopLevel = False
Panel1.Controls.Add(FormInPanel)
FormInPanel.Show()
On ChangeBtn.Click Form2 opens as showdialog
I want NameLbl.text to change to NameLbl.text when SaveBtn is clicked But normal code doesnt work.
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
Form1.NameLbl.text=NameTxt.text
End Sub
What Should I do? Any suggestions? Given that i need to open the forms in panels for certain reasons.
Please keep in mind that this is just an example. I have multiple controls in Form1 which i want to change on form2.SaveBtn.click
I have also tried this but it does nothing
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
For Each c As Control In MainForm.Panel1.Controls(0).Controls
If c.Name="NameLbl" Then
c.Text = NameTxt.Text
End If
Next
End Sub
Please Somebody tell me how it do it!
Form2 doesn't seem to have any connection back to Form1 or MainForm. You'd need to raise an event from Form2 which MainForm handles or another class handles and can pass to MainForm
Edit:
Sorry, I've just seen how you're calling Form2. There are lots of ways to get a value back from Form2 after calling ShowDialog(). One is to create a property called Result and check Result if ShowDialog() == DialogResult.OK. Something like the following.
Public Class Form2
Inherits System.Windows.Forms.Form
Public Property Result() As String
Get
Return m_Result
End Get
Set
m_Result = Value
End Set
End Property
Private m_Result As String
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
Public ChangeBtn As Button
Public NameLbl As Label
Public Sub New()
Me.ChangeBtn = New Button()
AddHandler Me.ChangeBtn.Click, AddressOf ChangeBtn_Click
Me.NameLbl = New Label()
End Sub
Private Sub ChangeBtn_Click(sender As Object, e As EventArgs)
Dim form As New Form2()
Dim dr = New form.ShowDialog()
If dr = DialogResult.OK Then
Me.NameLbl.Text = form.Result
End If
End Sub
End Class
I'd like to add that if you plan on growing this application much larger you'll run into issues with maintenance. Look into some patterns for dealing with UI logic like MVC, MVP, MVVM if you're interested.
you can try this code:
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
panel1.Controls(0).NameLbl.text=NameTxt.text '"0" is the index of forminpanel in panel1,maybe it need to change.
End Sub
Form1 is contained in Panel1, so you can not access it via
Form1.
Only MainForm is visible from Form2:
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
For Each c As Control In MainForm.Panel1.Controls(0).Controls
If TypeOf c Is TextBox Then
c.Text = NameTxt.Text
End If
Next
End Sub
Try this:
For Each form1 As Form1 In MainForm.OwnedForms.OfType(Of Form1)
Form1.NameLbl.text = NameTxt.text
Next
I've faced same issue and I've fixed with this
Dim f As FormInPanel
f = Form.Panel1.Controls(0)
f.transection = True
f.NameLbl.text=NameTxt.text