Solved

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

Posted on 2012-03-12
3
274 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
[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
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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