How to get the textbox value of other form

With VB 6 there was no problem getting a value of the control on other form. Now I failed to do it. I think the following simple code should work but VB.NET won't get me the value. I have a textbox1 on a form FrmA and I need the value of the textbox1 to use as a parameter of a query on a form FrmB.

When the FrmB loads I put the following

Dim FrmA As New FrmA
Dim strA As String
strA=FrmA.TextBox1.Text.ToString

Why strA doesn't get any value  Help!


Answer this question

How to get the textbox value of other form

  • Nigel36

    There's your problem:

    Dim FrmA As New FrmA

    FrmB creates it's own instance of FrmA. When you open FrmB you need to pass the instance of FrmA along:

    -- FormB --

    Class FormB ...
    Public a As FormA
    ...
    strA = a.TextBox1.Text


    -- FormA --

    Dim FrmB As New FormB
    FrmB.A = Me
    FrmB.Show



    It probably takes some getting used to that you can have multiple instances of the same form open...

  • Matthew Langley

    Dear DanielRieck,

    Thank you so much Daniel! I wish I were so good at .net coding as you. Thank you again.

  • nscripta

    These 3 lines are on FrmB_Load

    Dim FrmA As New FrmA 
    Dim strA As String 
    strA=FrmA.TextBox1.Text.ToString 

    Btn_Click event on FrmA includes:

    Dim FrmB As New FrmB
    FrmB.Show()

    I really want to know why it doesn't work.

  • Marcos Antonio

    Two questions:
    1.) Is FrmA.TextBox1 public
    2.) How are you checking the value of strA

    -Ari

  • Colvic


    Dim FrmA As New FrmA 
    Dim strA As String 
    strA=FrmA.TextBox1.Text.ToString 


    do you have a FrmA.Show() before the 
    strA=FrmA.TextBox1.Text.ToString 

  • RBen

    Are you trying to get the value of a parent form from the dialog popup form or vice versa

    If you are trying to get the value of a variable in the parent form class (from the popup dialog), then pass the form instance (using the this keyword) to the constructor of the dialog form, then use to reference the variable in the parent form.


  • etrynus

    <hr>quote <strong>shinemakers</strong>

    Dim FrmA As New FrmA 
    Dim strA As String 
    strA=FrmA.TextBox1.Text.ToString 

    Btn_Click event on FrmA includes: 

    Dim FrmB As New FrmB 
    FrmB.Show()<hr>
    Here's what you're doing:

    1. Button event in FrmA instantiates a FrmB object. And shows it.
    2. FrmB on show (OnLoad) instantiates <em>another</em> FrmA object. This is different from the original FrmA object that instantiated it. The TextBox value in this new FrmA is naturally not the same as the original FrmA object.

    What you want is to have FrmA pass its own reference into FrmB's constructor, so that FrmB knows <em>which</em> FrmA object spawn it, and thus have a relationship and communication link.Public Class FrmB Inherits System.Windows.Forms.Form

    Public Sub New(FrmA caller)
    me._frmA = caller
    End Suband reference me._frmA later.

  • How to get the textbox value of other form