?
Solved

Forms Communication

Posted on 2004-08-26
7
Medium Priority
?
255 Views
Last Modified: 2010-04-15
If I have a windows forms application in c# which has a main form with a button on it.  When this button is clicked another form is opened and the main form is hidden.  when the second form is closed I want to unhide the main form.  How do I do this - ie how do I access the main forms properties form the other form?

Thanks

Dave
0
Comment
Question by:daveamour
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Expert Comment

by:develc
ID: 11901283
Hello Dave,

maybe that helps you :

Second From closed :
{
                  FormMain<Name> fMain = new FormMain<Name>s();
                  fMain.Show();
}

Access to the main forms properties you can get on the same way or you can use a global class :

public class Global

{

 public Global()

 {

   

 }

 public static SortedList Data1 = null;

 public static ArrayList Data2 = null;

 public static Form1 FormMain = null;

 public static string strExample = null;

// and so on

}

then you can bind the class to your MainForm and others using static.

Good luck.

Best regards develc
0
 
LVL 1

Expert Comment

by:johanjohansson
ID: 11901284
You don't have to do it that way. When the subform is started using ShowDialog it will stop at that line until the subform is closed.

void someMethodInMainForm()
{
   SubForm sf = new SubForm();
   this.Hide();
   DialogResult dr = sf.ShowDialog();
   this.Show();
}
0
 
LVL 19

Author Comment

by:daveamour
ID: 11901418
johan - this is very useful but might not give me enough control - ie I might have the subform closing and opening another one and not until I explicitly decide will I want to show the main form again.  This is very useful though, thanks.

develc - Not sure how to bind the class to my main form.  Also with your first example aren't you just creating another instance of my main form which will leave you with 2 running and 1 of them hidden?

Thanks

Dave
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Accepted Solution

by:
EBatista earned 200 total points
ID: 11901629
hi all, it is better to use the owner property:
//in FormMain

public string sMessage;  //public property/variable

//button_click event
Form2 frm=new Form2();
frm.Owner=this;
frm.Show();
this.Hide();

/*then within Form2 you can retrieve the property/variable value by retrieveing the actuall instanece of FormMain that have opened Form2*/
TexBox1.Text=((FormMain)this.Owner).sMessage;

//Form2 closing event
((FormMain)this.Owner).Show();

hope this help
Elio
0
 
LVL 19

Author Comment

by:daveamour
ID: 11901715
Thanks EBatista this is exactly what I wanted!

I uses this.Owner.Show() though without the cast.  Why did you use a cast?

Thanks

Dave

PS If I have another method in frmMain eg:

            public void SayHello()
            {
                  MessageBox.Show("Hello");
            }

How would I call this from the second form - it doesn't work using the above method but the Show method does?
0
 
LVL 10

Expert Comment

by:EBatista
ID: 11902238
well, the cast is needed cos the Owner property return a generic Form object, so you have to convert it to the correct type which is frmMain in order to use its properties.

the MessageBox.Show("Hello") should work, it is maybe that frmMain is hidden, try it without hiding it.
0
 
LVL 19

Author Comment

by:daveamour
ID: 11902416
Ok I see now.  My code worked as my form is a generic form but if it had been inherited from a class which in turn inherits from the generic form then it would break.

I figured out the SayHello problem - I didn't have the brackets in the right place:

((frmMain)this.Owner).SayHello() is needed whereas I was trying (frmMain)this.Owner.SayHello()

Thanks

Dave
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question