Solved

c# How do I have Form1 send data to other forms

Posted on 2012-03-12
3
273 Views
Last Modified: 2012-03-30
Form1 creates multiple instances of Form2
Button1 on Form1 populates a textbox on Form2 but not every instance - just one or two of them depending on the situation.
How do I do this?
0
Comment
Question by:pdering
  • 2
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37713085
The same way as was described in your other question:  http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_27629120.html
0
 
LVL 3

Accepted Solution

by:
pradhanv earned 500 total points
ID: 37715241
While you create the child forms pass the reference of the parent form to them.
On click of the button in the main form throw an event. Now listen to these events in the child form and execute whatever you want depending on the situation.

using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;

namespace QuickTest {
    
    public class Class1  {
       
        public static void Main() {
            System.Console.WriteLine("Hello World!");
            Form1 f = new Form1();
            Application.Run(f);

            System.Console.ReadLine();
        }
    }
}

class Form1 : Form
{
 public delegate void ButtonClickedHandler(int i);
 public event ButtonClickedHandler ButtonClicked;
 int i=0;

 public Form1()
 {
  Button b = new Button();
  b.Click+=new EventHandler(this.Button_Clicked);
  this.Controls.Add(b);
  for(int i=0; i<3; i++)
  {
   new Form2(this).Show();
  }

}
 
 private void Button_Clicked(object o, EventArgs a )
 {
  if(ButtonClicked!=null)ButtonClicked(i++);
 }
}

class Form2 : Form
{
 RichTextBox a ;
 public Form2(Form1 f)
 {
  a = new RichTextBox();
  this.Controls.Add(a);
  f.ButtonClicked+=new Form1.ButtonClickedHandler(this.FillTextArea);
 }
 private void FillTextArea(int i)
 {
a.AppendText(""+i);
 }

}

Open in new window

0
 
LVL 3

Expert Comment

by:pradhanv
ID: 37723639
Hi pdering,
Did the posts answer your question ?
0

Featured Post

Industry Leaders: 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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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