473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Hiding Forms

I have a form with 5 buttons, each time you click a button another form pops out. However, I want to hide any open forms (except the form with buttons) each time you click the button to make way for the new form. Example if I click button4 form6 pops out then if I click button2 I want form6 to hide and form4 to pop out and so on and so forth. Any help would be greating appreciated!!



This is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. namespace WindowsFormsApplication5
  2. {
  3.     public partial class SideMenu : Form
  4.     {
  5.         public SideMenu()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         private void button1_Click(object sender, EventArgs e)
  11.         {
  12.  
  13.             Form3 newMDIChild1 = new Form3();
  14.             newMDIChild1.MdiParent = this.MdiParent;
  15.             newMDIChild1.Show();
  16.  
  17.  
  18.  
  19.         }
  20.  
  21.         private void button2_Click(object sender, EventArgs e)
  22.         {
  23.             Form4 newMDIChild2 = new Form4();
  24.             newMDIChild2.MdiParent = this.MdiParent;
  25.             newMDIChild2.Show();
  26.  
  27.  
  28.         }
  29.  
  30.         private void button3_Click(object sender, EventArgs e)
  31.         {
  32.             Form5 newMDIChild3 = new Form5();
  33.             newMDIChild3.MdiParent = this.MdiParent;
  34.             newMDIChild3.Show();
  35.  
  36.         }
  37.  
  38.         private void button4_Click(object sender, EventArgs e)
  39.         {
  40.             Form6 newMDIChild4 = new Form6();
  41.             newMDIChild4.MdiParent = this.MdiParent;
  42.             newMDIChild4.Show();
  43.         }
  44.  
  45.         private void button5_Click(object sender, EventArgs e)
  46.         {
  47.             Form7 newMDIChild5 = new Form7();
  48.             newMDIChild5.MdiParent = this.MdiParent;
  49.             newMDIChild5.Show();
  50.         }   
  51.  
  52.  
  53.     }
  54. }
Nov 20 '11 #1

✓ answered by GaryTexmo

Ah, so the trouble is you want only one form to be shown at a time... you can open any form but you only want one (asides from your main form) visible at a time?

If I've understood you correctly (my apologies if I haven't!!) it shouldn't be too hard to do. If you're only ever intending to have a single form, why not just use a class member variable instead of creating object in every button click? All your forms inherit from the Form class, so you can make use of this. Your code would be something like...

Expand|Select|Wrap|Line Numbers
  1. public class ButtonForm : Form
  2. {
  3.   private Form m_subForm = null;
  4.  
  5.   ...
  6.  
  7.   private void button1_Click(object sender, EventArgs e)
  8.   {
  9.     SwitchForms(new Form3);
  10.   }
  11.  
  12.   ...
  13.  
  14.   private void SwitchForms(Form newForm)
  15.   {
  16.     if (m_subForm != null)
  17.     {
  18.       m_subForm.Hide();
  19.       m_subForm.Dispose();
  20.     }
  21.  
  22.     m_subForm = newForm;
  23.     m_subForm.MdiParent = this.MdiParent;
  24.     m_subForm.Show();
  25.   }
  26.  
  27.   ...
  28. }
Something like that. You'll note that this still destroys the old forms and creates new ones on every button click. If you want to preserve the state of each form and just show/hide on demand, you could look at keeping track of them in a Hashtable or a List structure. Then you can search those to see if the form already exists and show it while hiding all the others.

2 1985
GaryTexmo
1,501 Expert 1GB
Ah, so the trouble is you want only one form to be shown at a time... you can open any form but you only want one (asides from your main form) visible at a time?

If I've understood you correctly (my apologies if I haven't!!) it shouldn't be too hard to do. If you're only ever intending to have a single form, why not just use a class member variable instead of creating object in every button click? All your forms inherit from the Form class, so you can make use of this. Your code would be something like...

Expand|Select|Wrap|Line Numbers
  1. public class ButtonForm : Form
  2. {
  3.   private Form m_subForm = null;
  4.  
  5.   ...
  6.  
  7.   private void button1_Click(object sender, EventArgs e)
  8.   {
  9.     SwitchForms(new Form3);
  10.   }
  11.  
  12.   ...
  13.  
  14.   private void SwitchForms(Form newForm)
  15.   {
  16.     if (m_subForm != null)
  17.     {
  18.       m_subForm.Hide();
  19.       m_subForm.Dispose();
  20.     }
  21.  
  22.     m_subForm = newForm;
  23.     m_subForm.MdiParent = this.MdiParent;
  24.     m_subForm.Show();
  25.   }
  26.  
  27.   ...
  28. }
Something like that. You'll note that this still destroys the old forms and creates new ones on every button click. If you want to preserve the state of each form and just show/hide on demand, you could look at keeping track of them in a Hashtable or a List structure. Then you can search those to see if the form already exists and show it while hiding all the others.
Nov 21 '11 #2
Thank you so much for your help!!!!
Nov 27 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Guest | last post by:
Hi, I am writing a Jeopardy-like game that contains three forms. One is the Jeopardy board, another is the Question/Answer displayer, and the last is the scoreboard. I need to find a way to...
1
by: Eric W | last post by:
CreateProductForm f = new CreateProductForm(); f.Parent = this; f.Show(); I'm trying to show a child form on the smartphone. It works fine when i just call f.Show() but as soon as I...
17
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that...
5
by: Ivan | last post by:
I am used to VB6 and am not sure how to do this in Vstudio .NET. I have a main form which calls other forms. I want to disable that main form while other ones are called. I tried hiding it and...
11
by: Kevin | last post by:
I've got a timer on my MDI parent form. If there's no mouse movement for a set number of minutes, the Visible property of all open forms is set to False and the Log On form is displayed. I could do...
4
by: JustAFriend | last post by:
Hi. I'm tring to hide a form while using threads. For some reason the form is NULL whan I enter the thread, although it's defined as a class member. What am I doing wrong? ...
0
by: JustAFriend | last post by:
Hi. I'm tring to hide a form while using threads. For some reason the form is NULL whan I enter the thread, although it's defined as a class member. What am I doing wrong? ...
12
by: kronecker | last post by:
I found this nifty routine that closes a form one by one every time it is called. However, I need to hide them instead of closing them. Is there a way to alter the code? I assume it has something...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
3
by: Anthony P. | last post by:
Hello Everyone, I'm writing an application that, so far, only has three forms: frmSplashScreen frmLicenseScreen frmConfigurationScreen Now, frmSplashScreen has a timer that sits it on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.