Solved

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

Posted on 2012-03-12
3
271 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# guarantee sql connection close 6 50
2 questions 10 29
Expression Evaluater 3 37
Using data from certificate to re-create certificate programmatically 3 25
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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