Results 1 to 15 of 15

Thread: Change Event of Textbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    466

    Change Event of Textbox

    I use quite a lot the Change Event of invisible Textbox in order to have the event fired when I set a value to the textbox.
    So I when the Event is fired, Code is run.

    The downside is that it I need to have invisible Textbox xixh are graphical objects.

    Does someone have an idea to replace that way?

    Thanks

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    4,600

    Re: Change Event of Textbox

    I use quite a lot the Change Event of invisible Textbox in order to have the event fired when I set a value to the textbox.
    Instead of changing the content of the invisible Textbox you can also put the code of the Change event ib a separate subroutine and call this.
    Code:
    Private Sub MyHeavyRoutineWithChangeEvent()
      Do Until PowerLoss
         '
         ' A lot a heavy processing
         '
         txtInvisible.Text = "Update me now: " & PercentageComplete
      Loop
    End Sub
    
    Private Sub txtInvisible_Change()
      ' Do whatever you need to do
    End Sub
    Code:
    Private Sub MyHeavyRoutine()
      Do Until PowerLoss
         '
         ' A lot a heavy processing
         '
         pUpdateMeNow PercentageComplete
         'txtInvisible.Text = "Update me now: " & PercentageComplete
      Loop
    End Sub
    
    Private Sub pUpdateMeNow(ByVal Percentage As Double)
      ' Do whatever you need to do
    End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    466

    Re: Change Event of Textbox

    This is not the way I use it.
    It is after an action (mainly), and even sometimes even between forms

    For example :

    In a button click : (Choose something)
    tbID.Text = "1"

    In the event

    Private Sub tbID_Change()

    Call ShowData(tbID.Text)

    End Sub

  4. #4
    PowerPoster
    Join Date
    Jun 2013
    Posts
    5,497

    Re: Change Event of Textbox

    I'd handle that in a dedicated ChangeIDto(NewID) routine:

    Code:
    Sub ChangeIDto(NewID)
      tbID.Text = NewID 'just reflect the ID in a disabled TextBox
      ShowData NewID 'do some additional action, based on the new ID
    End Sub
    
    Private Sub btnSomeButton_Click()
      ...
      ChangeIDto 123
    End Sub
    No TextBox-Change-EventHandler necessary in this case.

    Olaf

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    466

    Re: Change Event of Textbox

    Olaf, this could be a solution of course, but as I do it quite a lot, and also between forms, I dhould create as many functions as _Change events (or 1 function with a big select case)

    Of couser I can create a working class like this
    Code:
    Option Explicit
    
    Private sID             As String
    
    Public Event Change(sID As String)
    
    Public Property Let ID(NewValue As String)
    
       sID = NewValue
       RaiseEvent Change(sID)
    
    End Property
    
    Public Property Get ID() As String
    
       ID = sID
    
    End Property
    And the code
    Code:
    Option Explicit
    
    Private WithEvents oTest As Class1
    
    Private Sub Command1_Click()
    
       oTest.ID = "tested"
    
    End Sub
    
    Private Sub Form_Load()
    
       Set oTest = New Class1
    
    End Sub
    
    Private Sub oTest_Change(sID As String)
    
       MsgBox sID
    
    End Sub
    But it also needs
    Set oTest = New Class1

    And not sure, if replaceing a textbox with such will be benefit (replaceing GDI handles by something else, and in a lot of code)

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,627

    Re: Change Event of Textbox

    Why use an invisible text box at all? If the user can not see it and can not enter data into it then a text box makes no sense. Sounds like you should be using a variable. Since you are setting the value of the text box there should be no need for an event to be fired you can simply execute the code when you change the value.

  7. #7
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,111

    Re: Change Event of Textbox

    Quote Originally Posted by Thierry69 View Post
    This is not the way I use it.
    It is after an action (mainly), and even sometimes even between forms

    For example :

    In a button click : (Choose something)
    tbID.Text = "1"

    In the event

    Private Sub tbID_Change()

    Call ShowData(tbID.Text)

    End Sub
    This is utterly unreliable because if tbID.Text is already "1" then assignment tbID.Text = "1" does *not* raise the Change event. Check out this snippet

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Text1.Text = "1"
        Text1.Text = "1"
        Text1.Text = "1"
        Text1.Text = "1"
    End Sub
    
    Private Sub Text1_Change()
        Debug.Print Text1.Text, Timer
    End Sub
    It produces a single (initial) debug print in Immediate Window and misses notification on subsequent assignments.

    cheers,
    </wqw>

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    466

    Re: Change Event of Textbox

    Why use an invisible text box at all? If the user can not see it and can not enter data into it then a text box makes no sense. Sounds like you should be using a variable. Since you are setting the value of the text box there should be no need for an event to be fired you can simply execute the code when you change the value.
    The textbox is used as a kind of variable, BUT used to have the event changed Fired
    You can't do that with a variable

    Quote Originally Posted by wqweto View Post
    This is utterly unreliable because if tbID.Text is already "1" then assignment tbID.Text = "1" does *not* raise the Change event. Check out this snippet

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Text1.Text = "1"
        Text1.Text = "1"
        Text1.Text = "1"
        Text1.Text = "1"
    End Sub
    
    Private Sub Text1_Change()
        Debug.Print Text1.Text, Timer
    End Sub
    It produces a single (initial) debug print in Immediate Window and misses notification on subsequent assignments.

    cheers,
    </wqw>
    This is the reason why I use Change Event, if the text is not changed, then no event is fired.

  9. #9
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,560

    Re: Change Event of Textbox

    It still doesn't make sense to use an invisible textBox just to have a "ready-to-use" Event firing.

    In your post #5 you actually showed how it would be the proper way.
    The only difference being, in Your Let-Property you would have to encase the code in a
    If sID<>NewValue Then
    sID=NewValue
    RaiseEvent Change(sID)
    End If

    Your argument, that you would need a
    Private WithEvents oTest As Class1
    and a
    Set oTest=New Class1

    What do you think vb6 is doing in the Background with your invisible Textbox?
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    466

    Re: Change Event of Textbox

    Zvoni, in my post 5, it was a quick and dirty class.

    > What do you think vb6 is doing in the Background with your invisible Textbox?
    Probably doning the same, and this should probably the solution I'll do (using a class), but I wanted to have some advices here, if a better solution could exists.

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,627

    Re: Change Event of Textbox

    Quote Originally Posted by Thierry69 View Post
    The textbox is used as a kind of variable, BUT used to have the event changed Fired
    You can't do that with a variable
    This is the reason why I use Change Event, if the text is not changed, then no event is fired.
    But you can easily do that with a variable and a conditional test. If you are going to be calling it from multiple places just create a little sub.
    Code:
    Sub SetVar(Value as String)
        If Value<>MyVar then
             MyVar=Value
             Text Changed so do whatever
        End If
    End Sub
    And of course you could use a class and a property and place code in the let section if you prefer.

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,519

    Re: Change Event of Textbox

    Why not just write the action as a method other code can call?

  13. #13
    PowerPoster
    Join Date
    Feb 2017
    Posts
    3,275

    Re: Change Event of Textbox

    For those cases I usually make public properties, and update what needs to be updated only when the property value changes.

    But anyway, if you have all working, why do you want to change it now, just to avoid some GDI objects?

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    466

    Re: Change Event of Textbox

    Eduardo, you are right, everything working, but I always tr to make it the fastest way as possible.
    GDI object is comsumuning ressources.
    So trying to find a better way

    I am implementing the mains solution (class with withevents) but maybe the community could have a better solution

  15. #15
    PowerPoster
    Join Date
    Feb 2017
    Posts
    3,275

    Re: Change Event of Textbox

    Quote Originally Posted by Thierry69 View Post
    Eduardo, you are right, everything working, but I always tr to make it the fastest way as possible.
    GDI object is comsumuning ressources.
    So trying to find a better way
    Not much resources I think.

    Quote Originally Posted by Thierry69 View Post
    I am implementing the mains solution (class with withevents) but maybe the community could have a better solution
    I still think that that's not needed, simple properties would do the job:

    Code:
    Private mPropValue As String
    
    Public Property Get PropValue() As String
        PropValue = mPropValue
    End Property
    
    Public Property Let PropValue(nValue As String)
        If nValue <> mPropValue Then
            mPropValue = nValue
            ' Code to update displayed items
        End If
    End Property

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