Multiple Forms - VB.Net

Discussion in 'MCSD' started by Quest, Sep 30, 2003.

  1. Quest

    Quest Guest

    Hi,



    I would appreciate any input on this problem. (Visual Basic.NET Windows
    Application)



    Facts:

    I have created a new windows project from Visual Studio.net called
    'Database2'.

    On the default created form called Form1 I have created a textbox called
    TextBox1 (at design time)

    I have created from Solution Explorer a new Module called Module1 and a new
    form called Form2. (at design time)



    Problem:

    How do I access Textbox1 residing on Form1 FROM either Form2 or Module1. It
    must however, reference the ORIGINAL instance of the form created at design
    time (not a new instance).



    I have tried the following (both give an error message) code on Form2 :



    Database2.form1.Textbox1.text="test"

    Form1.Textbox1.text="test"



    I can refer to a new instance of the form1 with the following code



    Dim myform as new Form1



    But then it would NOT be referring to the ORGINAL instance of the form.



    Any ideas on how to access the Textbox1 on Form1 created at design time
    without instantiating a new instance of Form1?



    Thank you.
    Quest, Sep 30, 2003
    #1
    1. Advertising

  2. Quest

    Kumar Guest

    If you open the code for Form1, it may look like the
    following:

    Public Class Form1
    Inherits System.Windows.Forms.Form
    .....
    ....

    Which means that the form you see on the designer is
    nothing by an instance of the Form1 class. Visual Studio
    does a special job for you to show the form in design
    time. If you want refer any form you need to create an
    instance of it.

    I hope this helps.

    -Kumar

    >-----Original Message-----
    >Hi,
    >
    >
    >
    >I would appreciate any input on this problem. (Visual

    Basic.NET Windows
    >Application)
    >
    >
    >
    >Facts:
    >
    >I have created a new windows project from Visual

    Studio.net called
    >'Database2'.
    >
    >On the default created form called Form1 I have created a

    textbox called
    >TextBox1 (at design time)
    >
    >I have created from Solution Explorer a new Module called

    Module1 and a new
    >form called Form2. (at design time)
    >
    >
    >
    >Problem:
    >
    >How do I access Textbox1 residing on Form1 FROM either

    Form2 or Module1. It
    >must however, reference the ORIGINAL instance of the form

    created at design
    >time (not a new instance).
    >
    >
    >
    >I have tried the following (both give an error message)

    code on Form2 :
    >
    >
    >
    >Database2.form1.Textbox1.text="test"
    >
    >Form1.Textbox1.text="test"
    >
    >
    >
    >I can refer to a new instance of the form1 with the

    following code
    >
    >
    >
    >Dim myform as new Form1
    >
    >
    >
    >But then it would NOT be referring to the ORGINAL

    instance of the form.
    >
    >
    >
    >Any ideas on how to access the Textbox1 on Form1 created

    at design time
    >without instantiating a new instance of Form1?
    >
    >
    >
    >Thank you.
    >
    >
    >.
    >
    Kumar, Sep 30, 2003
    #2
    1. Advertising

  3. Quest

    Quest Guest

    Ok. But the point is this : When you run the application, you are viewing
    the instance of the form that you created at design time. The question is,
    how do you access this SAME instance in code ??
    If I can access the same instance of the form in code, then I can access the
    texbox1 on form1 that the user first sees when the application is run.

    "Kumar" <> wrote in message
    news:131f01c3878f$c05f25c0$...
    > If you open the code for Form1, it may look like the
    > following:
    >
    > Public Class Form1
    > Inherits System.Windows.Forms.Form
    > ....
    > ...
    >
    > Which means that the form you see on the designer is
    > nothing by an instance of the Form1 class. Visual Studio
    > does a special job for you to show the form in design
    > time. If you want refer any form you need to create an
    > instance of it.
    >
    > I hope this helps.
    >
    > -Kumar
    >
    > >-----Original Message-----
    > >Hi,
    > >
    > >I would appreciate any input on this problem. (Visual

    > Basic.NET Windows
    > >Application)
    > >
    > >Facts:
    > >
    > >I have created a new windows project from Visual

    > Studio.net called 'Database2'.


    > >On the default created form called Form1 I have created a

    > textbox called TextBox1 (at design time)


    > >I have created from Solution Explorer a new Module called

    > Module1 and a new
    > >form called Form2. (at design time)
    > >
    > >Problem:
    > >
    > >How do I access Textbox1 residing on Form1 FROM either

    > Form2 or Module1. It
    > >must however, reference the ORIGINAL instance of the form

    > created at design
    > >time (not a new instance).
    > >
    > >I have tried the following (both give an error message)

    > code on Form2 :
    > >
    > >Database2.form1.Textbox1.text="test"
    > >Form1.Textbox1.text="test"
    > >
    > >I can refer to a new instance of the form1 with the

    > following code
    > >
    > >Dim myform as new Form1


    > >But then it would NOT be referring to the ORGINAL

    > instance of the form.
    > >
    > >Any ideas on how to access the Textbox1 on Form1 created

    > at design time without instantiating a new instance of Form1?
    > >
    > >Thank you.
    Quest, Oct 1, 2003
    #3
  4. Quest

    Jim Parker Guest

    The controls on forms are no longer public as they were in VB6.
    Create public properties on Form1 to share the info that is desired to share
    (textBox1.Text, whatever).
    Create a property of type Form1 on Form2, and set it to the already created
    instance of Form1 when an instance of Form2 is created.
    Then you can easily access the public items on Form1 from Form2.

    JD.

    "Quest" <> wrote in message
    news:Uszeb.831$...
    > Ok. But the point is this : When you run the application, you are viewing
    > the instance of the form that you created at design time. The question is,
    > how do you access this SAME instance in code ??
    > If I can access the same instance of the form in code, then I can access

    the
    > texbox1 on form1 that the user first sees when the application is run.
    >
    > "Kumar" <> wrote in message
    > news:131f01c3878f$c05f25c0$...
    > > If you open the code for Form1, it may look like the
    > > following:
    > >
    > > Public Class Form1
    > > Inherits System.Windows.Forms.Form
    > > ....
    > > ...
    > >
    > > Which means that the form you see on the designer is
    > > nothing by an instance of the Form1 class. Visual Studio
    > > does a special job for you to show the form in design
    > > time. If you want refer any form you need to create an
    > > instance of it.
    > >
    > > I hope this helps.
    > >
    > > -Kumar
    > >
    > > >-----Original Message-----
    > > >Hi,
    > > >
    > > >I would appreciate any input on this problem. (Visual

    > > Basic.NET Windows
    > > >Application)
    > > >
    > > >Facts:
    > > >
    > > >I have created a new windows project from Visual

    > > Studio.net called 'Database2'.

    >
    > > >On the default created form called Form1 I have created a

    > > textbox called TextBox1 (at design time)

    >
    > > >I have created from Solution Explorer a new Module called

    > > Module1 and a new
    > > >form called Form2. (at design time)
    > > >
    > > >Problem:
    > > >
    > > >How do I access Textbox1 residing on Form1 FROM either

    > > Form2 or Module1. It
    > > >must however, reference the ORIGINAL instance of the form

    > > created at design
    > > >time (not a new instance).
    > > >
    > > >I have tried the following (both give an error message)

    > > code on Form2 :
    > > >
    > > >Database2.form1.Textbox1.text="test"
    > > >Form1.Textbox1.text="test"
    > > >
    > > >I can refer to a new instance of the form1 with the

    > > following code
    > > >
    > > >Dim myform as new Form1

    >
    > > >But then it would NOT be referring to the ORGINAL

    > > instance of the form.
    > > >
    > > >Any ideas on how to access the Textbox1 on Form1 created

    > > at design time without instantiating a new instance of Form1?
    > > >
    > > >Thank you.

    >
    >
    Jim Parker, Oct 1, 2003
    #4
  5. Quest

    Jim Parker Guest

    BTW,
    This answers your question, but it is not a good OO practice.

    JD.

    "Jim Parker" <> wrote in message
    news:...
    > The controls on forms are no longer public as they were in VB6.
    > Create public properties on Form1 to share the info that is desired to

    share
    > (textBox1.Text, whatever).
    > Create a property of type Form1 on Form2, and set it to the already

    created
    > instance of Form1 when an instance of Form2 is created.
    > Then you can easily access the public items on Form1 from Form2.
    >
    > JD.
    >
    > "Quest" <> wrote in message
    > news:Uszeb.831$...
    > > Ok. But the point is this : When you run the application, you are

    viewing
    > > the instance of the form that you created at design time. The question

    is,
    > > how do you access this SAME instance in code ??
    > > If I can access the same instance of the form in code, then I can access

    > the
    > > texbox1 on form1 that the user first sees when the application is run.
    > >
    > > "Kumar" <> wrote in message
    > > news:131f01c3878f$c05f25c0$...
    > > > If you open the code for Form1, it may look like the
    > > > following:
    > > >
    > > > Public Class Form1
    > > > Inherits System.Windows.Forms.Form
    > > > ....
    > > > ...
    > > >
    > > > Which means that the form you see on the designer is
    > > > nothing by an instance of the Form1 class. Visual Studio
    > > > does a special job for you to show the form in design
    > > > time. If you want refer any form you need to create an
    > > > instance of it.
    > > >
    > > > I hope this helps.
    > > >
    > > > -Kumar
    > > >
    > > > >-----Original Message-----
    > > > >Hi,
    > > > >
    > > > >I would appreciate any input on this problem. (Visual
    > > > Basic.NET Windows
    > > > >Application)
    > > > >
    > > > >Facts:
    > > > >
    > > > >I have created a new windows project from Visual
    > > > Studio.net called 'Database2'.

    > >
    > > > >On the default created form called Form1 I have created a
    > > > textbox called TextBox1 (at design time)

    > >
    > > > >I have created from Solution Explorer a new Module called
    > > > Module1 and a new
    > > > >form called Form2. (at design time)
    > > > >
    > > > >Problem:
    > > > >
    > > > >How do I access Textbox1 residing on Form1 FROM either
    > > > Form2 or Module1. It
    > > > >must however, reference the ORIGINAL instance of the form
    > > > created at design
    > > > >time (not a new instance).
    > > > >
    > > > >I have tried the following (both give an error message)
    > > > code on Form2 :
    > > > >
    > > > >Database2.form1.Textbox1.text="test"
    > > > >Form1.Textbox1.text="test"
    > > > >
    > > > >I can refer to a new instance of the form1 with the
    > > > following code
    > > > >
    > > > >Dim myform as new Form1

    > >
    > > > >But then it would NOT be referring to the ORGINAL
    > > > instance of the form.
    > > > >
    > > > >Any ideas on how to access the Textbox1 on Form1 created
    > > > at design time without instantiating a new instance of Form1?
    > > > >
    > > > >Thank you.

    > >
    > >

    >
    >
    Jim Parker, Oct 1, 2003
    #5
  6. Quest

    Quest Guest

    Thank you.
    I think you have the right idea. With reference to your quote where you say
    "Create a property of type Form1 on Form2, and set it to the already created
    instance of Form1 ....", My question still stands as to how you access that
    instance of Form1 in code ?
    Remember, that Form1 is a 'type' and not an instance. Therefore we are
    trying to access the instance of form1 that is automatically created by VB
    at runtime that is based on our design time creation of Form1.

    In form2, I have added the following code :

    Public Property myForm() As Form1
    Get

    myForm = Form1

    End Get

    Set(ByVal Value As Form1)

    End Set

    End Property

    This gives an error message that Form1 is a type ! How do I access the
    instance of Form1 from Form2 ?




    "Jim Parker" <> wrote in message
    news:...
    > The controls on forms are no longer public as they were in VB6.
    > Create public properties on Form1 to share the info that is desired to

    share
    > (textBox1.Text, whatever).
    > Create a property of type Form1 on Form2, and set it to the already

    created
    > instance of Form1 when an instance of Form2 is created.
    > Then you can easily access the public items on Form1 from Form2.
    >
    > JD.
    >
    > "Quest" <> wrote in message
    > news:Uszeb.831$...
    > > Ok. But the point is this : When you run the application, you are

    viewing
    > > the instance of the form that you created at design time. The question

    is,
    > > how do you access this SAME instance in code ??
    > > If I can access the same instance of the form in code, then I can access

    > the
    > > texbox1 on form1 that the user first sees when the application is run.
    > >
    > > "Kumar" <> wrote in message
    > > news:131f01c3878f$c05f25c0$...
    > > > If you open the code for Form1, it may look like the
    > > > following:
    > > >
    > > > Public Class Form1
    > > > Inherits System.Windows.Forms.Form
    > > > ....
    > > > ...
    > > >
    > > > Which means that the form you see on the designer is
    > > > nothing by an instance of the Form1 class. Visual Studio
    > > > does a special job for you to show the form in design
    > > > time. If you want refer any form you need to create an
    > > > instance of it.
    > > >
    > > > I hope this helps.
    > > >
    > > > -Kumar
    > > >
    > > > >-----Original Message-----
    > > > >Hi,
    > > > >
    > > > >I would appreciate any input on this problem. (Visual
    > > > Basic.NET Windows
    > > > >Application)
    > > > >
    > > > >Facts:
    > > > >
    > > > >I have created a new windows project from Visual
    > > > Studio.net called 'Database2'.

    > >
    > > > >On the default created form called Form1 I have created a
    > > > textbox called TextBox1 (at design time)

    > >
    > > > >I have created from Solution Explorer a new Module called
    > > > Module1 and a new
    > > > >form called Form2. (at design time)
    > > > >
    > > > >Problem:
    > > > >
    > > > >How do I access Textbox1 residing on Form1 FROM either
    > > > Form2 or Module1. It
    > > > >must however, reference the ORIGINAL instance of the form
    > > > created at design
    > > > >time (not a new instance).
    > > > >
    > > > >I have tried the following (both give an error message)
    > > > code on Form2 :
    > > > >
    > > > >Database2.form1.Textbox1.text="test"
    > > > >Form1.Textbox1.text="test"
    > > > >
    > > > >I can refer to a new instance of the form1 with the
    > > > following code
    > > > >
    > > > >Dim myform as new Form1

    > >
    > > > >But then it would NOT be referring to the ORGINAL
    > > > instance of the form.
    > > > >
    > > > >Any ideas on how to access the Textbox1 on Form1 created
    > > > at design time without instantiating a new instance of Form1?
    > > > >
    > > > >Thank you.

    > >
    > >

    >
    >
    Quest, Oct 1, 2003
    #6
  7. Quest

    Jim Parker Guest

    You wrote:
    > Public Property myForm() As Form1
    > Get
    >
    > myForm = Form1
    >
    > End Get


    No, that won't work.
    Keep in mind that this blows in the face of good OO practices...
    Here is a crude example in C#; in VB.Net it would be very similar.

    In Form1, the form that is already instantiated:

    private Form2 frmTest;
    private void button1_Click(object sender, System.EventArgs e)
    {
    frmTest = new Form2();
    frmTest.MyForm(this);
    frmTest.Show();
    }

    In Form2, which is instantiated from Form1:

    private Form1 frmMyForm;
    public void MyForm(Form1 frmIn)
    {
    frmMyForm = frmIn;
    }

    Then in the instance of Form2 you can access anything declared as public in
    Form1 by accessing it as frmMyForm.

    JD.



    "Quest" <> wrote in message
    news:kYBeb.1149$...
    > Thank you.
    > I think you have the right idea. With reference to your quote where you

    say
    > "Create a property of type Form1 on Form2, and set it to the already

    created
    > instance of Form1 ....", My question still stands as to how you access

    that
    > instance of Form1 in code ?
    > Remember, that Form1 is a 'type' and not an instance. Therefore we are
    > trying to access the instance of form1 that is automatically created by VB
    > at runtime that is based on our design time creation of Form1.
    >
    > In form2, I have added the following code :
    >
    > Public Property myForm() As Form1
    > Get
    >
    > myForm = Form1
    >
    > End Get
    >
    > Set(ByVal Value As Form1)
    >
    > End Set
    >
    > End Property
    >
    > This gives an error message that Form1 is a type ! How do I access the
    > instance of Form1 from Form2 ?
    >
    >
    >
    >
    > "Jim Parker" <> wrote in message
    > news:...
    > > The controls on forms are no longer public as they were in VB6.
    > > Create public properties on Form1 to share the info that is desired to

    > share
    > > (textBox1.Text, whatever).
    > > Create a property of type Form1 on Form2, and set it to the already

    > created
    > > instance of Form1 when an instance of Form2 is created.
    > > Then you can easily access the public items on Form1 from Form2.
    > >
    > > JD.
    > >
    > > "Quest" <> wrote in message
    > > news:Uszeb.831$...
    > > > Ok. But the point is this : When you run the application, you are

    > viewing
    > > > the instance of the form that you created at design time. The question

    > is,
    > > > how do you access this SAME instance in code ??
    > > > If I can access the same instance of the form in code, then I can

    access
    > > the
    > > > texbox1 on form1 that the user first sees when the application is run.
    > > >
    > > > "Kumar" <> wrote in message
    > > > news:131f01c3878f$c05f25c0$...
    > > > > If you open the code for Form1, it may look like the
    > > > > following:
    > > > >
    > > > > Public Class Form1
    > > > > Inherits System.Windows.Forms.Form
    > > > > ....
    > > > > ...
    > > > >
    > > > > Which means that the form you see on the designer is
    > > > > nothing by an instance of the Form1 class. Visual Studio
    > > > > does a special job for you to show the form in design
    > > > > time. If you want refer any form you need to create an
    > > > > instance of it.
    > > > >
    > > > > I hope this helps.
    > > > >
    > > > > -Kumar
    > > > >
    > > > > >-----Original Message-----
    > > > > >Hi,
    > > > > >
    > > > > >I would appreciate any input on this problem. (Visual
    > > > > Basic.NET Windows
    > > > > >Application)
    > > > > >
    > > > > >Facts:
    > > > > >
    > > > > >I have created a new windows project from Visual
    > > > > Studio.net called 'Database2'.
    > > >
    > > > > >On the default created form called Form1 I have created a
    > > > > textbox called TextBox1 (at design time)
    > > >
    > > > > >I have created from Solution Explorer a new Module called
    > > > > Module1 and a new
    > > > > >form called Form2. (at design time)
    > > > > >
    > > > > >Problem:
    > > > > >
    > > > > >How do I access Textbox1 residing on Form1 FROM either
    > > > > Form2 or Module1. It
    > > > > >must however, reference the ORIGINAL instance of the form
    > > > > created at design
    > > > > >time (not a new instance).
    > > > > >
    > > > > >I have tried the following (both give an error message)
    > > > > code on Form2 :
    > > > > >
    > > > > >Database2.form1.Textbox1.text="test"
    > > > > >Form1.Textbox1.text="test"
    > > > > >
    > > > > >I can refer to a new instance of the form1 with the
    > > > > following code
    > > > > >
    > > > > >Dim myform as new Form1
    > > >
    > > > > >But then it would NOT be referring to the ORGINAL
    > > > > instance of the form.
    > > > > >
    > > > > >Any ideas on how to access the Textbox1 on Form1 created
    > > > > at design time without instantiating a new instance of Form1?
    > > > > >
    > > > > >Thank you.
    > > >
    > > >

    > >
    > >

    >
    >
    Jim Parker, Oct 1, 2003
    #7
  8. If Form1 is creating Form2, give Form2 an overloaded constructor that takes
    a reference to Form1. Either that or use a callback with a delegate.

    You can't reference Form1.xxx or Form2.xxx because they're not static
    members. You need an actual instance.

    "Quest" <> wrote in message
    news:CDheb.1018$...
    > Hi,
    >
    >
    >
    > I would appreciate any input on this problem. (Visual Basic.NET Windows
    > Application)
    >
    >
    >
    > Facts:
    >
    > I have created a new windows project from Visual Studio.net called
    > 'Database2'.
    >
    > On the default created form called Form1 I have created a textbox called
    > TextBox1 (at design time)
    >
    > I have created from Solution Explorer a new Module called Module1 and a

    new
    > form called Form2. (at design time)
    >
    >
    >
    > Problem:
    >
    > How do I access Textbox1 residing on Form1 FROM either Form2 or Module1.

    It
    > must however, reference the ORIGINAL instance of the form created at

    design
    > time (not a new instance).
    >
    >
    >
    > I have tried the following (both give an error message) code on Form2 :
    >
    >
    >
    > Database2.form1.Textbox1.text="test"
    >
    > Form1.Textbox1.text="test"
    >
    >
    >
    > I can refer to a new instance of the form1 with the following code
    >
    >
    >
    > Dim myform as new Form1
    >
    >
    >
    > But then it would NOT be referring to the ORGINAL instance of the form.
    >
    >
    >
    > Any ideas on how to access the Textbox1 on Form1 created at design time
    > without instantiating a new instance of Form1?
    >
    >
    >
    > Thank you.
    >
    >
    General Protection Fault, Oct 1, 2003
    #8
  9. Quest

    Quest Guest

    Yes it does work !!!
    Thank you.

    Lesson to be learnt here is that you can pass the instance of the Form1 as a
    parameter, in this case we have used MyForm(this) or in VB.NET it is
    MyForm(Me). Great.
    Once again thank you for the insight.

    "Jim Parker" <> wrote in message
    news:...
    > You wrote:
    > > Public Property myForm() As Form1
    > > Get
    > >
    > > myForm = Form1
    > >
    > > End Get

    >
    > No, that won't work.
    > Keep in mind that this blows in the face of good OO practices...
    > Here is a crude example in C#; in VB.Net it would be very similar.
    >
    > In Form1, the form that is already instantiated:
    >
    > private Form2 frmTest;
    > private void button1_Click(object sender, System.EventArgs e)
    > {
    > frmTest = new Form2();
    > frmTest.MyForm(this);
    > frmTest.Show();
    > }
    >
    > In Form2, which is instantiated from Form1:
    >
    > private Form1 frmMyForm;
    > public void MyForm(Form1 frmIn)
    > {
    > frmMyForm = frmIn;
    > }
    >
    > Then in the instance of Form2 you can access anything declared as public

    in
    > Form1 by accessing it as frmMyForm.
    >
    > JD.
    >
    >
    >
    > "Quest" <> wrote in message
    > news:kYBeb.1149$...
    > > Thank you.
    > > I think you have the right idea. With reference to your quote where you

    > say
    > > "Create a property of type Form1 on Form2, and set it to the already

    > created
    > > instance of Form1 ....", My question still stands as to how you access

    > that
    > > instance of Form1 in code ?
    > > Remember, that Form1 is a 'type' and not an instance. Therefore we are
    > > trying to access the instance of form1 that is automatically created by

    VB
    > > at runtime that is based on our design time creation of Form1.
    > >
    > > In form2, I have added the following code :
    > >
    > > Public Property myForm() As Form1
    > > Get
    > >
    > > myForm = Form1
    > >
    > > End Get
    > >
    > > Set(ByVal Value As Form1)
    > >
    > > End Set
    > >
    > > End Property
    > >
    > > This gives an error message that Form1 is a type ! How do I access the
    > > instance of Form1 from Form2 ?
    > >
    > >
    > >
    > >
    > > "Jim Parker" <> wrote in message
    > > news:...
    > > > The controls on forms are no longer public as they were in VB6.
    > > > Create public properties on Form1 to share the info that is desired to

    > > share
    > > > (textBox1.Text, whatever).
    > > > Create a property of type Form1 on Form2, and set it to the already

    > > created
    > > > instance of Form1 when an instance of Form2 is created.
    > > > Then you can easily access the public items on Form1 from Form2.
    > > >
    > > > JD.
    > > >
    > > > "Quest" <> wrote in message
    > > > news:Uszeb.831$...
    > > > > Ok. But the point is this : When you run the application, you are

    > > viewing
    > > > > the instance of the form that you created at design time. The

    question
    > > is,
    > > > > how do you access this SAME instance in code ??
    > > > > If I can access the same instance of the form in code, then I can

    > access
    > > > the
    > > > > texbox1 on form1 that the user first sees when the application is

    run.
    > > > >
    > > > > "Kumar" <> wrote in message
    > > > > news:131f01c3878f$c05f25c0$...
    > > > > > If you open the code for Form1, it may look like the
    > > > > > following:
    > > > > >
    > > > > > Public Class Form1
    > > > > > Inherits System.Windows.Forms.Form
    > > > > > ....
    > > > > > ...
    > > > > >
    > > > > > Which means that the form you see on the designer is
    > > > > > nothing by an instance of the Form1 class. Visual Studio
    > > > > > does a special job for you to show the form in design
    > > > > > time. If you want refer any form you need to create an
    > > > > > instance of it.
    > > > > >
    > > > > > I hope this helps.
    > > > > >
    > > > > > -Kumar
    > > > > >
    > > > > > >-----Original Message-----
    > > > > > >Hi,
    > > > > > >
    > > > > > >I would appreciate any input on this problem. (Visual
    > > > > > Basic.NET Windows
    > > > > > >Application)
    > > > > > >
    > > > > > >Facts:
    > > > > > >
    > > > > > >I have created a new windows project from Visual
    > > > > > Studio.net called 'Database2'.
    > > > >
    > > > > > >On the default created form called Form1 I have created a
    > > > > > textbox called TextBox1 (at design time)
    > > > >
    > > > > > >I have created from Solution Explorer a new Module called
    > > > > > Module1 and a new
    > > > > > >form called Form2. (at design time)
    > > > > > >
    > > > > > >Problem:
    > > > > > >
    > > > > > >How do I access Textbox1 residing on Form1 FROM either
    > > > > > Form2 or Module1. It
    > > > > > >must however, reference the ORIGINAL instance of the form
    > > > > > created at design
    > > > > > >time (not a new instance).
    > > > > > >
    > > > > > >I have tried the following (both give an error message)
    > > > > > code on Form2 :
    > > > > > >
    > > > > > >Database2.form1.Textbox1.text="test"
    > > > > > >Form1.Textbox1.text="test"
    > > > > > >
    > > > > > >I can refer to a new instance of the form1 with the
    > > > > > following code
    > > > > > >
    > > > > > >Dim myform as new Form1
    > > > >
    > > > > > >But then it would NOT be referring to the ORGINAL
    > > > > > instance of the form.
    > > > > > >
    > > > > > >Any ideas on how to access the Textbox1 on Form1 created
    > > > > > at design time without instantiating a new instance of Form1?
    > > > > > >
    > > > > > >Thank you.
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Quest, Oct 1, 2003
    #9
  10. Quest

    Quest Guest

    Yes it does work !!!
    Thank you.

    Lesson to be learnt here is that you can pass the instance of the Form1 as a
    parameter, in this case we have used MyForm(this) or in VB.NET it is
    MyForm(Me). Great.
    Once again thank you for the insight.

    "Jim Parker" <> wrote in message
    news:...
    > You wrote:
    > > Public Property myForm() As Form1
    > > Get
    > >
    > > myForm = Form1
    > >
    > > End Get

    >
    > No, that won't work.
    > Keep in mind that this blows in the face of good OO practices...
    > Here is a crude example in C#; in VB.Net it would be very similar.
    >
    > In Form1, the form that is already instantiated:
    >
    > private Form2 frmTest;
    > private void button1_Click(object sender, System.EventArgs e)
    > {
    > frmTest = new Form2();
    > frmTest.MyForm(this);
    > frmTest.Show();
    > }
    >
    > In Form2, which is instantiated from Form1:
    >
    > private Form1 frmMyForm;
    > public void MyForm(Form1 frmIn)
    > {
    > frmMyForm = frmIn;
    > }
    >
    > Then in the instance of Form2 you can access anything declared as public

    in
    > Form1 by accessing it as frmMyForm.
    >
    > JD.
    >
    >
    >
    > "Quest" <> wrote in message
    > news:kYBeb.1149$...
    > > Thank you.
    > > I think you have the right idea. With reference to your quote where you

    > say
    > > "Create a property of type Form1 on Form2, and set it to the already

    > created
    > > instance of Form1 ....", My question still stands as to how you access

    > that
    > > instance of Form1 in code ?
    > > Remember, that Form1 is a 'type' and not an instance. Therefore we are
    > > trying to access the instance of form1 that is automatically created by

    VB
    > > at runtime that is based on our design time creation of Form1.
    > >
    > > In form2, I have added the following code :
    > >
    > > Public Property myForm() As Form1
    > > Get
    > >
    > > myForm = Form1
    > >
    > > End Get
    > >
    > > Set(ByVal Value As Form1)
    > >
    > > End Set
    > >
    > > End Property
    > >
    > > This gives an error message that Form1 is a type ! How do I access the
    > > instance of Form1 from Form2 ?
    > >
    > >
    > >
    > >
    > > "Jim Parker" <> wrote in message
    > > news:...
    > > > The controls on forms are no longer public as they were in VB6.
    > > > Create public properties on Form1 to share the info that is desired to

    > > share
    > > > (textBox1.Text, whatever).
    > > > Create a property of type Form1 on Form2, and set it to the already

    > > created
    > > > instance of Form1 when an instance of Form2 is created.
    > > > Then you can easily access the public items on Form1 from Form2.
    > > >
    > > > JD.
    > > >
    > > > "Quest" <> wrote in message
    > > > news:Uszeb.831$...
    > > > > Ok. But the point is this : When you run the application, you are

    > > viewing
    > > > > the instance of the form that you created at design time. The

    question
    > > is,
    > > > > how do you access this SAME instance in code ??
    > > > > If I can access the same instance of the form in code, then I can

    > access
    > > > the
    > > > > texbox1 on form1 that the user first sees when the application is

    run.
    > > > >
    > > > > "Kumar" <> wrote in message
    > > > > news:131f01c3878f$c05f25c0$...
    > > > > > If you open the code for Form1, it may look like the
    > > > > > following:
    > > > > >
    > > > > > Public Class Form1
    > > > > > Inherits System.Windows.Forms.Form
    > > > > > ....
    > > > > > ...
    > > > > >
    > > > > > Which means that the form you see on the designer is
    > > > > > nothing by an instance of the Form1 class. Visual Studio
    > > > > > does a special job for you to show the form in design
    > > > > > time. If you want refer any form you need to create an
    > > > > > instance of it.
    > > > > >
    > > > > > I hope this helps.
    > > > > >
    > > > > > -Kumar
    > > > > >
    > > > > > >-----Original Message-----
    > > > > > >Hi,
    > > > > > >
    > > > > > >I would appreciate any input on this problem. (Visual
    > > > > > Basic.NET Windows
    > > > > > >Application)
    > > > > > >
    > > > > > >Facts:
    > > > > > >
    > > > > > >I have created a new windows project from Visual
    > > > > > Studio.net called 'Database2'.
    > > > >
    > > > > > >On the default created form called Form1 I have created a
    > > > > > textbox called TextBox1 (at design time)
    > > > >
    > > > > > >I have created from Solution Explorer a new Module called
    > > > > > Module1 and a new
    > > > > > >form called Form2. (at design time)
    > > > > > >
    > > > > > >Problem:
    > > > > > >
    > > > > > >How do I access Textbox1 residing on Form1 FROM either
    > > > > > Form2 or Module1. It
    > > > > > >must however, reference the ORIGINAL instance of the form
    > > > > > created at design
    > > > > > >time (not a new instance).
    > > > > > >
    > > > > > >I have tried the following (both give an error message)
    > > > > > code on Form2 :
    > > > > > >
    > > > > > >Database2.form1.Textbox1.text="test"
    > > > > > >Form1.Textbox1.text="test"
    > > > > > >
    > > > > > >I can refer to a new instance of the form1 with the
    > > > > > following code
    > > > > > >
    > > > > > >Dim myform as new Form1
    > > > >
    > > > > > >But then it would NOT be referring to the ORGINAL
    > > > > > instance of the form.
    > > > > > >
    > > > > > >Any ideas on how to access the Textbox1 on Form1 created
    > > > > > at design time without instantiating a new instance of Form1?
    > > > > > >
    > > > > > >Thank you.
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Quest, Oct 1, 2003
    #10
  11. Quest

    Quest Guest

    Yes it does work !!!
    Thank you.

    Lesson to be learnt here is that you can pass the instance of the Form1 as a
    parameter, in this case we have used MyForm(this) or in VB.NET it is
    MyForm(Me). Great.
    Once again thank you for the insight.

    "Jim Parker" <> wrote in message
    news:...
    > You wrote:
    > > Public Property myForm() As Form1
    > > Get
    > >
    > > myForm = Form1
    > >
    > > End Get

    >
    > No, that won't work.
    > Keep in mind that this blows in the face of good OO practices...
    > Here is a crude example in C#; in VB.Net it would be very similar.
    >
    > In Form1, the form that is already instantiated:
    >
    > private Form2 frmTest;
    > private void button1_Click(object sender, System.EventArgs e)
    > {
    > frmTest = new Form2();
    > frmTest.MyForm(this);
    > frmTest.Show();
    > }
    >
    > In Form2, which is instantiated from Form1:
    >
    > private Form1 frmMyForm;
    > public void MyForm(Form1 frmIn)
    > {
    > frmMyForm = frmIn;
    > }
    >
    > Then in the instance of Form2 you can access anything declared as public

    in
    > Form1 by accessing it as frmMyForm.
    >
    > JD.
    >
    >
    >
    > "Quest" <> wrote in message
    > news:kYBeb.1149$...
    > > Thank you.
    > > I think you have the right idea. With reference to your quote where you

    > say
    > > "Create a property of type Form1 on Form2, and set it to the already

    > created
    > > instance of Form1 ....", My question still stands as to how you access

    > that
    > > instance of Form1 in code ?
    > > Remember, that Form1 is a 'type' and not an instance. Therefore we are
    > > trying to access the instance of form1 that is automatically created by

    VB
    > > at runtime that is based on our design time creation of Form1.
    > >
    > > In form2, I have added the following code :
    > >
    > > Public Property myForm() As Form1
    > > Get
    > >
    > > myForm = Form1
    > >
    > > End Get
    > >
    > > Set(ByVal Value As Form1)
    > >
    > > End Set
    > >
    > > End Property
    > >
    > > This gives an error message that Form1 is a type ! How do I access the
    > > instance of Form1 from Form2 ?
    > >
    > >
    > >
    > >
    > > "Jim Parker" <> wrote in message
    > > news:...
    > > > The controls on forms are no longer public as they were in VB6.
    > > > Create public properties on Form1 to share the info that is desired to

    > > share
    > > > (textBox1.Text, whatever).
    > > > Create a property of type Form1 on Form2, and set it to the already

    > > created
    > > > instance of Form1 when an instance of Form2 is created.
    > > > Then you can easily access the public items on Form1 from Form2.
    > > >
    > > > JD.
    > > >
    > > > "Quest" <> wrote in message
    > > > news:Uszeb.831$...
    > > > > Ok. But the point is this : When you run the application, you are

    > > viewing
    > > > > the instance of the form that you created at design time. The

    question
    > > is,
    > > > > how do you access this SAME instance in code ??
    > > > > If I can access the same instance of the form in code, then I can

    > access
    > > > the
    > > > > texbox1 on form1 that the user first sees when the application is

    run.
    > > > >
    > > > > "Kumar" <> wrote in message
    > > > > news:131f01c3878f$c05f25c0$...
    > > > > > If you open the code for Form1, it may look like the
    > > > > > following:
    > > > > >
    > > > > > Public Class Form1
    > > > > > Inherits System.Windows.Forms.Form
    > > > > > ....
    > > > > > ...
    > > > > >
    > > > > > Which means that the form you see on the designer is
    > > > > > nothing by an instance of the Form1 class. Visual Studio
    > > > > > does a special job for you to show the form in design
    > > > > > time. If you want refer any form you need to create an
    > > > > > instance of it.
    > > > > >
    > > > > > I hope this helps.
    > > > > >
    > > > > > -Kumar
    > > > > >
    > > > > > >-----Original Message-----
    > > > > > >Hi,
    > > > > > >
    > > > > > >I would appreciate any input on this problem. (Visual
    > > > > > Basic.NET Windows
    > > > > > >Application)
    > > > > > >
    > > > > > >Facts:
    > > > > > >
    > > > > > >I have created a new windows project from Visual
    > > > > > Studio.net called 'Database2'.
    > > > >
    > > > > > >On the default created form called Form1 I have created a
    > > > > > textbox called TextBox1 (at design time)
    > > > >
    > > > > > >I have created from Solution Explorer a new Module called
    > > > > > Module1 and a new
    > > > > > >form called Form2. (at design time)
    > > > > > >
    > > > > > >Problem:
    > > > > > >
    > > > > > >How do I access Textbox1 residing on Form1 FROM either
    > > > > > Form2 or Module1. It
    > > > > > >must however, reference the ORIGINAL instance of the form
    > > > > > created at design
    > > > > > >time (not a new instance).
    > > > > > >
    > > > > > >I have tried the following (both give an error message)
    > > > > > code on Form2 :
    > > > > > >
    > > > > > >Database2.form1.Textbox1.text="test"
    > > > > > >Form1.Textbox1.text="test"
    > > > > > >
    > > > > > >I can refer to a new instance of the form1 with the
    > > > > > following code
    > > > > > >
    > > > > > >Dim myform as new Form1
    > > > >
    > > > > > >But then it would NOT be referring to the ORGINAL
    > > > > > instance of the form.
    > > > > > >
    > > > > > >Any ideas on how to access the Textbox1 on Form1 created
    > > > > > at design time without instantiating a new instance of Form1?
    > > > > > >
    > > > > > >Thank you.
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Quest, Oct 1, 2003
    #11
  12. Quest

    Jim Parker Guest

    You are welcome.
    "General Protection Fault" suggests another, perhaps more graceful,
    solution -- overload the constructor for Form2.

    That would be like this (C# example - Code in Form2 -- initialize from Form1
    with the overloaded constructor):

    ---------------------------------
    private Form1 frmMyForm;

    // This is the standard constructor
    public Form2()
    {
    InitializeComponent();
    }

    // This is the overload to be called from Form1
    public Form2(Form1 frmIn)
    {
    InitializeComponent();
    frmMyForm = frmIn;
    }
    ---------------------------------

    JD.


    "Quest" <> wrote in message
    news:G7Ieb.1725$...
    > Yes it does work !!!
    > Thank you.
    >
    > Lesson to be learnt here is that you can pass the instance of the Form1 as

    a
    > parameter, in this case we have used MyForm(this) or in VB.NET it is
    > MyForm(Me). Great.
    > Once again thank you for the insight.
    >
    > "Jim Parker" <> wrote in message
    > news:...
    > > You wrote:
    > > > Public Property myForm() As Form1
    > > > Get
    > > >
    > > > myForm = Form1
    > > >
    > > > End Get

    > >
    > > No, that won't work.
    > > Keep in mind that this blows in the face of good OO practices...
    > > Here is a crude example in C#; in VB.Net it would be very similar.
    > >
    > > In Form1, the form that is already instantiated:
    > >
    > > private Form2 frmTest;
    > > private void button1_Click(object sender, System.EventArgs e)
    > > {
    > > frmTest = new Form2();
    > > frmTest.MyForm(this);
    > > frmTest.Show();
    > > }
    > >
    > > In Form2, which is instantiated from Form1:
    > >
    > > private Form1 frmMyForm;
    > > public void MyForm(Form1 frmIn)
    > > {
    > > frmMyForm = frmIn;
    > > }
    > >
    > > Then in the instance of Form2 you can access anything declared as public

    > in
    > > Form1 by accessing it as frmMyForm.
    > >
    > > JD.
    > >
    > >
    > >
    > > "Quest" <> wrote in message
    > > news:kYBeb.1149$...
    > > > Thank you.
    > > > I think you have the right idea. With reference to your quote where

    you
    > > say
    > > > "Create a property of type Form1 on Form2, and set it to the already

    > > created
    > > > instance of Form1 ....", My question still stands as to how you access

    > > that
    > > > instance of Form1 in code ?
    > > > Remember, that Form1 is a 'type' and not an instance. Therefore we are
    > > > trying to access the instance of form1 that is automatically created

    by
    > VB
    > > > at runtime that is based on our design time creation of Form1.
    > > >
    > > > In form2, I have added the following code :
    > > >
    > > > Public Property myForm() As Form1
    > > > Get
    > > >
    > > > myForm = Form1
    > > >
    > > > End Get
    > > >
    > > > Set(ByVal Value As Form1)
    > > >
    > > > End Set
    > > >
    > > > End Property
    > > >
    > > > This gives an error message that Form1 is a type ! How do I access the
    > > > instance of Form1 from Form2 ?
    > > >
    > > >
    > > >
    > > >
    > > > "Jim Parker" <> wrote in message
    > > > news:...
    > > > > The controls on forms are no longer public as they were in VB6.
    > > > > Create public properties on Form1 to share the info that is desired

    to
    > > > share
    > > > > (textBox1.Text, whatever).
    > > > > Create a property of type Form1 on Form2, and set it to the already
    > > > created
    > > > > instance of Form1 when an instance of Form2 is created.
    > > > > Then you can easily access the public items on Form1 from Form2.
    > > > >
    > > > > JD.
    > > > >
    > > > > "Quest" <> wrote in message
    > > > > news:Uszeb.831$...
    > > > > > Ok. But the point is this : When you run the application, you are
    > > > viewing
    > > > > > the instance of the form that you created at design time. The

    > question
    > > > is,
    > > > > > how do you access this SAME instance in code ??
    > > > > > If I can access the same instance of the form in code, then I can

    > > access
    > > > > the
    > > > > > texbox1 on form1 that the user first sees when the application is

    > run.
    > > > > >
    > > > > > "Kumar" <> wrote in message
    > > > > > news:131f01c3878f$c05f25c0$...
    > > > > > > If you open the code for Form1, it may look like the
    > > > > > > following:
    > > > > > >
    > > > > > > Public Class Form1
    > > > > > > Inherits System.Windows.Forms.Form
    > > > > > > ....
    > > > > > > ...
    > > > > > >
    > > > > > > Which means that the form you see on the designer is
    > > > > > > nothing by an instance of the Form1 class. Visual Studio
    > > > > > > does a special job for you to show the form in design
    > > > > > > time. If you want refer any form you need to create an
    > > > > > > instance of it.
    > > > > > >
    > > > > > > I hope this helps.
    > > > > > >
    > > > > > > -Kumar
    > > > > > >
    > > > > > > >-----Original Message-----
    > > > > > > >Hi,
    > > > > > > >
    > > > > > > >I would appreciate any input on this problem. (Visual
    > > > > > > Basic.NET Windows
    > > > > > > >Application)
    > > > > > > >
    > > > > > > >Facts:
    > > > > > > >
    > > > > > > >I have created a new windows project from Visual
    > > > > > > Studio.net called 'Database2'.
    > > > > >
    > > > > > > >On the default created form called Form1 I have created a
    > > > > > > textbox called TextBox1 (at design time)
    > > > > >
    > > > > > > >I have created from Solution Explorer a new Module called
    > > > > > > Module1 and a new
    > > > > > > >form called Form2. (at design time)
    > > > > > > >
    > > > > > > >Problem:
    > > > > > > >
    > > > > > > >How do I access Textbox1 residing on Form1 FROM either
    > > > > > > Form2 or Module1. It
    > > > > > > >must however, reference the ORIGINAL instance of the form
    > > > > > > created at design
    > > > > > > >time (not a new instance).
    > > > > > > >
    > > > > > > >I have tried the following (both give an error message)
    > > > > > > code on Form2 :
    > > > > > > >
    > > > > > > >Database2.form1.Textbox1.text="test"
    > > > > > > >Form1.Textbox1.text="test"
    > > > > > > >
    > > > > > > >I can refer to a new instance of the form1 with the
    > > > > > > following code
    > > > > > > >
    > > > > > > >Dim myform as new Form1
    > > > > >
    > > > > > > >But then it would NOT be referring to the ORGINAL
    > > > > > > instance of the form.
    > > > > > > >
    > > > > > > >Any ideas on how to access the Textbox1 on Form1 created
    > > > > > > at design time without instantiating a new instance of Form1?
    > > > > > > >
    > > > > > > >Thank you.
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Jim Parker, Oct 2, 2003
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cowboy \(Gregory A. Beamer\)

    Take MCSD.NET with VB.NET and C#.NET in the same time?

    Cowboy \(Gregory A. Beamer\), Jan 1, 2004, in forum: MCSD
    Replies:
    1
    Views:
    437
    The Fox
    Jan 1, 2004
  2. Ranjandada

    Multiple forms on datagrid

    Ranjandada, Jul 28, 2006, in forum: Software
    Replies:
    0
    Views:
    774
    Ranjandada
    Jul 28, 2006
  3. mredelin
    Replies:
    0
    Views:
    1,167
    mredelin
    Dec 20, 2007
  4. tg
    Replies:
    9
    Views:
    434
  5. tg
    Replies:
    0
    Views:
    285
Loading...

Share This Page