Results 1 to 10 of 10

Thread: Changing Forms, the .NET Way?

  1. #1

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718

    Changing Forms, the .NET Way?

    In VB6 I used to have a Main menu and all my forms came off that, to change the forms I used

    VB Code:
    1. Unload Me
    2. Form1.Show

    Now, how do I do this in .NET?
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

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


    You first create an instance of the form to be opened then you make it visible, for example

    Dim frmCurrent as New frmOne
    frmCurrent.Show

    If you use the samename for the current form, assuming you have only one subsidiary form open at a time, it makes the coding for showing the various subsidiary forms simpler.

    When you have finished with it you would normally put the following in the click event of an Exit button on that form:

    Me.Close()

    or (less likely) put the following in the click event of a button on the main form:

    frmCurrent.Close



    Although the words look very similar to VB6, they are accomplishing a very different chain of events. Have a look at articles on Inheritance in the MSDN help files and search the subject in this forum.
    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.

  3. #3

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    OK, i tried that, when i execute the code on the main form, it closes the program. Is this common in all .NET or a VB.NET 2002 issue?
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  4. #4
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    You can not close your main startup form.
    If you do, you close the whole app.

    If you need to, you can hide the main form, while showing the second form.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

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

    Totally different problem. As I said, you are going to have to forget your VB6 knowledge when it comes to forms (and all other objects). You MUST become familiar with Inheritance, otherwise you will continue to ask very basic questions on this forum.

    You are probably trying to open the form in the VB6 manner than creating an instance in the VB.NET manner. Although you CAN do this the VB6 way, you will run into all sorts of problems, including the one you mention.

    Always use a module to start your programme.

    When you design a form named, for example, frmMain, you actually design a class (or more correctly a Sub Class) of that name, which is why you are (or should be) taught to name it fclsMain. The form does not actually exist in your running programme. What you have to do is to create an Instance of fclsMain by a declaration. So, in the General Section of the Module write;

    Dim frmFirst as New fclsMain or

    Public frmFirst as New fclsMain


    Then, in the same Module create your Sub Main:

    Private Sub Main()
    Application.Run(frmFirst)
    End Sub


    frmFirst will be an EXACT copy of fclsMain.

    Make Sub Main the startup object of your project and away you go.

    PS. I have just re-read your last post and there may be a slight misunderstanding. In my first reply, I understood you to say that you wanted to have a main form and then run all your other forms from it (your terminology). Therefore in the above example, you would, for example, have the following code in a button click event on fclsMain assuming you have designed a form named fclsStatement:

    Dim frmCurrent as New fclsStatement
    frmCurrent.show()


    The closing of that form can be achieved either by putting the following in the click event of a button in fclsStatement:

    Me.Close()

    or putting the following in the required event in fclsMain:

    frmCurrent.Close()

    If at any stage you close frmFirst without closing frmCurrent, you will terminate the project ( you can take steps to avoid that if required).
    Best of luck.
    Last edited by taxes; Feb 7th, 2004 at 04:26 AM.
    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.

  6. #6

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    Originally posted by pax
    Hi.

    You can not close your main startup form.
    If you do, you close the whole app.

    If you need to, you can hide the main form, while showing the second form.
    I tried that, but when i said Main.show etc it loaded a new instance of it for some unknown reason.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  7. #7

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    Taxes, thanks for taking the time to reply, i will certinly look into what you said. Looks like you got the idea.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  8. #8
    New Member
    Join Date
    Dec 2003
    Posts
    11
    You can also simply hide the first form with "me.hide". You have to remember it's just hidden, though, if you plan to return data to it later (i.e. don't launch another instance of the form).

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


    "If you need to, you can hide the main form, while showing the second form.
    --------------------------------------------------------------------------------
    I tried that, but when i said Main.show etc it loaded a new instance of it for some unknown reason."


    That should not happen. How do you know it is another instance? I have just checked it out and it works O.K. When you used MainForm.Show, did you use any other code as well? Did you declare it again?

    Regards,
    Last edited by taxes; Feb 7th, 2004 at 03:46 PM.
    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.

  10. #10

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    I know it loaded another instance because i put a textbox on the form, and hid it, then opened another form, closed it and showed the main one again and the text in the textbox was back to the default. I'll look at the code again though to see wha went wrong.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

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