Joao Pinto

- On form1 I click a button that opens form2 that has a treeview with options for me to select;


-After I select an option from the treeview on form2 I assign it to a variable (public) and close form2;


- I then return to form1 (it wasn't closed during the process on form2) and want to assign the value of the public variable to txtDescricao.text field.


What event should I use to put txtDescricao.text=ValueSelected

Thanks,

Joao Pinto



Re: Visual Basic Language Passing value from one form to another (tricky question!)

ahmedilyas

what you might want to do is to other:

  • create this global variable in form2 where this variable will have the value set to when the user does whatever it is they need to do in order to set this value

  • from form1, take this global variable value back. Something like:

    'form1:

    Dim theForm2 as new Form2()

    theForm2.ShowDialog()

    txtDescricao.Text = theForm2ValueChosen

    'form2:

    'global:

    public theForm2ValueChosen as String

    ..

    ..

    'some method to set the form2value based on user action or something

    Me.theForm2ValueChosen = valueHere

    does this help






  • Re: Visual Basic Language Passing value from one form to another (tricky question!)

    DMan1

    Try this approach



    Module
    Module1
    Private m_Var As String
    Public Event VarChanged(ByVal s As String)
    Public Property Var() As String
    Get
    Return m_Var
    End Get
    Set(ByVal value As String)
    m_Var = value
    RaiseEvent VarChanged(m_Var)
    End Set
    end Property
    End Module

     


    Public Class Form1
     
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    AddHandler Module1.VarChanged, AddressOf theVarChanged

    End Sub

    Private Sub theVarChanged(ByVal s As String)
    Me.TextBox1.Text = s

    End Sub

     


    Public Class Form2

    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

    Module1.Var = Me.TreeView1.SelectedNode.Text

    End Sub
    End
    Class

     

     Edit...using the event instead of the global variable will auto update the textbox anytime the variable is changed...






    Re: Visual Basic Language Passing value from one form to another (tricky question!)

    Joao Pinto

    The previous code is working fine but now I need to improve it and add another function to it. When I choose a Node, I need to pass also the SelectedNode.Parent.Text to an ComboBox. Example:

    I select a family of problems like "Equipment" and choose a sub-node from the "Equipment" node like "Robot". With the previous code I was passing the value "Robot" to a textbox. Now I need to pass also the "Equipment" value to a combobox of problems on the same form.

    I've tryed to put the Parent.text value to a public variable and then where I've the code Me.Textox1.Text=s I've tryed to do

    Me.cboGrupoProblemas.SelectedItem = FamiliaNode

    but the FamiliaNode is null at this point!

    Can you help me please

    Thanks,

    Joao Pinto

    http://senhorpires.blogspot.com/





    Re: Visual Basic Language Passing value from one form to another (tricky question!)

    Joao Pinto

    DMan1, can you help, please

    Annyone can help

    Thanks,

    Joao Pinto

    http://senhorpires.blogspot.com/





    Re: Visual Basic Language Passing value from one form to another (tricky question!)

    Spidermans_DarkSide

    DMan1,

    Would this be any easier from a common CLASS being added instead of using a MODULE

    I notice you are still using GET and SET PROPERTY methods.

    Regards,

    S_DS






    Re: Visual Basic Language Passing value from one form to another (tricky question!)

    DMan1

    Depending on what you are wanting FamiliaNode to point to

    FamiliaNode=Me.Treeview.SelectedNode

    Or

    FamiliaNode=Me.treeview.SelectedNode.Parent






    Re: Visual Basic Language Passing value from one form to another (tricky question!)

    DMan1

    Spidermans_DarkSide wrote:
    Would this be any easier from a common CLASS being added instead of using a MODULE

    Easier ...NO...the nice thing about a module is that you don't have to instaniate it and you don't have to worry about its scope

    Spidermans_DarkSide wrote:
    I notice you are still using GET and SET PROPERTY methods.

    Versus what

    Remember that the idea here is to be able to automatically update multiple class fields from a single variable access...which in that case would be to fire an event that could be captured by each class that handles the event






    Re: Visual Basic Language Passing value from one form to another (tricky question!)

    Larry Charlton

    First, thanks DMan1, got me thinking about events more :)

    DMan1 demonstrated single instance, i.e. no matter how many Form1's I create changing Form2 will update them all. If you ever think you might reuse functionality, I'd go for a slight variation on that using classes.

    On Form1 stick a 2 buttons and 4 labels
    On Form2 stick two text boxes

    The difference if it matters is that Form2 can be created any number of times and bound to specific fields on various forms with no worries. In addition complex data can be sent back in response to specific events (a variable changing) or more general events (some variable changed). It's a bit more code mostly due to a more complex senario, but unless you really need two independent active forms ShowDialog is even shorter code :)

    Public Class Form1

    Private _first As Form2 = Nothing

    Private _second As Form2 = Nothing

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If _first Is Nothing Then

    _first = New Form2

    _first.Text = "Button1 Data"

    End If

    AddHandler _first.AVarChanged, AddressOf OnAVarChanged

    AddHandler _first.Ended, AddressOf OnEnded

    _first.Show()

    End Sub

    Public Sub OnAVarChanged(ByVal data As FormData)

    Label1.Text = data.MyVar

    Label2.Text = data.MyVar2

    End Sub

    Public Sub OnEnded()

    _first = Nothing

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    If _second Is Nothing Then

    _second = New Form2

    _second.Text = "Button2 Data"

    End If

    AddHandler _second.AVarChanged, AddressOf OnAVarChanged2

    AddHandler _second.Ended, AddressOf OnEnded2

    _second.Show()

    End Sub

    Public Sub OnAVarChanged2(ByVal data As FormData)

    Label3.Text = data.MyVar

    Label4.Text = data.MyVar2

    End Sub

    Public Sub OnEnded2()

    _second = Nothing

    End Sub

    End Class

    Public Class Form2

    Private _myValues As New FormData

    Public Event AVarChanged(ByVal v As FormData)

    Public Event Ended()

    Private Sub OnAVarChanged(ByVal v As FormData)

    RaiseEvent AVarChanged(v)

    End Sub

    Private Sub Form2_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

    RaiseEvent Ended()

    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    AddHandler _myValues.AVarChanged, AddressOf OnAVarChanged

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    _myValues.MyVar = TextBox1.Text

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

    _myValues.MyVar2 = TextBox2.Text

    End Sub

    End Class

    Public Class FormData

    Private _myVar As String

    Private _myVar2 As String

    Public Event MyVarChanged(ByVal v As FormData)

    Public Event MyVar2Changed(ByVal v As FormData)

    Public Event AVarChanged(ByVal v As FormData)

    Public Property MyVar() As String

    Get

    Return _myVar

    End Get

    Set(ByVal value As String)

    _myVar = value

    RaiseEvent MyVarChanged(Me)

    RaiseEvent AVarChanged(Me)

    End Set

    End Property

    Public Property MyVar2() As String

    Get

    Return _myVar2

    End Get

    Set(ByVal value As String)

    _myVar2 = value

    RaiseEvent MyVar2Changed(Me)

    RaiseEvent AVarChanged(Me)

    End Set

    End Property

    End Class

    You can retro fit this to DMan's example by just putting the _myValues from Form2 as a variable in a Module or as a shared variable in a class.

    Module Module1

    Public _myVar As New FormData

    End Module

    or just change Form2 as follows:

    Public Class Form2

    Private Shared _myValues As New FormData