-
October 20th, 2011, 10:36 AM
#1
Accessing UserControl from a Class
I am trying to create a UserControl to put on a form and have started a UserControl Project in VS2010. On my control I have placed a couple of other controls, lets just use a simple text box as an example. I would like to access and change the text in that text box from another class in the same project.
for example purposes lets say
the UserControl is in the class "UserControl"
The second class is called "Class2" with the method "ChangeText()"
and the textbox is called "textbox1"
Now creating a new instance of the class UserControl and accessing the text box through that does not work since it does not change the text of the textbox displayed on my main control.
i.e.
UserControl example = new UserControl()
example.textbox1.text = "Text"
Also I do not want to change the test in the text box by calling creating a instance of Class2 and giving the method the parameters textbox1.
i.e.
Class2 test = new Class2();
test.ChangeText(textbox1)
What I do want is to be able to call the method "ChangeText()" without giving it any parameters and having the text in textbox1 changed.
1.e.
Class2 test = new Class2();
test.ChangeText();
-
October 20th, 2011, 10:38 AM
#2
Re: Accessing UserControl from a Class
This is in the 4.0 .NET Framework
-
October 21st, 2011, 04:17 AM
#3
Re: Accessing UserControl from a Class
This can be done by events and delegates
Create a delegate in the project. Create a new event in Class1.cs and register it to the method which has the same signature as that of the delegate ( the method should be in the UserControl class )
Invoke the event in the ChangeText() which would automatically call the method in the UserControl class where in you can change the contents of the text box
Thanks
Murali
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|