Results 1 to 9 of 9

Thread: [RESOLVED] How to make a variable to act on events like Textbox.TextChanged

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2013
    Location
    Norway; Haugesund
    Posts
    39

    Resolved [RESOLVED] How to make a variable to act on events like Textbox.TextChanged

    Hello again!

    As stated in the title, I'm wondering if there is a method to create a variable which will act on events like Textbox.TextChanged?
    I've done some research on the web, but I don't seem to get anywhere. However, as also mentioned in my other thread,

    (http://www.vbforums.com/showthread.p...21#post4828321)

    Niya's multi threading article has a working method, but I'm not sure how to make it all clean. Like just a sub which will react on the TextChanged event.

    I hope anyone with this knowledge is willing to give me the answer

    Kind regards,

    - Frek

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to make a variable to act on events like Textbox.TextChanged

    Your request doesn't actually make sense. Variables don't act on events. A variable is just a place to store some data. You put data in and get that data out. That's it. As for events, they get raised and then any methods registered to handle them are executed. That's it. Perhaps you could provide a FULL and CLEAR explanation of the problem.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2013
    Location
    Norway; Haugesund
    Posts
    39

    Re: How to make a variable to act on events like Textbox.TextChanged

    I read through my post a couple of times, and I got an idea. It worked. I feel very sorry for asking this question now, but it might be useful for other new people. The code was as simple as this:

    Code:
    'Variable
    Private WithEvents myTextChanged As New TextBox
    
    'Sub
    
    Private Sub myTextChanged_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles myTextChanged.TextChanged
            MsgBox(myTextChanged.Text) 'Displays the message given.
    End Sub
    
    'Call method
    
    myTextChanged.Text = "My new string"
    I don't really think this belongs in this forum, but maybe the code bank?

    Anyway, have a nice day guys!

    - Frek

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2013
    Location
    Norway; Haugesund
    Posts
    39

    Re: How to make a variable to act on events like Textbox.TextChanged

    Quote Originally Posted by jmcilhinney View Post
    Your request doesn't actually make sense. Variables don't act on events. A variable is just a place to store some data. You put data in and get that data out. That's it. As for events, they get raised and then any methods registered to handle them are executed. That's it. Perhaps you could provide a FULL and CLEAR explanation of the problem.
    Thanks for showing interest in my request! I'm sorry my request didn't make sense, and I will try to be more accurate in the future. I simply wanted a variable to act like a textbox. Like when the textbox's text was changed, an event would happen (TextChanged). I was looking for the same feature with variables. I though about this a couple of times, and why not use a textbox with no UI instead? I guess there is a reason why it works this way, and my request was probably that reason. There is no need for an event to happen when a variables content is changed, as you can use other objects, such as a textbox. Then as you said, a variable is just there to input or output stored information.

    - Frek
    Last edited by Frekvens1; Feb 2nd, 2015 at 08:22 AM. Reason: Spellings

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    527

    Re: [RESOLVED] How to make a variable to act on events like Textbox.TextChanged

    I'm glad you were able to find a solution by using a textbox, but just to expand on the subject a bit, if the textbox class didn't exist, the way to achieve the result you were after would have been by creating a class that would store your variable, and adding a custom event to react when that variable is changed through a class property.

    Here is how you can create custom events: http://www.codeproject.com/Articles/...lass-in-VB-Net

    Hope this helps.
    Don't ask why, just reboot!

  6. #6
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: [RESOLVED] How to make a variable to act on events like Textbox.TextChanged

    Just to expand on Alains post, it could be a simple as this....

    VB Code:
    1. Private _myProp As String
    2.     Public Event myProp_Changed()
    3.     Public Property myProp() As String
    4.         Get
    5.             Return _myProp
    6.         End Get
    7.         Set(ByVal value As String)
    8.             If value <> _myProp Then
    9.                 value = _myProp
    10.                 RaiseEvent myProp_Changed()
    11.             End If
    12.  
    13.         End Set
    14.     End Property
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2013
    Location
    Norway; Haugesund
    Posts
    39

    Re: [RESOLVED] How to make a variable to act on events like Textbox.TextChanged

    Quote Originally Posted by Alain View Post
    I'm glad you were able to find a solution by using a textbox, but just to expand on the subject a bit, if the textbox class didn't exist, the way to achieve the result you were after would have been by creating a class that would store your variable, and adding a custom event to react when that variable is changed through a class property.

    Here is how you can create custom events: http://www.codeproject.com/Articles/...lass-in-VB-Net

    Hope this helps.

    Quote Originally Posted by kebo View Post
    Just to expand on Alains post, it could be a simple as this....

    VB Code:
    1. Private _myProp As String
    2.     Public Event myProp_Changed()
    3.     Public Property myProp() As String
    4.         Get
    5.             Return _myProp
    6.         End Get
    7.         Set(ByVal value As String)
    8.             If value <> _myProp Then
    9.                 value = _myProp
    10.                 RaiseEvent myProp_Changed()
    11.             End If
    12.  
    13.         End Set
    14.     End Property
    I can't thank you guys enough! This is exactly what I was looking for. No, seriously, I really mean it. I can say you guys just improved the performance of my software! You have my respect

    - Frek

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: [RESOLVED] How to make a variable to act on events like Textbox.TextChanged

    I had already shown you this in your last thread how to raise events. Events should only ever be raised in One place. Read JMC's blog above in his signature that has a detailed tutorial on events.
    My Github - 1d3nt

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] How to make a variable to act on events like Textbox.TextChanged

    Quote Originally Posted by ident View Post
    I had already shown you this in your last thread how to raise events. Events should only ever be raised in One place. Read JMC's blog above in his signature that has a detailed tutorial on events.
    Specifically here:

    http://jmcilhinney.blogspot.com.au/2...om-events.html
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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
  •  



Click Here to Expand Forum to Full Width