CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2011

    Question 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.
    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.
    Class2 test = new Class2();

    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.
    Class2 test = new Class2();

  2. #2
    Join Date
    Oct 2011

    Re: Accessing UserControl from a Class

    This is in the 4.0 .NET Framework

  3. #3
    Join Date
    Jun 2008

    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



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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.