none
how to share variables between forms without anyone calling (formx.Show) the other RRS feed

  • Question

  • Hello, my question is the following
    I have a main MDI form and this link or at the beginning I had it show the 2 form Form1 and Form2.

    Now on Form1 when I add text to the Textbox and click on the button, it will be shown in the other formula.
    I want to clarify that Form1 does not show Form2.

    '---->Form1
     Dim fom2 As New Form2
    fom2.Variable_create = "String in other"
    form2.Show()
    
    '-----> Form2
    Public Variable As String
    
    Public Form2_Load()
    Label1.Text = Variable
    End Sub
    For example in this code to share or edit Variable, you have to use .Show (), and that's what I do not want, what I want is to keep the Variable constant  and when you click on Button, you update the Variable and show the change that I made.


    • Edited by Royer12 Wednesday, December 19, 2018 10:34 PM edit info
    Wednesday, December 19, 2018 10:05 PM

Answers

  • Hi,

    Public Class Form2
        
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Form3.Show()
        End Sub
    
    End Class


    Public Class Form3
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Form2.Label1.Text = Me.TextBox1.Text
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Royer12 Wednesday, December 26, 2018 2:02 PM
    Wednesday, December 26, 2018 1:28 AM

All replies

  • Hi,

    try the code:

    Form2(child1):

    Public Class Form2
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim formChild2 As Form3 = DirectCast(Me.MdiParent.MdiChildren(1), Form3)
            formChild2.ReceiveValue(TextBox1.Text)
        End Sub
    End Class

    Form3(Child2):

    Public Class Form3
        Public Sub ReceiveValue(ByVal value As String)
            TextBox1.Text = value
            Me.Activate()
            Me.Refresh()
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Royer12 Friday, December 21, 2018 1:02 AM
    • Unmarked as answer by Royer12 Saturday, December 22, 2018 4:29 AM
    Thursday, December 20, 2018 5:51 AM
  • Hi, tank you.

    Can I also update the DataGridView in this way?

    Public Sub ReceiveValue(ByVal value As String)
            DataGridView.Update() ' <----
            Me.Activate()
            Me.Refresh()
        End Sub

    Thursday, December 20, 2018 8:50 PM
  • Hi,

    of course

    Form2:

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim formChild2 As Form3 = DirectCast(Me.MdiParent.MdiChildren(1), Form3)
            formChild2.ReceiveValue(Me.DataGridView1.DataSource)
        End Sub


    Form3:

    Public Class Form3
        Public Sub ReceiveValue(ByVal value As DataTable)
            Me.DataGridView1.DataSource = value
            Me.Activate()
            Me.Refresh()
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 21, 2018 3:17 AM

  • Hi,if it was not MDI, what would I change here?

       Dim formChild2 As Form3 = DirectCast(Me.MdiParent.MdiChildren(1), Form3)

    if the form1 is not MDI and the form2 is not and the form3 is not

    Saturday, December 22, 2018 4:35 AM
  • The calling procedure for the this example is shown below:

    Sub PassData1()
    Dim myFrm As UserForm1
    Dim strCaption As String
      'Get a dynamic value to be passed to the userform from the user.
      Do
        'Define
    dynamic caption for userform frame control.
        strCaption
    = InputBox("Enter a custom Frame1 Caption: ", _
            "Custom
    Caption", "Enter your shoe size e.g., 10D")
        'Did
    the user cancel the inputbox?
        If
    StrPtr(strCaption) = 0 Then Exit Sub
      Loop
    Until strCaption <> ""
      'Create\load
    the userform in memory.
      Set
    myFrm = New UserForm1
      With
    myFrm
        'Employ
    a direct reference a form built-in property.
        .Frame1.Caption
    = strCaption
        'Display
    the form.
        .Show
        If
    .Tag <> "Canceled" Then
          'Employ
    a direct reference to a form built-in property.
          MsgBox
    "You entered size " & UCase(.TextBox1.Text) & ".
    This" _
               &
    " is a valid size.", , "Data Returned"
        End
    If
      End
    With
      Set
    myFrm = Nothing
    End
    Sub

    This example uses a simple InputBox function to prompt the user for dynamic information to pass to the userform.  This information is used to set caption text for a userform control.



    The code for the userform is provided below:

    Private
    Sub cmdOK_Click()
      If
    fcnSizeValidator(Me.TextBox1.Text) Then
        Me.Hide
      Else
        With
    Me
          .Frame1.Caption
    = Me.TextBox1.Text & " is invalid." _
                          &
    " Please enter a valid size."
          With
    .TextBox1
            .SetFocus
            .SelStart
    = 0
            .SelLength
    = Len(.Text)
          End
    With
        End
    With
      End
    If
    lbl_Exit:
      Exit
    Sub
    End
    Sub
    
    
    Private
    Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
      'Intercept/repurpose
    Unload if user clicks form "X" close button.
      If
    CloseMode = 0 Then
        Cancel
    = True
        Me.Tag
    = "Canceled"
        Me.Hide
      End
    If
    lbl_Exit:
      Exit
    Sub
    End
    Sub
    Private
    Sub cmdOK_Click()
     If
    fcnSizeValidator(Me.TextBox1.Text) Then
        Me.Hide
      Else
        With
    Me
          .Frame1.Caption
    = Me.TextBox1.Text & " is invalid." _ & "
    Please enter a valid size."
          With
    .TextBox1
            .SetFocus
            .SelStart
    = 0
            .SelLength
    = Len(.Text)
          End
    With
        End
    With
      End
    If
    lbl_Exit:
      Exit
    Sub
    End
    Sub
    
    
    Private
    Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
      'Intercept/repurpose
    Unload if user clicks form "X" close button.
      If
    CloseMode = 0 Then
        Cancel
    = True
        Me.Tag
    = "Canceled"
        Me.Hide
      End
    If
    lbl_Exit:
      Exit
    Sub
    End
    Sub

    After the user enters a shoe size and clicks the userform "OK" command button, the userform cmdOK_Click procedure evaluates a valid data entry by passing the user entered data as as argument to a separate function procedure in a standard project module. The function procedure is shown below:

    Function fcnSizeValidator(strSize As String) As Boolean

    strSize = Ucase(strSize)

    Select Case True

    Case Is = Val(Left(strSize, 2)) <= 16 _

    And Val(Left(strSize, 2)) >= 2

    Select Case True

    Case Is = strSize Like "##[C-E]" Or _

    strSize Like "#.#[C-E]" Or _

    strSize Like "##.#[C-E]" Or _

    strSize Like "##[C-E][C-E]" Or _

    strSize Like "#.#[C-E][C-E]" Or _

    strSize Like "##.#[C-E][C-E]" Or _

    strSize Like "##[C-E][C-E][C-E]" Or _

    strSize Like "#.#[C-E][C-E][C-E]" Or _

    strSize Like "##.#[C-E][C-E][C-E]"

    fcnSizeValidator = True

    Case Else

    fcnSizeValidator = False

    End Select

    Case Else

    
    


    
    
    • Edited by ShrutiSigh Saturday, December 22, 2018 6:48 AM
    Saturday, December 22, 2018 6:48 AM

  • Hi,if it was not MDI, what would I change here?

       Dim formChild2 As Form3 = DirectCast(Me.MdiParent.MdiChildren(1), Form3)

    if the form1 is not MDI and the form2 is not and the form3 is not

    Hi,

    try the code:

    Imports System.Runtime.InteropServices
    
    Public Class Form2
        <DllImport("user32.dll")>
        Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        End Function
        Dim f3 As Form3
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim myIntPtr As IntPtr = FindWindow(Nothing, "Form3")
            f3 = Form3.FromHandle(myIntPtr)
            f3.TextBox1.Text = Me.TextBox1.Text
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Saturday, December 22, 2018 8:37 AM

  • Imports System.Runtime.InteropServices
    
    Public Class Form2
        <DllImport("user32.dll")>
        Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        End Function
        Dim f3 As Form3
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim myIntPtr As IntPtr = FindWindow(Nothing, "Form3")
            f3 = Form3.FromHandle(myIntPtr)
            f3.TextBox1.Text = Me.TextBox1.Text
        End Sub
    End Class

    Best Regards,

    Alex

    You can explain how it works, I do not understand so much, you are using the form2 towards the form3,

    in form2  I have:  ---> send

        TextBox

        Button -- action

    in form3 I have:    <--- receives

       Label

    Saturday, December 22, 2018 4:36 PM
  • Hi,

    Get the form handle to send a message,

    Form1:

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f2 = New Form2
            Dim f3 = New Form3
            f2.Show()
            f3.Show()
        End Sub
    End Class
    

    Form2:

    Imports System.Runtime.InteropServices
    
    Public Class Form2
        <DllImport("user32.dll")>
        Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        End Function
        Dim f3 As Form3
        'get the handle of Form3,"Test123" is Form3 title
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim myIntPtr As IntPtr = FindWindow(Nothing, "Test123")
            f3 = Form3.FromHandle(myIntPtr)
            f3.Label1.Text = Me.TextBox1.Text
        End Sub
    End Class

    Form3:

    Public Class Form3
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Text = "Test123"
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, December 23, 2018 4:19 AM
  • Hi,

    Get the form handle to send a message,

    Form1:

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f2 = New Form2
            Dim f3 = New Form3
            f2.Show()
            f3.Show()
        End Sub
    End Class

    Form2:

    Imports System.Runtime.InteropServices
    
    Public Class Form2
        <DllImport("user32.dll")>
        Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        End Function
        Dim f3 As Form3
        'get the handle of Form3,"Test123" is Form3 title
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim myIntPtr As IntPtr = FindWindow(Nothing, "Test123")
            f3 = Form3.FromHandle(myIntPtr)
            f3.Label1.Text = Me.TextBox1.Text
        End Sub
    End Class

    Form3:

    Public Class Form3
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Text = "Test123"
        End Sub
    End Class

    Best Regards,

    Alex

    Hello and if by chance there was no form1, what would your code change?

    • Edited by Royer12 Sunday, December 23, 2018 3:43 PM edit answer
    Sunday, December 23, 2018 3:03 PM
  • Hi,

    So what to open Form2 and Form3?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 24, 2018 1:24 AM
  • Hi,

    So what to open Form2 and Form3?

    Best Regards,

    Alex

    form2 opens to form3
    Monday, December 24, 2018 10:37 PM
  • Hi,

    it is the simplest,

    Public Class Form2
        Dim f3 As Form3 = New Form3
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            f3.Label1.Text = Me.TextBox1.Text
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            f3.Show()
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 25, 2018 1:43 AM
  • Hi,

    it is the simplest,

    Public Class Form2
        Dim f3 As Form3 = New Form3
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            f3.Label1.Text = Me.TextBox1.Text
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            f3.Show()
        End Sub
    End Class

    Best Regards,

    Alex

    what happens is that form2 calls form3 only once, but form2 sends the text continuously so, my question was that the form3 receives the data constantly and is updated.
    Tuesday, December 25, 2018 3:03 AM
  • like this:

    Public Class Form2
        Dim f3 As New Form3
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            f3.Show()
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            f3.Label1.Text = Me.TextBox1.Text
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Royer12 Tuesday, December 25, 2018 3:06 PM
    • Unmarked as answer by Royer12 Tuesday, December 25, 2018 3:27 PM
    Tuesday, December 25, 2018 3:28 AM
  • like this:

    Public Class Form2
        Dim f3 As New Form3
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            f3.Show()
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            f3.Label1.Text = Me.TextBox1.Text
        End Sub
    End Class

    Best Regards,

    Alex

    Hello, I would like to make a final query, if I am in the form3 and there I have a textbox and a button. Now I want to send form3 information to the form2, can it be possible?, 
    Tuesday, December 25, 2018 3:30 PM
  • Hi,

    Public Class Form2
        
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Form3.Show()
        End Sub
    
    End Class


    Public Class Form3
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Form2.Label1.Text = Me.TextBox1.Text
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Royer12 Wednesday, December 26, 2018 2:02 PM
    Wednesday, December 26, 2018 1:28 AM
  • Tank You   ----> Alex <---
    Wednesday, December 26, 2018 2:03 PM