Click here to Skip to main content
14,984,271 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts
I am working with window form. and i have two user control 1)First Control have two text box and one OK button 2)Second Control have grid view only
both control are apply on window form when i Press OK button from control first data show in grid view.
then can you help me how to communicate between two user control using delegate
in C#.

"One Window Form Having Two User Control One For data enter(two text box and one OK button) and another show the data enter by you(grid view). as you press the ok button of control first grid refresh and show data entered by you"
Window Form1
|  user control1                  |
| -------------------             |
|    Textbox1       |             | 
|    TextBox2       |             | 
|    Ok button      |             | 
|  -----------------              | 
|   user control2                 |
|  -------------------            |
|  |   Grid View      |           |
|  -------------------            | 

Thank U
Updated 17-Oct-11 2:46am
BillWoodruff 17-Oct-11 6:36am
Please tell us more specifically what's going on here: you have a Form; on it are two UserControls; the first one has two TextBoxes and a Button; the second a GridView ... now when you press the Button ... exactly what happens : are you reading the contents of the TextBoxes on the first Form and then inserting them in the GridView ?

Does the GridView on the second Form every do anything that changes or interacts with the first Form ?
Er. Dinesh Sharma 17-Oct-11 6:51am
Both control on same Form.
Anil Honey 206 17-Oct-11 7:22am
you want code for this or? or any problem your facing?
Er. Dinesh Sharma 17-Oct-11 7:37am

1 solution

Okay, let's assume that you put a public property in UserControl2 that exposes the GridView:

// in UserControl2
public GridView uc2GridView { get; set; }

// in UserControl2's Load event:
uc2GridView = theGridView; // whatever you named your GridView

Now your Form, which has created both UserControls, can 'see' the exposed GridView ... but UserControl1 cannot. Or ... think a minute ... since the Parent of UserControl1 is probably the Form ... maybe it can ?

At this point a typical solution would be to have the Button Click event in UserControl1 raise a custom Event which would be subscribed to by the Form : that custom event would do the right thing to read the TextBox(es) data in UserControl1, and pass it to the Form (probably using a custom EventArgs class). And then the Form, using its access to the exposed GridView in UserControl2, can do the right thing to manipulate it, or call a Public method you've made accessible in UserControl1 to do what you need to do.

Now, another strategy you could pursue would be to have UserControl1 expose its TextBoxes to the Form, just like UserControl2 exposed its GridView.

But, that still leaves you with the issue of how do you notify the Form that the Button in UserControl1 has been pressed ?

And that's what you need to work on:

1. How to create an EventArgs class to hold the data you need to pass when an Event occurs.

2. How to define a Delegate

3. How to implement a public Event in a class or usercontrol, or whatever, that external code can subscribe to.

Master these basic skills, and you will be on your way to mastery of C#, and what a great feeling that will be !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900