Results 1 to 10 of 10

Thread: Multiple Forms

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2004
    Location
    UK
    Posts
    43

    Multiple Forms

    iv'e got two forms, the first is Mainform and second_form

    wha i am trying to do, is to access a button on the mainform from the second form,,,,,,,

    while running the second form, i managed to do

    Dim mf as new mainform

    mf.button1.text = " new text updated"

    but i found out that i have now two mainform opened,,,,,

    and i couldnt' manage to change the data from mainform1

    so what u advise me to do in this situation,



    thank for helping

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Multiple Forms

    Originally posted by xyz777
    iv'e got two forms, the first is Mainform and second_form

    wha i am trying to do, is to access a button on the mainform from the second form,,,,,,,

    while running the second form, i managed to do

    Dim mf as new mainform

    mf.button1.text = " new text updated"

    but i found out that i have now two mainform opened,,,,,

    and i couldnt' manage to change the data from mainform1

    so what u advise me to do in this situation,



    thank for helping
    you have to understand how classes work. A form is a class. You need to have a reference to the form to be able to access it.
    If you want to be able to access a form from another form, you can pass a reference to the form in the new constructor (have to change the constructor yourself), or you could just declare the forms publicly in a module. Something like this:

    VB Code:
    1. Module Module1
    2.     ' Change Form1 to whatever you want
    3.     Public frm1, frm2 As Form1
    4.  
    5.     Sub Main()
    6.         frm1 = New Form1        
    7.         frm2 = New Form1
    8.  
    9.         ' runs your app with frm1
    10.         Application.Run(frm1)
    11.     End Sub
    12. End Module

    you can access frm1 and frm2 anywhere in your app if you put that code in a module

    edit: umm make sure you change the startup object to the module. If you dont know how: right click on your project in the Solution Explorer, click on Properties. In the left hand make sure Common Properties/General is selected. in the left section choose Sub Main from the "Startup object" dropdown
    Last edited by MrPolite; Jan 30th, 2004 at 11:53 AM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi xyz777,

    I read your question differently from Mr Polite. As I read it you are wondering why you have two different MainForms opened.

    I suspect that you have used MainForm as the application start form, or at least have created an instance of it in a Sub Main procedure. Therefore, when you placed the code
    "Dim mf as new mainform"
    in the second form, you created a second and entirely separate instance of MainForm, causing confusion (to you not VB.NET).

    Remove the above code and alter the call to

    MainForm.button1.text = " new text updated" (assuming that MainForm is the name you hav declared)

    This works provided that MainForm is still open.

    For More involved aspects see:


    http://www.vbforums.com/showthread.p...hreadid=276165

    and for an unusual alternative:

    http://www.vbforums.com/showthread.p...hreadid=276604
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  4. #4
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    Sorry to drag this up out of the tar pits of time, but I'm haing a very similar problem that the suggested solution does not work for.

    My particular line of code is:
    VB Code:
    1. Form1.TabControl1.SelectedIndex = Me.ListBox1.SelectedIndex
    That is called from Form2 (me = Form2). I get the build error:
    Reference to a non-shared member requires an object reference
    And "Form1.TabControl1" is underlined in my code.

    Form1 is where the application starts from and it calls Form2. Both instances are open and shown. Form2 is trying to set a value on Form1. What am I doing wrong?

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Form2 doesn't know anything about Form1. It's sort of like trying to use integer variable x in sub A when it's declared in sub B.
    The easiest solution would be to declare the forms in the module, as above.

  6. #6
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    I tried that:
    VB Code:
    1. Module Module1
    2.     Public PaddbFlag, lbflag As Integer
    3.     Public Frm1 As Form1
    4.     Public Frm2 As New Form2
    5.  
    6.     Public Sub Main()
    7.         Frm1 = New Form1
    8.         Application.Run(Frm1)
    9.     End Sub
    10. End Module
    I get this error when I try to execute the code on form2:
    An unhandled exception of type 'System.NullReferenceException' occurred in Test006.exe

    Additional information: Object reference not set to an instance of an object.
    How can I be sure that the program is starting in Sub Main and not in Form 1?

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    When you reference Form1 from Form2, don't call it Form1, call it Frm1, in your example. That's the name you gave it. Otherwise it's like saying
    VB Code:
    1. dim i as integer
    2. For integer = 0 to yadayada...

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Just to clarify what salvelinus siad;

    When you create an instance of a form, that form exists ONLY under the name you gave it in the instancing.

    e.g.
    Where fcls1 and fcls2 are the forms, or more correctly the Form SubClasses, you created in the designer

    In Module
    Public frm1 as new fcls1
    Public frm2 as new fcls2


    In a sub or method in frm1 you refer to any object in frm2 as, e.g.
    frm2.TextBox1

    fcls1 and fcls2, your originally designed forms (subclasses) do not exist in your runtime application - one of the big differences from VB6.


    Hope this helps.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9
    Junior Member
    Join Date
    Feb 2004
    Location
    Smithfield, Utah
    Posts
    19
    Thanks everyone for replying.


    BUT I found someone who explained it very understandably.


    http://www.devcity.net/net/article.a...=multipleforms

    This link has a zip file with full sample code.

    Thanks,

  10. #10
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    Well, thanks to Dooglo's article, I was able to get it to work by overloading Form2's constructor method and by passing Form1 as an arguement, i.e. Dim frm2 As New Form2(Me). By passing the arguement to Form2's constructor, I was able to assign it to a variable. This gives Form2 a direct link to Form1. Worked like a charm.

    But I'm still a bit hazy on this instanciation stuff. Here's where you guru's get to educate a relative noob.

    Is there a way to assign a varible to an already existing instance of an object? Maybe I'm not using the right words. In the statement, "Dim frm2 As New Form2," not only are you creating a new instance of Form2, but you are also assigning a variable, frm2, to it, right? Or do I have this wrong?

    Is "Dim frm2 As Form2" - notice the abscense of "New"- a valid statement? I guess that it would be. It creates the varible, frm2, but does not assign it to any particular instance of a Form2. Is there a way of assigning frm2 to an already existing instance of Form2?

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