Design time support for instance properties

  • 14 years ago

    Hi All

    Im new to VB.net and .net development

    How do I access any properties of a an Instance of a form, from another form or child form.

    1: At design time

    2: At runtime

    eg If I attempt to code the following

    me.parent.GETCURRENTREFNUM where GETCURRENTREFNUM is a property of an parent instance of another form ...it wont work as I cannot code for GETCURRENTREFNUM at design time

    HTMS

    VB2005, XP

    many Thanks

  • 14 years ago
    You'll need a reference to whatever form you want to access.  If you need form1 to create form2 and form2 should access properties on form1 then you might try passing form1 in the constructor.  Here is some code that demonstates this.
    Public Class Form2
        Private _parent As Form1
    
        'Accepts Form1
        Public Sub New(ByVal parent As Form1)
            InitializeComponent()
    
            'Sets parent
            _parent = parent
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Displays the text of the parent
            MsgBox(_parent.Text)
        End Sub
    End Class
    This is what form1's code might look like.
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
           'Create instance of form2 and pass a reference to this form
            Dim frm As New Form2(Me)
            frm.Show()
        End Sub
    End Class
  • 14 years ago

    Thank you for that.

    Unfortunatly this does not expose  my methods and properties on Form1 to Form2 at design time.

    Once again thanks

  • 14 years ago
    It will if they are declared public.  If you still can't get it working try posting some relevent code.  Maybe we can figure out what's going on.  Also just to make sure try creating an instance of form1 and see if you properties and methods show up.  If they are public they'll be there.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“XML is like violence - if it's not working for you, you're not using enough of it.”