Results 1 to 6 of 6

Thread: [RESOLVED] Focus on opened childform

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    30

    Resolved [RESOLVED] Focus on opened childform

    Hi all,

    I have created one mdi parent form and several mdi child forms. My question is when i click on open a mdi child form
    this happening
    Code:
    if (frmKlantenbeheer.alreadyopen== false) //to check if form already open
                {
                    frmKlantenbeheer form1 = new frmKlantenbeheer();
                    form1.MdiParent = this;
                    form1.Show();
                }
                else //if already open then focus that form
                {
                    frmKlantenbeheer.ActiveForm.Activate();
                    frmKlantenbeheer.ActiveForm.BringToFront();
                }
    if a mdi child already opened i want to focus on that back
    so this part doesn't work
    Code:
             else //if already open then focus that form
                {
                    frmKlantenbeheer.ActiveForm.Activate();
                    frmKlantenbeheer.ActiveForm.BringToFront();
                }[/
    Could anyone help me for this

    already thanks
    bye

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

    Re: Focus on opened childform

    BringToFront is not relevant at all. That is what you call on controls while Activate is what you call on forms.

    Also, there's not much point calling Activate on the ActiveForm because it's already active. If you want to activate frmKlantenbeheer then you call the Activate method of frmKlantenbeheer:
    c# Code:
    1. frmKlantenbeheer.Activate();
    That's all you need. I'd still go with the Singleton pattern that I posted in that other thread you posted to. If a form is only supposed to have one instance then that behaviour should be part of the form itself.
    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    30

    Resolved Re: Focus on opened childform

    Singleton pattern
    this is too complex for me but i solved this
    actually is not complex just using a static function but in this case you have to write in one form other case you have to write code in both forms (child and mdi) heheh i hope it was clear :d

    here is the code
    Code:
    // frmLeveranciersbeheer is the one I'm looking for 
                frmLeveranciersbeheer childForm = null;
    
                foreach (Form f in this.MdiChildren)
                {
                    if (f is frmLeveranciersbeheer)
                    {
                        // found it 
                        childForm = (frmLeveranciersbeheer)f;
                        break;
                    }
                }
    
                if (childForm != null)
                {
                    childForm.Show();
                    childForm.Focus();
                }
                else
                {
                    childForm = new frmLeveranciersbeheer();
                    childForm.MdiParent = this;
                    childForm.Show();
                    childForm.Focus();
                }

    Thanks for reply

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

    Re: [RESOLVED] Focus on opened childform

    Don't call the Focus method. Call Activate.
    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

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    30

    Re: [RESOLVED] Focus on opened childform

    Quote Originally Posted by jmcilhinney
    Don't call the Focus method. Call Activate.
    Can I ask why?


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

    Re: [RESOLVED] Focus on opened childform

    Always read the documentation, not that you're the first person not to. From the MSDN help topic for the Focus method:
    Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.
    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

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