Results 1 to 10 of 10

Thread: Two interlinked textboxes

  1. #1

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Two interlinked textboxes

    Hi

    How do I clean a textbox when the other contains value and vice versa

    Example: Two Textbox

    Txtbox01 and Txtbox02

    When user fill Txtbox01 I must clear Txtbox02, When the user fill Txbox02 Must to clear Txtbox01


  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,391

    Re: Two interlinked textboxes


  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Two interlinked textboxes

    @jdc2000, you linked a .Net conversation, but point should be taken -- monitor the Change event.

    Tip to Mutley: If you clear a textbox, the change event will also be triggered, if there was anything in the textbox to begin with. So, I'd think that inside your textbox change event, you'd want to test if the change was because you cleared it from your other textbox.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,817

    Re: Two interlinked textboxes

    Yeah, this is something I do in one particular place. I've got a form whereby people do physical exams. However, different clients have me design custom forms for their practices. I use the original form as a "master", but then they can call up their "custom" forms over the top of that "master".

    And I also have the "master" mirror any changes made to the "custom" forms, although the "master" is disabled while the "custom" form is loaded. To accomplish this, I just monitor the ..._change() event of all the controls on the "custom" form, and shove the changes back into the "master" form.

    It does get a bit tricky though. When the "custom" form is initially loaded, I pull all of the initial values out of the "master" form. Therefore, to get it loaded (in the "custom" form's load event), I set a module level mbLoading variable. If that's true, I just immediately exit those ..._change() events. That allows me to get the "custom" form loaded without the code jumping all over the place.

    Here's an example of what I do:

    Code:
    
    Private Sub cboLeftOrthosis_Change()
        If mbLoading Then Exit Sub
        frmAddEditPhysicalExams.cboLeftOrthosis.Text = cboLeftOrthosis.Text
    End Sub
    

    That's a ComboBox, but same idea.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,382

    Re: Two interlinked textboxes

    Huh?
    Why not use the KeyUp-Events?
    Aircode!
    Code:
    Private Sub Textbox01_KeyUp(whateverargumentsarehere) 'Too lazy to look them up.
    If Len(Textbox01)>0 And Len(Textbox02)>0 Then Textbox02.Text=""
    End Sub
    
    Private Sub Textbox02_KeyUp(whateverargumentsarehere) 'Too lazy to look them up.
    If Len(Textbox01)>0 And Len(Textbox02)>0 Then Textbox01.Text=""
    End Sub
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE 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.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Two interlinked textboxes

    Quote Originally Posted by Zvoni View Post
    Huh?
    Why not use the KeyUp-Events?
    ...
    One reason, if you paste text into the textbox, the Key Up event is not triggered.

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,382

    Re: Two interlinked textboxes

    Quote Originally Posted by passel View Post
    One reason, if you paste text into the textbox, the Key Up event is not triggered.
    Hmm, true.

    Code:
    Private Sub TextBox1_Change()
    If Len(TextBox1) > 0 And Len(TextBox2) > 0 Then TextBox2.Text = ""
    End Sub
    
    Private Sub TextBox2_Change()
    If Len(TextBox1) > 0 And Len(TextBox2) > 0 Then TextBox1.Text = ""
    End Sub
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE 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.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Two interlinked textboxes

    Another option would be to check to see if the textbox has focus
    Code:
    Option Explicit
    
    Private Sub Text1_Change()
    If Me.ActiveControl Is Text1 Then Text2.Text = vbNullString
    End Sub
    
    Private Sub Text2_Change()
    If Me.ActiveControl Is Text2 Then Text1.Text = vbNullString
    End Sub

  9. #9
    Addicted Member
    Join Date
    Jun 2018
    Posts
    189

    Re: Two interlinked textboxes

    Just to reuse...

    Code:
    Option Explicit
    
    Private Sub Text1_Change()
        ClearTbx Text1, Text2
    End Sub
    
    Private Sub Text2_Change()
        ClearTbx Text2, Text1
    End Sub
    
    Private Sub ClearTbx(tbx1 As TextBox, tbx2 As TextBox)
      If ActiveControl Is tbx1 Then tbx2.Text = ""
    End Sub

  10. #10
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,382

    Re: Two interlinked textboxes

    Just to nitpick...
    DM's/PGB's Solution sets the Text of the other textbox to empty every time the active Textbox changes, even if it's not necessary (a.k.a the other textbox already being empty)

    Code:
    Private Sub ClearTbx(tbx1 As TextBox, tbx2 As TextBox)
      If ActiveControl Is tbx1 and Len(tbx2)>0 Then tbx2.Text = ""
    End Sub
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE 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.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

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