473,473 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to access form controls from a UserControl Button

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.

Nov 16 '05 #1
4 14456
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!
Nov 16 '05 #2
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.

Nov 16 '05 #3
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!
Nov 16 '05 #4
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!


Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Ramey | last post by:
How can controls on a Windows Form be accessed (or referenced) from another Class? I know how to do it from another Form. The following doesn't work even though the Control Modifiers property is...
1
by: Mamatha | last post by:
Hi I have a small application in VB.NET,it consists of one form,one class and one module.I have declared one thread globally in module to execute the function in the form.I started that thread...
1
by: Paez | last post by:
Hi there. How can I modify a form control property from another form. Let's say, I have a frmMain that has a ToolStripMenuItem with the current user info. Then, I open another form...
5
by: Jon E. Scott | last post by:
I'm a little confused with "static" methods and how to access other unstatic methods. I'm a little new to C#. I'm testing a callback routine within a DLL and the callback function returns a...
1
by: ykhamitkar | last post by:
Hi there, I have created one ms access form which based on a table. Now when I use scroll wheel of my mouse on the form it automatically changes records. How can I disable scroll wheel ...
3
by: SimhachalamNaidu | last post by:
i have a form namee Form1. and i have a class myclass.now what i need is i want to modify controls of Form1 from myclass. please notice that Controls in Form1 are runtime user controls. please help...
0
by: vtelebyteM | last post by:
i have 1)User control with one MSFlexGrid on it 2)Form to place user control 3)Some form's own control i dont know how many controls are there under form i have to place them in grid columns...
1
by: mbilalk | last post by:
Hello, I have two tables 1. Person 2. Doctor DoctorId is in Person table as forfeign key. I have created a form based on Query. I am displaying person information (name, address, phone,..)...
0
by: Anuj Sengar | last post by:
Hi, I am facing a problem to access form control from usercontrol which is actually in a panel. I want to update status of statustriplabel on click of usercontrol button. Pl give some code or...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.