PC Review


Reply
Thread Tools Rate Thread

How to access form controls from a UserControl Button

 
 
athlonman
Guest
Posts: n/a
 
      7th Jan 2005
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.

 
Reply With Quote
 
 
 
 
Maqsood Ahmed
Guest
Posts: n/a
 
      7th Jan 2005
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
athlonman
Guest
Posts: n/a
 
      7th Jan 2005
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.

 
Reply With Quote
 
Maqsood Ahmed
Guest
Posts: n/a
 
      8th Jan 2005
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
athlonman
Guest
Posts: n/a
 
      10th Jan 2005
Ohhh Yeah!!!!! That works and do exactly what i'm looking for!!!!

Many thanks Ahmed!!!!!



Maqsood Ahmed wrote:
> 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
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Option Button in form controls Pnoahjones Microsoft Excel Programming 3 8th Apr 2010 03:59 PM
bind controls on a usercontrol to bindingsource from anotherform/usercontrol jogisarge Microsoft C# .NET 0 18th Dec 2008 08:10 AM
How to make a Add button to add new controls in the form Bon Microsoft Access Forms 1 9th Jan 2006 03:28 PM
Access my UserControl from my form? =?Utf-8?B?U3RldmU=?= Microsoft Dot NET 0 24th Jun 2005 03:58 PM
Re: How to access form public variable from usercontrol John Wood Microsoft C# .NET 0 9th Aug 2003 10:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:33 PM.