Results 1 to 7 of 7

Thread: [SOLVED] showing form... (back in time, it was so easy!)

  1. #1

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    Resolved [SOLVED] showing form... (back in time, it was so easy!)

    I remember back in time, when there was no .NET, naviguating through form was so easy... Now, with that .NET framework... it's big deal... I've been stuck for 3days on how to call a page... I search the msdn, this forums and google, and I just dont find something simply explaind.. I just want to call form2 from the menu on my form1. I dont want to create a new form, I want to show a form I already desinged... I know there is some declaration, but what is that initializing thing??? Anyone can provide some simple sample code... please, thank you all!!! realkly appreciated

    Jean Christiophe Avard
    Last edited by jcavard; Jul 14th, 2005 at 12:21 PM.

  2. #2
    Lively Member
    Join Date
    Jun 2004
    Posts
    99

    Re: showing form... (back in time, it was so easy!)

    Inside your click event for the menu item -

    Dim NewForm as New Form2
    NewForm.Show()

    (designing a form doesn't create an instance of it, it just outlines what it should be when created. the only reason the first form is created by default is because it is set as the startup object for the project you are working on)

  3. #3
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: showing form... (back in time, it was so easy!)

    VB Code:
    1. Private Sub MenuItem9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem9.Click
    2.  
    3.         ' Detailed way
    4.         Dim frm As Form2
    5.         frm = New Form2
    6.         frm.ShowDialog()
    7.  
    8.         'short way
    9.         Dim frm2 As Form2 = New Form2
    10.         frm.ShowDialog()
    11.  
    12.     End Sub

    You may also look to this thread

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: showing form... (back in time, it was so easy!)

    If your Form2 is already initialized in your project and you want to access it from Form1 then it depends on how you are starting
    your project and what the startup object is.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: showing form... (back in time, it was so easy!)

    This way you can access any form object throughout your project without having to instanciate a new one, but then
    more resources since all forms are instanciated all the time, etc.

    Set your startup object to Sub Main, add a Module to your project.


    VB Code:
    1. Option Explicit On
    2.  
    3. Module Module1
    4.  
    5.     Public goForm1 As Form1
    6.     Public goForm2 As Form2
    7.  
    8.     Public Sub Main()
    9.        
    10.         goForm1 = New Form1
    11.         goForm2 = New Form2
    12.         Application.Run(goForm1)
    13.  
    14.     End Sub
    15.  
    16. End module
    17.  
    18.  
    19.  
    20. 'Behind form1
    21. Option Explicit On
    22.  
    23. Public Class Form1
    24.  
    25. 'blah, blah...
    26.  
    27.     Private Sub MenuItem9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem9.Click
    28.         goForm2.ShowDialog()
    29.     End Sub
    30.  
    31. End Class
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: showing form... (back in time, it was so easy!)

    Hi,

    I'm sure this has been said on this forum many times before, and will continue to be said again. I don't know if I can put it any simpler than that which has already been said, but I will try.

    Dim newform2 as New Form2

    YOU ONLY DO THIS ONCE, EITHER WHEN YOUR PROGRAM LOADS OR THE FIRST TIME YOU NEED THE FORM

    newform2.show

    YOU DO THIS EVERY TIME YOU WANT TO SHOW FORM 2.


    I know you design the forms using the objects from the toolbar at design time. But when it comes to runtime, this form is a bit like a generic CommandButton or TextBox, or any other object. You don't just refer to "Commandbutton.text", you have to choose a Particular Commandbutton, e.g. "Commandbutton1.Text". Similarly, you don't actually have a Form2 until you load one up based on the template which you designed at design time, and you don't just have "Form2", you have a particular Instance of Form2, called "newform2".

    Think of the forms at design time solely as templates rather than as objects, and it will become a bit easier to understand.

    So, in your menu, you don't want to be creating a new version of Form2 from the template each time. Hence all you want is "newform2.show". However, this "newform2" has to be created at some point, using the line:

    Dim newform2 As New Form2

    You could do this in the Form1 class (right at the top of the code for Form1), or in a module, or in fact almost anywhere else.
    "newform2" then behaves like anything else created at runtime - it is only valid as a reference within the area of code at which you created it. So if you made a button and put the "Dim newform2..." in there, your program would only recognise this name until the end of the "Command1_Click" event.
    If you want to get it out of a particular section of code, you have to pass it out or declare it at a level at which other bits of your code can see it, such as in a module.

    Does this make any sense? Quote bits that don't and I'm sure somebody will try to help.



    There are so many posts coming up on this subject so regularly that it would be good to get one that everyone can understand and have it Stuck to the top for a while....


    HTH

    zaza

  7. #7

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    Re: showing form... (back in time, it was so easy!)

    thanks zaza, thank you everuyone soooo much!! that really helped me!

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