11 Replies - 2319 Views - Last Post: 12 May 2012 - 08:57 AM Rate Topic: -----

#1 assafey  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 08-July 09

Show hidden form1 with code in form2

Posted 11 May 2012 - 06:23 AM

Hi,

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?

Is This A Good Question/Topic? 0
  • +

Replies To: Show hidden form1 with code in form2

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Show hidden form1 with code in form2

Posted 11 May 2012 - 06:37 AM

Well, there are a few ways. You could pass a reference to the active Form1 using the keyword this to Form2. You'd have to add a parameter or a constructor argument to accept an instance of Form1 to Form2. Then, you can .Show that instance.

There's another way using events; are you familiar with writing custom events?
Was This Post Helpful? 0
  • +
  • -

#3 cilaes  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 75
  • Joined: 12-December 11

Re: Show hidden form1 with code in form2

Posted 11 May 2012 - 06:40 AM

Are you using WPF or Windows Forms?
Was This Post Helpful? 0
  • +
  • -

#4 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: Show hidden form1 with code in form2

Posted 11 May 2012 - 07:04 AM

Make one form be child of other. So lets say Form2 have:
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

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,217
  • Joined: 02-June 10

Re: Show hidden form1 with code in form2

Posted 11 May 2012 - 08:00 AM

Child forms are never really supposed to know much of their parents.

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
Spoiler




View PostNerfpl, on 11 May 2012 - 08:04 AM, said:

Make one form be child of other. So lets say Form2 have:
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.
Was This Post Helpful? 0
  • +
  • -

#6 assafey  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 08-July 09

Re: Show hidden form1 with code in form2

Posted 11 May 2012 - 11:56 AM

View PostCurtis Rutland, on 11 May 2012 - 07:37 AM, said:

Well, there are a few ways. You could pass a reference to the active Form1 using the keyword this to Form2. You'd have to add a parameter or a constructor argument to accept an instance of Form1 to Form2. Then, you can .Show that instance.

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?

View Postcilaes, on 11 May 2012 - 07:40 AM, said:

Are you using WPF or Windows Forms?


Windows Forms
Was This Post Helpful? 0
  • +
  • -

#7 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: Show hidden form1 with code in form2

Posted 11 May 2012 - 11:57 AM

Classes are passed by ref. Structs are passed by val.

It's actually sometimes quite troublesome to copy (get value) of class object.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,217
  • Joined: 02-June 10

Re: Show hidden form1 with code in form2

Posted 11 May 2012 - 12:01 PM

View Postassafey, on 11 May 2012 - 12:56 PM, said:

View PostCurtis Rutland, on 11 May 2012 - 07:37 AM, said:

Well, there are a few ways. You could pass a reference to the active Form1 using the keyword this to Form2. You'd have to add a parameter or a constructor argument to accept an instance of Form1 to Form2. Then, you can .Show that instance.

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?

View Postcilaes, on 11 May 2012 - 07:40 AM, said:

Are you using WPF or Windows Forms?


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.
Was This Post Helpful? 0
  • +
  • -

#9 assafey  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 08-July 09

Re: Show hidden form1 with code in form2

Posted 11 May 2012 - 12:09 PM

View PosttlhIn`toq, on 11 May 2012 - 01:01 PM, said:

View Postassafey, on 11 May 2012 - 12:56 PM, said:

View PostCurtis Rutland, on 11 May 2012 - 07:37 AM, said:

Well, there are a few ways. You could pass a reference to the active Form1 using the keyword this to Form2. You'd have to add a parameter or a constructor argument to accept an instance of Form1 to Form2. Then, you can .Show that instance.

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?

View Postcilaes, on 11 May 2012 - 07:40 AM, said:

Are you using WPF or Windows Forms?


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....
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,217
  • Joined: 02-June 10

Re: Show hidden form1 with code in form2

Posted 11 May 2012 - 12:28 PM

That's fine. That explains why you can't use .ShowDialog().
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

I'm trying to make changes to Form1 from Form2

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:

This post has been edited by tlhIn`toq: 11 May 2012 - 12:29 PM

Was This Post Helpful? 0
  • +
  • -

#11 assafey  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 08-July 09

Re: Show hidden form1 with code in form2

Posted 12 May 2012 - 04:09 AM

Wow thank you!!
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.

Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,217
  • Joined: 02-June 10

Re: Show hidden form1 with code in form2

Posted 12 May 2012 - 08:57 AM

Hurray! Well done.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1