473,397 Members | 1,969 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,397 software developers and data experts.

Hide and Show of Form in C#

33
Hi:
If I have 2 forms. Form1 have a timer that increase a counter and display in a textbox. If I press a button Form1 will be hide but still in process in the background, and Form2 will be displayed. Then If I press a button in Form2, Form 2 will be hide while Form1 will be display again ( with the latest counter value).

How can i do it?

Thanks
Oct 7 '08 #1
14 14239
r035198x
13,262 8TB
Open the docs for the Form class and browse through all the methods in it. The one you want should be easy to spot.

P.S Refuse to write any code if you don't have either the docs or intellisense.
Oct 7 '08 #2
Jetean
33
I tried in Form2 by creating a new instance.

Like this: Form1 newForm= new Form1();
newForm.show();


But that it the new instance, not the one that run on background..
Oct 7 '08 #3
r035198x
13,262 8TB
I tried in Form2 by creating a new instance.

Like this: Form1 newForm= new Form1();
newForm.show();


But that it the new instance, not the one that run on background..
Use the Hide and Show methods on those same Forms that you have. Using the new keyword will create a, well, new (different) form.
Oct 7 '08 #4
tezza98
38
Form1.visible = false
form2.visible = true
Oct 7 '08 #5
IanWright
179 100+
I tried in Form2 by creating a new instance.

Like this: Form1 newForm= new Form1();
newForm.show();


But that it the new instance, not the one that run on background..
Indeed that will be a new instance... So what you need is the first instance? So you need to a reference to that on Form2...

So how about?

Expand|Select|Wrap|Line Numbers
  1. // within Form2
  2.  
  3. Form1 form1;
  4.  
  5. Initialize(Form1 form1)
  6. {
  7.    this.form1 = form1;
  8. }
  9.  
  10. public override void Show()
  11. {
  12.    this.form1.Hide();
  13.    base.Show();
  14. }
  15.  
  16. public void ButtonClick(object sender, EventArgs e)
  17. {
  18.    this.form1.Hide();
  19. }
  20.  
Or even better you could have an event handler on Form1, which makes it hide/show itself depening on the result...

Expand|Select|Wrap|Line Numbers
  1. // on Form1
  2.  
  3. public ShowHideToggle(object sender, bool show)
  4. {
  5.   if(show)
  6.       this.Show();
  7.    else
  8.       this.Hide();
  9. }
  10.  
  11.  
  12. // on Form2
  13. public event EventHandler<bool> ShowHide;
  14.  
  15. Initialize(Form1 form1)
  16. {
  17.     this.ShowHide += form1.ShowHideToggle;
  18. }
  19.  
  20. public override void Show()
  21. {
  22.    if(ShowHide != null)
  23.       ShowHide(false);
  24.  
  25.    base.Show();
  26. }
  27.  
  28. public void ButtonPressed(object sender, EventArgs e)
  29. {
  30.    if(ShowHide != null)
  31.       ShowHide(true);
  32. }
  33.  
This then allows Form2 to not need to know about Form1 once it has an EventHandler set up...
Oct 7 '08 #6
tlhintoq
3,525 Expert 2GB
I tried in Form2 by creating a new instance.

Like this: Form1 newForm= new Form1();
newForm.show();


But that it the new instance, not the one that run on background..
The computer does only what you tell it - and EXACTLY what you tell it to do.

You told it to create a *new* Form1 so that's what it did. It created a *new* instance of a Form1 object, and thus it was not the hidden instance of the earlier Form1. Those two Form1's might look the same, but they are entirely different objects.

Do some searching about how to communicate between classes. Shift your thinking from making new items, to having methods inside each class that perform the functions you need. Maybe something like a method in Form1 that directs the form to hide itself, and one to show itself. Same with Form2. Then work out a way for Form2 to tell Form1 to perform the method you want.

A little less of 'A' directly affecting 'B'. A little more of 'A' telling 'B' in what manner to take care of itself.

If your Form2 class always knows how to take care of itself then you only have to write a method one time for "update", "show", "hide", "load", "resize", whatever. But if you try to make Form1 responsible for Form2, then you have to keep everything very synchronized. Form3 will have to duplicate all the control you put in Form1. If you make a change to Form2, you have to update Form1 and Form3 with identical changes.
Oct 7 '08 #7
Jetean
33
Indeed that will be a new instance... So what you need is the first instance? So you need to a reference to that on Form2...

So how about?

Expand|Select|Wrap|Line Numbers
  1. // within Form2
  2.  
  3. Form1 form1;
  4.  
  5. Initialize(Form1 form1)
  6. {
  7.    this.form1 = form1;
  8. }
  9.  
  10. public override void Show()
  11. {
  12.    this.form1.Hide();
  13.    base.Show();
  14. }
  15.  
  16. public void ButtonClick(object sender, EventArgs e)
  17. {
  18.    this.form1.Hide();
  19. }
  20.  
Or even better you could have an event handler on Form1, which makes it hide/show itself depening on the result...

Expand|Select|Wrap|Line Numbers
  1. // on Form1
  2.  
  3. public ShowHideToggle(object sender, bool show)
  4. {
  5.   if(show)
  6.       this.Show();
  7.    else
  8.       this.Hide();
  9. }
  10.  
  11.  
  12. // on Form2
  13. public event EventHandler<bool> ShowHide;
  14.  
  15. Initialize(Form1 form1)
  16. {
  17.     this.ShowHide += form1.ShowHideToggle;
  18. }
  19.  
  20. public override void Show()
  21. {
  22.    if(ShowHide != null)
  23.       ShowHide(false);
  24.  
  25.    base.Show();
  26. }
  27.  
  28. public void ButtonPressed(object sender, EventArgs e)
  29. {
  30.    if(ShowHide != null)
  31.       ShowHide(true);
  32. }
  33.  
This then allows Form2 to not need to know about Form1 once it has an EventHandler set up...
How to do this ? I got error using your code
Form1 form1;

Initialize(Form1 form1)
{
this.form1 = form1;
}
Oct 7 '08 #8
joedeene
583 512MB
why not just use the .hide() and .show() methods, once you have your referenced form ?

joedeene
Oct 7 '08 #9
r035198x
13,262 8TB
why not just use the .hide() and .show() methods, once you have your referenced form ?

joedeene
I'm afraid this is turning into one of those threads ...
Oct 8 '08 #10
IanWright
179 100+
How to do this ? I got error using your code
Form1 form1;

Initialize(Form1 form1)
{
this.form1 = form1;
}
Within Form1, when you create Form2 you need to be doing

Expand|Select|Wrap|Line Numbers
  1. Form2 f2 = new Form2();
  2. f2.Initialize(this);
  3.  
Oct 8 '08 #11
joedeene
583 512MB
I'm afraid this is turning into one of those threads ...
..one of those threads?

joedeene
Oct 8 '08 #12
Frinavale
9,735 Expert Mod 8TB
why not just use the .hide() and .show() methods, once you have your referenced form ?

joedeene
I was wondering the same thing myself......
Oct 8 '08 #13
r035198x
13,262 8TB
..one of those threads?

joedeene
... where the solution has already been given but the thread rumbles on because the OP doesn't bother reading(*) about the easy solution.

(*) Reading : An extinct art that was used to obtain information.
Oct 9 '08 #14
dennijr
17
i tried a bunch of things using given code and could not get it to work either...
Nov 15 '09 #15

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

Similar topics

3
by: Ryh | last post by:
I have the following scritpt. It hides div layer when mouse is out of the div layer. Inside DIV I have IFRAME box. Unfortuantely it does not work in Mozilla or IE 5.5. It hides div when cursor is...
2
by: MOHSEN KASHANI | last post by:
Hi, I am trying to hide some form elements in a form by default and show/hide depending on which radio button is clicked. This is what I have but it is not working: <head> <style> ..noshow {...
4
by: jerryyang_la1 | last post by:
I've found this script that allows be to hide/show form elements.. <script language="JavaScript"><!-- var toggle = true; function show(object) { if (document.layers && document.layers)...
5
by: Frances | last post by:
I'm doing a page w/lots of divs that I set to visibile or hidden dynamically using getElementById.. the divs are all forms... sometimes elements in a hidden form show thru on a visible form.. ...
4
by: John B | last post by:
I have a utility that minimizes to the system tray. When I double click or right-click and select restore I want to show the window and for it to be the active window again. I have tried...
3
by: kartikss | last post by:
HI!! If I have 2 forms ie form 1 , form 2 In Form 1 I have a command button to open form 2. how will i hide form 1 and show form 2, until i click on close button it open form 1 and form 2...
15
by: worked | last post by:
I have a script that hides / shows form elements, and wrapped in a tab script (tab navigation). When the code is duplicated (per tab content), the hide / show function works for the first tab but...
2
by: =?Utf-8?B?VG9kZCBKYXNwZXJz?= | last post by:
Hey guys, I've got a MAIN form, which I have a series of navigational buttons on. When I open a secondary form from that form, I'd like to be able to HIDE the original form. The reason is, if...
2
by: makunta | last post by:
Hi all, First here is the website of what I am trying to do: http://www.exmhosting.com/ Locate the login form on the top-right of the page. What I need to do is allow the user to sign into...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.