Click here to Skip to main content
15,892,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have small doubt in my windows application. In my application I kept one form as menu form and put buttons to navigate to all other forms. On debugging when I click one button it will display corresponding form(eg: sales) and I minimized that form. Again when I click that particular button it shows a new form(new sales form) instead of that minimized one. I want the minimized form to be active when I click the button. How can I solve this problem? Can anybody help me?
Posted
Updated 15-Dec-10 18:55pm
v2

Hi,
When you click on that button check instance of that form is already open or not. if no then open new form but if yes as per your case then set focus to that same one.
eg take object of that form. Take for loop of mdichild forms and check it out that form with same name is open if yes then set focus to that.

Hope this help!!!! :)
 
Share this answer
 
C#
Form2 obj = new Form2();//declare as Global your navigation form.

       private void button1_Click(object sender, EventArgs e)
       {
          
           if ( obj.IsDisposed)
               obj = new Form2();           
            if (obj.WindowState == FormWindowState.Minimized)
               obj.WindowState = FormWindowState.Normal;
               else
               obj.Show();     
       }
//if this helped please vote.

<pre>
 
Share this answer
 
Comments
sevenbell 16-Dec-10 2:40am    
Thanks a lot its working as fine.
Dalek Dave 16-Dec-10 3:40am    
Good Answer.
justinonday 16-Dec-10 5:12am    
Thank you SevenBell and Dalek Dave
Hi ,

Source of problem is every time you click on button there is new instance of a form is created which is different from other.

Now there is two way to solve this problem.

Declare then base or main form as a mdi form and check your code is

VB
If Me.MdiChildren.Length > 0 Then
                For Each frmChild As Form In Me.MdiChildren
                    'Do you task here 
                Next
                
        Else
            'Code to Open form 
End If


Another way to maintain a static Dictionary of object of form and form name as key. On button click check is object is already exists the activate it or create a new instance of form.
 
Share this answer
 
Comments
Toli Cuturicu 16-Dec-10 10:24am    
Question tags: C# 3.0
Your code: some strange programming language resembling bad pseudocode.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900