Results 1 to 13 of 13

Thread: Edit TextBox of an UserControl from another CLass

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    6

    Edit TextBox of an UserControl from another CLass

    Hello,
    I ned your help.
    I have an User contol let's call it UserControl1 (Public Class UserControl1) that contain a Label let's call it Label1, and two classes Class1 and Class2.
    The usercontrol1 is created (instantiated) in the Class1.
    And in the Class2 I have a function let's call it Function2 (it's a public shared function), in the function2 I have a String let's call it String2.
    What I want to do is to edit the text of Labe1 with String2, and I can't find out how to it.
    I can't add the code because it's too long.
    Thank you.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Edit TextBox of an UserControl from another CLass

    Welcome to VBForums

    In UserControl1 create a Property to set the text of the label. If you don't know how, see this: https://docs.microsoft.com/en-us/dot...ate-a-property

    In Function2, have a parameter that enables you to pass in an instance of UserControl1, then within the routine use the appropriate property on the parameter. eg:
    Code:
    Public Shared Function function2 (uc as UserControl1)
        ...
        uc.LabelText = string2
    Last edited by si_the_geek; Feb 16th, 2021 at 04:33 AM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    6

    Re: Edit TextBox of an UserControl from another CLass

    Thank you for your response. The problem is I'm working in a big project. And when the project is opened it's creating a new instance of UserControl1, and the only possibility (I think) to resolve my problem is the event (like some example in the project) but I don't know how to do it.
    I have done some research, I think I need to raise an event in the Function2 and send with this event a String2 and catch this event in the UserControl1, but I don't know how to do that,
    I tried something but it's not working.

    Quote Originally Posted by si_the_geek View Post
    Welcome to VBForums

    In UserControl1 create a Property to set the text of the label. If you don't know how, see this: https://docs.microsoft.com/en-us/dot...ate-a-property

    In Function2, have a parameter that enables you to pass in an instance of UserControl1, then within the routine use the appropriate property on the parameter. eg:
    Code:
    Public Shared Function function2 (uc as UserControl1)
        ...
        uc.LabelText = string2

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Edit TextBox of an UserControl from another CLass

    You should not use an Event for this, a Property is the way to go.

    To understand the difference between the two, think about a TextBox:
    • the .Text property allows you to set/read the text
    • the KeyPress event allows you to run code when a key is pressed.
    In this case you want the equivalent of setting the .Text property, so you should be using a property (perhaps called LabelText, but it is up to you).

    You can set the value of the property in code (such as in the Shown event of the form, so that it runs as soon as the form is first shown), or if you set the property up in the appropriate way you can set the value with the designer.

    Within the property, assign the value passed in to the labels Text property.


    While it is technically possible to use an event for this, doing so will just add extra complexity over using a property.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    6

    Re: Edit TextBox of an UserControl from another CLass

    Sorry I didn't give a lot of detail in my first post.
    The problem is the function in the Form (where I have the value I want to pass to the user control ) update in real time the value I want to pass to the UserControl.
    The user control is created in another class and I can't create another one, I have to use The first User Control created when the software is launched. Then I can't Instantiate a new Usercontrol.
    My main problem is with the instantiation of the user control it is done inside another class and displayed in the main windows (empty TextBox) like this :

    Code:
    Dim UC1 As New UserControl1()
    controle = myIO.addControl(UC1 , rec)
    And added to a list of Controls.

    And if I create a New instance of UserControl1 and modifyit. It doesn't modify the original one shown in the main window.
    And thank you again for your help.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Edit TextBox of an UserControl from another CLass

    Tthe only new information that seems relevant is that you don't seem to have a variable/property that refers to the control, so you need to get it somehow, perhaps something like this:
    Code:
    Dim UC1 as UserControl1 
    UC1 = myIO.Controls("UC1")

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    6

    Re: Edit TextBox of an UserControl from another CLass

    I just find that the variables I want to display in my user Control all are stored in a list :
    Code:
    Private shared listVariable As List(of ClassVariable)
    I think I will create a property in my UserControl and assign the list to it and then detect if the property is changed by INotifyPropertyChanged.PropertyChanged Event
    And then assign the new values to my textBox. Do you think a private shared variable (list ) will be updated automatically ?
    Last edited by mabido; Feb 16th, 2021 at 12:13 PM.

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Edit TextBox of an UserControl from another CLass

    In short: no. That's like asking if you're bank would know if your private wallet in your pocket would know if you had $20 or $10... the list is private, meaning it is only visible to the scope in which it was created, presumably the UC. That means it can only be seen from inside the UC. You couldn't even update it from outside the UC anyways. Even then, depending on what "ClassVariable" are will dictate what happens when you update the values.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    6

    Re: Edit TextBox of an UserControl from another CLass

    Thank you all for your help, I find a solution to my problem by using a timer to update each time my variables.

  10. #10
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Edit TextBox of an UserControl from another CLass

    So to recap:
    "I need would like to do X, but can't figure it out."
    "To do X, the best way, would be to do A and B, along with C."
    "But it's not that simple because of L M N O P."
    "Would have been nice to know about N O P from the start, but that doesn't really change A, B, or C. You just need a reference at M."
    "What if I do X Y Z?"
    "No, that's just plain silly."
    "Solved it by doing QUIGIBO instead."

    wth?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    6

    Re: Edit TextBox of an UserControl from another CLass

    I didn't understand the meaning of your comment, people do mistake, I am just learning I am not a pro. don't be mean to people like that.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Edit TextBox of an UserControl from another CLass

    Quote Originally Posted by mabido View Post
    I didn't understand the meaning of your comment
    Quote Originally Posted by mabido View Post
    don't be mean to people like that.
    How do you know he's being mean if you don't understand what he posted? Maybe you should not assume that criticism is being mean and consider that perhaps it's warranted and you could do better in future. Yes, people do make mistakes but that doesn't mean that they should have. A considerate person will try to consider all the relevant information before asking others to help them with a problem. If they fail to do so and it's pointed out, maybe they should take the criticism and try to do better in future than immediately jumping to passive-aggressive victimhood.

  13. #13
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Edit TextBox of an UserControl from another CLass

    I wasn't trying to be mean... I'm confused. But then again, that's not unusual for this forum... we tried to help and give you the best possible solution and you went off on a tangent with something completely different, totally disregarding any and and advice in this thread. OK, sure I realize sometimes there's deadlines to be met, and sometimes there's a quick and dirty band-aid that needs to be applied, but sometimes you also should take the time to apply the tourniquet and do it right, or it's going to come back around and bite you in the back side. I hope there's someone checking your work and mentoring you. I sincerly do. And I hope they look at that solution and say, "no that's not an ideal solution, let's try this." That;s what my lead does... he lets us have enough slack in our rope to explore the line on our own, but he checks our work afterwards, and when he finds something that isn't quite right, we have a discussion as to why it isn't right.

    So on that note: Timers ... notoriously unreliable. They can and will fire off when you don't want them, and not fire off when you do want them. Also depending on what interval you set up, you have effectively created a poll/wait situation that continuously checks something and updates another, that can result in high CPU usage (can, doesn't mean it will) which means you're just doing things when you don't really need to be doing them. It ends up being like that Verizon commercial: "Have you changed yet?" "How about now?".... "What about now?" ... "What about now?"... and so on... That's just ridiculous.

    THAT's why the suggestion was to include a property in your user control that exposes the label caption. It's not that hard. I get that your UCs are dynamically created. So F%^ what. Create a List(Of) with your user controls in it so you can keep track of them later, find the one you want, send it the value you want using the property. Boom, done. It's not rocket science. It isn't brain surgery either. Honestly I can't even imagine how a timer would be the solution in this case. The data originates outside the UC, so the data needs to be pushed into it.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width