How to access form controls from a UserControl Button

A

athlonman

Hi all!!

II'm trying to set the Text of a TextBox in "Form1" when I click a
Button in a UserControl. My code is:

[Form1.cs]:
public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.TextBox textBox1;
private WindowsApplication1.UserControl1 userControl11;
{...}
}

provate void InitializeComponent()
{
{...}
this.userControl11 = new WindowsApplication1.UserControl1(this);
{...}
}

[UserControl1.cs]:
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
public UserControl1(Form1 frm)
{
InitializaComponent();
}
{...}
private void button1_Click(object sender, System.EventArgs e)
{
//Here is where I wish to set the Text of the TextBox of Form1!!!!
}

}

Is it possible?? i've been searching for help on this but i can't find
it. Hope anybody could help. BTW I'm a newbie ;)

Thanks in advance.
 
M

Maqsood Ahmed

Hello,
Make a custom event for the UserControl and consume it in the parent
form. Then set the TextBox text in the event handler.
Please have a look at the following link for Handling and Raising Events
in .net.
msdn.microsoft.com/library/en-us/cpguide/html/cpconevents.asp

Bye!

Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
A

athlonman

Thanks Ahmed. It really make clearer what i have to do. But I'm still
have a problem.

I made the event the custom event for the UserControl, and it works
fine if I use it in the UC itself, but I still cant' understand how do
you consume it in Form1. The MSDN article shows only how to consume it
in the same form, like the classic "button_click" event.
Thanks again for answer.
 
M

Maqsood Ahmed

Hello,
It'll be same as you consume events for other controls inside the form.
Suppose you have defined a delegate named MyDelegate in the usercontrol
and an event MyTextEvent. Inside the button click event handler or
usercontrol, you'll raise the custom event for setting the text in the
textbox, and consume it in the form. Please see the following code
snippet for the scenario i have just described. Suppose UserControl's
name is MyUserControl

//Inside UserControl
public delegate void MyDelegate(string textToSet);
public event MyDelegate MyTextEvent;
....

//Inside Button Click's event handler
if(MyTextEvent != null) //Checks if anyone has registered the event.
MyTextEvent("This is a sample text");

//Inside the form that contains TextBox (textBox1) and the UserControl
(myUC1)

//Inside Form's Load event.
myUC1.MyTextEvent += new MyUserControl.MyDelegate(myUC1_TextEvent);

//Event Handler for the UserControl's custom event.
protected void myUC1_TextEvent(string textToSet);
{
textBox1.Text = textToSet;
}

----------------------------------------
I hope it'll make things clear for you now :)
Bye! Cheers.

Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
A

athlonman

Ohhh Yeah!!!!! That works and do exactly what i'm looking for!!!!

Many thanks Ahmed!!!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top