Show hidden form1 with code in form2
Page 1 of 111 Replies - 1112 Views - Last Post: 12 May 2012 - 08:57 AM
#1
Show hidden form1 with code in form2
Posted 11 May 2012 - 06:23 AM
I have two forms. Lets call them form1, form2.
I hide (visible = false, showInTaskBar = false --> and NOT FORM.HIDE()) form1, and show form2.
within the code of form2, if the user clicks a button, I want to show the hidden form1.
I tried doing it with application.openforms technique, but with out any success.
That because form1 is not open for the app (it is "hidden").
How can I open form1 without creating a new intstance?
Replies To: Show hidden form1 with code in form2
#2
Re: Show hidden form1 with code in form2
Posted 11 May 2012 - 06:37 AM
There's another way using events; are you familiar with writing custom events?
#3
Re: Show hidden form1 with code in form2
Posted 11 May 2012 - 06:40 AM
#4
Re: Show hidden form1 with code in form2
Posted 11 May 2012 - 07:04 AM
public Form1 childform = new Fomr1(); //Then just use childform.ShowDialog(this); //without 'this' for WPF Window
ShowDialog() shows form, makes calling form unclickable and causes shown form to just hide on closing.
This post has been edited by Nerfpl: 11 May 2012 - 07:06 AM
#5
Re: Show hidden form1 with code in form2
Posted 11 May 2012 - 08:00 AM
What you should do is raise an event in the child (form2) that Form1 can then react to.
Let's say the user clicks a button on Form2.
Form2
private void btnDone_Click(object sender, eventargs e)
{
this.hide();
RaiseDoneEvent();
}
Form2 does not try to control Form1. When Form1 hears the 'Done' event it can react by unhiding itself. Form1 controls Form1. Form2 controls Form2.
Form1
private void FormTwoDone(object sender, eventargs e)
{
this.Show();
}
See FAQ # 3. (Click the SHOW button below)
TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2
FAQ (Frequently Asked Questions - Updated Apr 2012
Nerfpl, on 11 May 2012 - 08:04 AM, said:
public Form1 childform = new Fomr1(); //Then just use childform.ShowDialog(this); //without 'this' for WPF Window
ShowDialog() shows form, makes calling form unclickable and causes shown form to just hide on closing.
The problem with this is that form2 will have to be CLOSED rather than HIDDEN. If that second form needs to be reused then a new instance will need to be made, values populated and so on. Sometimes its just easier to make one instance and show/hide it throughout the lifetime of the application.
#6
Re: Show hidden form1 with code in form2
Posted 11 May 2012 - 11:56 AM
Curtis Rutland, on 11 May 2012 - 07:37 AM, said:
There's another way using events; are you familiar with writing custom events?
Can I pass form1 to form2 by-ref?
I need to do changes to it , from Form2 than open form1 with the changes....
If I pass it by-val chenges wont be shown.
Is there a way to point (pointer) to Form1?
cilaes, on 11 May 2012 - 07:40 AM, said:
Windows Forms
#7
Re: Show hidden form1 with code in form2
Posted 11 May 2012 - 11:57 AM
It's actually sometimes quite troublesome to copy (get value) of class object.
#8
Re: Show hidden form1 with code in form2
Posted 11 May 2012 - 12:01 PM
assafey, on 11 May 2012 - 12:56 PM, said:
Curtis Rutland, on 11 May 2012 - 07:37 AM, said:
There's another way using events; are you familiar with writing custom events?
Can I pass form1 to form2 by-ref?
I need to do changes to it , from Form2 than open form1 with the changes....
If I pass it by-val chenges wont be shown.
Is there a way to point (pointer) to Form1?
cilaes, on 11 May 2012 - 07:40 AM, said:
Windows Forms
Can you do it? Yes.
Should you do it? Absolutely not. Its a horrible habit and terrible coding practice. Don't develop these bad habits now.
Why would you want to? What was wrong with the two recommendatins already made to you?
- Show the second form as a dialog
- Respond to an event from the second form when it is closed.
You're making a simple little app. Don't start covering it in band-aides. Take a moment to learn the right techniques and build your skill level.
#9
Re: Show hidden form1 with code in form2
Posted 11 May 2012 - 12:09 PM
tlhIn`toq, on 11 May 2012 - 01:01 PM, said:
assafey, on 11 May 2012 - 12:56 PM, said:
Curtis Rutland, on 11 May 2012 - 07:37 AM, said:
There's another way using events; are you familiar with writing custom events?
Can I pass form1 to form2 by-ref?
I need to do changes to it , from Form2 than open form1 with the changes....
If I pass it by-val chenges wont be shown.
Is there a way to point (pointer) to Form1?
cilaes, on 11 May 2012 - 07:40 AM, said:
Windows Forms
Can you do it? Yes.
Should you do it? Absolutely not. Its a horrible habit and terrible coding practice. Don't develop these bad habits now.
Why would you want to? What was wrong with the two recommendatins already made to you?
- Show the second form as a dialog
- Respond to an event from the second form when it is closed.
You're making a simple little app. Don't start covering it in band-aides. Take a moment to learn the right techniques and build your skill level.
Ok,
I will try to explain (sorry for my english
Form1 has a datagridview, Loading data from a file.
Form2 is accessible from form1.
when Form1 and Form2 are shown, the user can control both of them.
Form2 can open Form3 that changes data in the file.
Those changes need to be on DGV on Form1 also.
So after Form3 is closed I'm trying to make changes to Form1 from Form2 even if Form1 is visible false etz...
So in showdialog(this) when minimized form1, form2 also minimized....
#10
Re: Show hidden form1 with code in form2
Posted 11 May 2012 - 12:28 PM
Then you should be doing this through events, not passing the forms back and forth. Tightly binding the forms together is just bad practice and something you don't need to do.
Quote
You seem to be missing the point that form2 should not being directly affecting form1 controls. That's bad.
When you are done making changes on form2 raise an event and have form1 take care of itself.
I provided a link to you with tutorials on the right way to have form-to-form communication/interaction. Have you worked those tutorials?
How do I?
Q: ...get Form 'A' to make a change or talk to Form 'B'
A:
- See this thread for the simplest of example code between two forms.
The tutorials below actually teach and explain it further. - Passing values between forms/classes
- Bulding an application - Part 1
- Building an application - Part 2
- Quick and easy custom events
- Delegates, Lambdas and Events
This post has been edited by tlhIn`toq: 11 May 2012 - 12:29 PM
#11
Re: Show hidden form1 with code in form2
Posted 12 May 2012 - 04:09 AM
Problem resolved!
This post has been edited by tlhIn`toq: 12 May 2012 - 08:58 AM
Reason for edit:: No need to quote the entire previous post. It just makes the thread really long.
#12
Re: Show hidden form1 with code in form2
Posted 12 May 2012 - 08:57 AM
|
|

New Topic/Question
Reply




MultiQuote







|