Results 1 to 5 of 5

Thread: Passing properties between forms

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    26

    Passing properties between forms

    Actually it has for a long time been considered bad practice to be able to reach the properties and method of controls from an other form.
    A control should always be considered to be private.
    In VB.Net this has been enforced so all controls are private to the form that host them.
    So if you need to pass values between forms you should write your own wrapper property procedures.

    For example: You have a TextBox on Form1 and you need to get the value of that textbox in Form2.
    Then you could add the following to Form1:

    visual basic code:

    Public Property TextBoxContent() As String
    Get
    TextBoxContent = TextBox1.Text
    End Get
    Set(ByVal Value As String)
    TextBox1.Text = Value
    End Set
    End Property
    Since this property is Public it can be reached from Form2

    visual basic code:

    MsgBox Form1.TextBoxContent
    Best regards
    I've tried this and it did not work for me.

    An unhandled exception of type 'System.NullReferenceException' occurred in 6CUL1763.exe

    Additional information: Object reference not set to an instance of an object.

    is the error that I got. otherwise the only thing that I changed was the property name.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    ok, when loading Form2 ( from a button / sub in Form1 ) ...
    VB Code:
    1. [COLOR=blue]Dim[/color] frm2 [COLOR=blue]As New[/COLOR] Form2()
    2. [COLOR=blue]MyBase[/color].AddOwnedForm(frm2)
    3. frm2.Show()
    then in Form2 ( to access the controls on Form1 , again in a button click / sub ) ...
    VB Code:
    1. [COLOR=blue]Dim[/color] frmMain [COLOR=blue]As[/color] Form1 = [COLOR=blue]DirectCast[/color]([COLOR=blue]Me[/color].Owner,Form)
    2. TextBox.Text = frmMain.TextBox1.Text
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3
    Addicted Member Hole-In-One's Avatar
    Join Date
    Mar 2003
    Location
    Minnesota
    Posts
    195
    Whats up with the "DirectCast"?


    Ive always just used :

    Dim frm as Form1= Me.Owner

    What is the difference, if any?

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    try switching Option Strict On and see
    Dim frm as Form1= Me.Owner will throw an error , as it's a bad cast , the correct vb.net syntax is using DirectCast
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  5. #5
    Addicted Member Hole-In-One's Avatar
    Join Date
    Mar 2003
    Location
    Minnesota
    Posts
    195


    Thanks For the Explanation

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