Results 1 to 6 of 6

Thread: [RESOLVED] MDI question

  1. #1

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Resolved [RESOLVED] MDI question

    How can i check if one of the child forms is already open as i dont want to create multiple instances of the same form

    cheers
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: MDI question

    You can use something called Singletons. Only one instance of the class is allowed to exist.

    http://www.codeproject.com/vb/net/Si...eton_Forms.asp

  3. #3
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: MDI question

    since the thread title is called MDI, then i will assume that you are talking about mdi child forms.


    check my code for this issue , i use it in my mdi application

    VB Code:
    1. ' this code should be written in the mdi parent form
    2. dim frmx as new form1    'form1 is the name of the wanted form
    3.   Dim ctl As Form
    4.         For Each ctl In me.MdiChildren
    5.             If ctl.Name = FrmX.Name Then
    6.                 ctl.BringToFront() : ctl.Show()
    7.                 FrmX.Dispose()
    8.                 Exit Sub
    9.             End If
    10.         Next
    11.         'Setting Parent
    12.         FrmX.MdiParent = me

    i hope this will help
    rgds

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

    Re: MDI question

    Assuming that the type of the MDI child is Form2, I would use something similar to maged but not quite the same:
    VB Code:
    1. Dim myChild As Form2
    2.  
    3. For Each frm As Form In Me.MdiChildren
    4.     If TypeOf frm Is Form2
    5.         myChild = frm
    6.         Exit For
    7.     End If
    8. Next frm
    9.  
    10. If myChild Is Nothing Then
    11.     'Create a new child form.
    12.     myChild = New Form2
    13.     myChild.MdiParent = Me
    14.     myChild.Show()
    15. Else
    16.     'Activate the existing child form.
    17.     myChild.Activate()
    18. End If
    Edit:
    Having said that, the Singleton approach is definitely the more professional. It is actually not a bad idea to create a SingletonForm class that inherits the Form class and then derive all the forms you want to have this behaviour from that, instead of from Form as you usually would.
    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

  5. #5
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: MDI question

    the advantage of your code is that u dont create an new instance of the form if it is not needed. for sure that is more effecient than creating a new form and then disposing it.

    better code i must admit

  6. #6

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: MDI question

    cool, cheers guys, first time doing a mdi app so will prob have more questions later
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

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