Results 1 to 13 of 13

Thread: loading forms

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    79

    loading forms

    hi guys. i have a quick question. I have a form with a command button that when i click i want to load a new form. it was easy in vb6 but i can't figure it out in vb.net

    I'd love some help with this one guys.

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: loading forms

    Just create it and then show it,

    Code:
            Dim frm As New Form2
            frm.show

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: loading forms

    wes4dbt is correct, but you should be aware that the Form class implements IDisposable and should be disposed after you access it. Here is documentation on the subject: https://learn.microsoft.com/en-us/do...em-idisposable

    If you wanted to display it as a modal then wrap the form creation in a using statement (documentation) and call the ShowDialog method (documentation):
    Code:
    Using frm = New Form2()
        frm.ShowDialog()
    End Using
    If you wanted to display it without being a modal, then you can dispose of it manually after the form closes by calling the Dispose method (documentation). You can do this in the Form's FormClosed event (documentation):
    Code:
    Dim frm = New Form2()
    
    AddHandler frm.FormClosed, Sub(sender, e)
                                   DirectCast(sender, Form).Dispose()
                               End Sub
    
    frm.Show()
    Last edited by dday9; Mar 28th, 2024 at 03:36 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    79

    Re: loading forms

    Using frm = New Form2()
    frm.ShowDialog()
    End Using

    just keeps bringing up the original form.

    Let me explain, i have a form with a dropdown.box with form2 as an item
    i need to Load form2 when a certain item is selected.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    79

    Re: loading forms

    i figured it out. i was loading the wrong form. thank you so much for your help!

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: loading forms

    I don't understand. If Form1 holds the dropdown, when the item is selected it should create a new instance of Form2 and show it.
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim selectedItem = ComboBox1.Text
            If (selectedItem.Equals("Form2", StringComparison.OrdinalIgnoreCase)) Then
                Using frm = New Form2()
                    frm.ShowDialog()
                End Using
            End If
        End Sub
    
    End Class
    Attachment 190942
    Attachment 190943
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: loading forms

    Then that code would be located in the SelectedIndexChanged event for the dropdown. I'm not sure what you meant by it just bringing up the original form. The code shown will create an instance of Form2 and display it. How is that the original form? I expect that there is a miscommunication here.
    My usual boring signature: Nothing

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: loading forms

    Cross posted, glad you got it resolved.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: loading forms

    Quote Originally Posted by dday9 View Post
    If you wanted to display it without being a modal, then you can dispose of it manually after the form closes by calling the Dispose method (documentation). You can do this in the Form's FormClosed event (documentation):
    Code:
    Dim frm = New Form2()
    
    AddHandler frm.FormClosed, Sub(sender, e)
                                   DirectCast(sender, Form).Dispose()
                               End Sub
    
    frm.Show()
    That is unnecessary. When a form is displayed by calling ShowDialog, closing it does not dispose it, so you actually do have to dispose it explicitly or via a using block. When you display a form by calling Show though, closing it actually does dispose it. It may well have been done that way specifically because having to write code to dispose forms that might even outlive their caller would be cumbersome. Less so now with the advent of lambda expressions but back in the .NET Framework 1.0 days, definitely so.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: loading forms

    Quote Originally Posted by jmcilhinney View Post
    ... When you display a form by calling Show though, closing it actually does dispose it ...
    I was not aware of that. It doesn't look like that is documented anywhere in the Form's Show method (here), do you know where I can find some more information on that?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: loading forms

    It's actually (kind of) noted on ShowDialog
    Quote Originally Posted by MSDN
    Because a form displayed as a dialog box is hidden instead of closed, you must call the Dispose method of the form when the form is no longer needed by your application.
    Looks like there's more info about it in the Close method.
    When a form is closed, all resources created within the object are closed and the form is disposed
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: loading forms

    Quote Originally Posted by techgnome View Post
    It's actually (kind of) noted on ShowDialog


    Looks like there's more info about it in the Close method.


    -tg
    I found this in the Reference Source. Didn't seem to matter if it was Show or ShowDialog.

    // MSDN: When a form is closed, all resources created within the object are closed and the form is disposed.
    // For MDI child: MdiChildren collection gets updated (VSWhidbey# 368642 & 93550)
    Dispose();
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: loading forms

    I don't want to derail this member's thread, so I'm going to open a new one.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Tags for this Thread

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