Results 1 to 9 of 9

Thread: [RESOLVED][02/03] how to pass variable from one form to another form

  1. #1

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    [RESOLVED][02/03] how to pass variable from one form to another form

    Hi all,

    Now i'm facing a problem when i passing a variable from one form to another form, i can't get the value.

    I used the method of property.
    vb code
    Form A
    Code:
    Private UserID as string
    Private Sub New()
       Me.User_ID = textbox1.text
    End Sub
    Public property User_ID() as string
       Get 
           Return UserID
       End Get
       Set (ByVal Value As String)
           UserID = Value
       End Set
    end property
    Form B --> Try to get value from Variable form A

    Code:
        dim frm as new FormA
        textbox1.text = frm.User_ID
    Last edited by nUflAvOrS; Aug 10th, 2007 at 07:08 PM.
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  2. #2
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: [02/03] how to pass variable from one form to another form

    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  3. #3

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: [02/03] how to pass variable from one form to another form

    Thx Andy showed me the link.
    I faced the problem while passing value from parent to child .
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  4. #4
    Registered User RaviIntegra's Avatar
    Join Date
    Mar 2007
    Location
    Pondicherry, India
    Posts
    125

    Re: [02/03] how to pass variable from one form to another form

    Assign a variable as public in Form A and then assign some values to that variable and call that variable from Form B.

    Example

    Public test as string="Testing"
    'Write this code in Form A and write the following line in Form B

    messagebox.show(FormA.test)

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [02/03] how to pass variable from one form to another form

    You had it backwards in the constructor. It should be
    Code:
    Private Sub New()
       Me.textbox1.text = UserID
    End Sub
    And in formA, when you instantiate formB, you should set the User_ID property of formB
    Code:
    'This is done in formA
    Dim frmB As New FormB
    frmB.User_ID = "whatever id you want to put in here"
    frmB.Show()

  6. #6

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: [02/03] how to pass variable from one form to another form

    Thx stanax,

    I ever tried your method, but when everytime i need passing variables i have to create a lot of property.Furthermore, if i create a property inside a class why i cannot used the class public property to store the value when program is running ?

    Stanax i rated you ... thanks a lot
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  7. #7

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: [02/03]Please Help how to pass variable from one form to another form

    My project contains a class store the public variable

    In VB 6 i always used
    Code:
    Public Variable as string
    to assign a variable to store value along project is running.

    In .net i'm still new

    I tried create a class but still cannot obtains the value

    Class
    Code:
    Public Class VariableStorage
        Private UserID As String
        Public Sub New()
            MyBase.new()
        End Sub
        Public Property User_ID() As String
            Get
                Return UserID
            End Get
            Set(ByVal Value As String)
                UserID = Value
            End Set
        End Property
    
    End Class

    Code:
     Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
       Dim USerID As New VariableStorage
       USerID.User_ID = Me.txtUserID.Text.ToString
     end sub
    Code:
    Private Sub mdiForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim UserID As New VariableStorage
           
        Me.sbpMdiForm.Panels.Item(0).Text = "User ID:  " & UserID.User_ID
       END SUB
    thanks !!!!!
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [02/03] how to pass variable from one form to another form

    Quote Originally Posted by nUflAvOrS
    Thx stanax,

    I ever tried your method, but when everytime i need passing variables i have to create a lot of property.Furthermore, if i create a property inside a class why i cannot used the class public property to store the value when program is running ?

    Stanax i rated you ... thanks a lot
    Unless it is absolutely needed, I tend not to use public variables because they're considered as bad programming practice... Why is it bad? If you just google for "why are public variables bad?" and you'll get the image

  9. #9

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: [RESOLVED][02/03] how to pass variable from one form to another form

    THANKS stanav ,, ;-) i will take ur advise
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width