Results 1 to 5 of 5

Thread: Win forms extention method to Open Or show the existing instance to front

  1. #1

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Win forms extention method to Open Or show the existing instance to front

    With win forms
    i am trying to have an extension method to open OR show the existing instance of the form , if its already open.
    but if the form is already open, it's not being presented at the front and activated,
    However if its not working new instance is being presented correctly
    what im doing wrong ?
    vb.net Code:
    1. public static void OpenOrShowFrom(this Form f)
    2.         {
    3.             Boolean IsOpen = false;
    4.  
    5.             foreach (Form frm in Application.OpenForms)
    6.             {
    7.                 IsOpen = frm.Name == f.Name;
    8.  
    9.                 if (IsOpen)
    10.                 {
    11.                     break;
    12.                 }
    13.             }
    14.  
    15.             // If for is already open just bring it to front & Activate
    16.             // TODO: If form is already open , it's not being bring to front & activated ?
    17.             if (IsOpen)
    18.             {              
    19.                
    20.                 f.Activate();
    21.                 f.WindowState = FormWindowState.Normal;
    22.                 f.BringToFront();            
    23.                                
    24.             }
    25.             else // Other wise open new instance of the form
    26.             {
    27.                 f.Show();
    28.             }
    29.            
    30.  
    31.         }
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

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

    Re: Win forms extention method to Open Or show the existing instance to front

    I'm not really sure that that code makes sense. What exactly is this section trying to do?
    Code:
                foreach (Form frm in Application.OpenForms)
                {
                    IsOpen = frm.Name == f.Name;
     
                    if (IsOpen)
                    {
                        break;
                    }
                }
    Is that checking whether the specific instance is already open or whether an instance of the same type is already open? If it's the latter then what's the point of that when later on you don't actually call Activate on that instance? If it's the former then most of that code is pointless. All you need is this:
    csharp Code:
    1. public static class FormExtensions
    2. {
    3.     public static void ShowOrActivate(this Form source)
    4.     {
    5.         source.WindowState = FormWindowState.Normal;
    6.         source.Show();
    7.         source.Activate();
    8.     }
    9. }
    Of course, like your existing code, this doesn't allow for the fact that the form may have been shown and closed and thus cannot be shown or activated. You might do this:
    csharp Code:
    1. public static class FormExtensions
    2. {
    3.     public static TForm ShowOrActivate<TForm>(this TForm source) where TForm : Form, new()
    4.     {
    5.         if (source == null || source.IsDisposed)
    6.         {
    7.             source = new TForm();
    8.         }
    9.  
    10.         source.WindowState = FormWindowState.Normal;
    11.         source.Show();
    12.         source.Activate();
    13.  
    14.         return source;
    15.     }
    16. }
    and then do something like this:
    csharp Code:
    1. private Form2 f2;
    2.  
    3. private void button1_Click(object sender, EventArgs e)
    4. {
    5.     f2 = f2.ShowOrActivate();
    6. }
    That will display a Form2 instance with focus in all the following situations:

    • No instance exists
    • An instance exists but has not been displayed
    • An instance exists and has been displayed and has focus
    • An instance exists and has been displayed but does not have focus
    • An instance exists and has been displayed but is minimised
    • An instance exists and has been displayed but has been closed

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Win forms extention method to Open Or show the existing instance to front

    If what you want is to create a Singleton that acts something like default instances in VB, I suggest that you take a look at this:

    https://www.vbforums.com/showthread....nce-of-a-Form)

    It incorporates much of the same logic as I demonstrated above.

  4. #4

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Win forms extention method to Open Or show the existing instance to front

    Sir thanks for the reply
    First time i am seeing the syntax like this,
    Code:
    public static TForm ShowOrActivate<TForm>(this TForm source) where TForm : Form, new()
    This is bit difficult for me to follow & use , please show some light on the syntax please.
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Win forms extention method to Open Or show the existing instance to front

    Quote Originally Posted by make me rain View Post
    First time i am seeing the syntax like this,
    Code:
    public static TForm ShowOrActivate<TForm>(this TForm source) where TForm : Form, new()
    This is bit difficult for me to follow & use , please show some light on the syntax please.
    Do I just wait for you to follow the link I provided and see that it's described there or do I actually point it out explicitly? Let's go with pointing it out.

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