Results 1 to 4 of 4

Thread: replacing contents of textbox on command button click

  1. #1

    Thread Starter
    Member
    Join Date
    May 1999
    Posts
    49

    Hi,

    In my textbox the user can enter as many 0 or 1 characters as they want.

    For example:

    "1110010101001"

    Does anybody know how I can do this:

    When the user clicks my Command1 button, all 0 characters in the text box are replaced with 1, and all 1 characters are replaced with 0?

    Gracias

  2. #2
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Cleveland, Ohio
    Posts
    263
    Code:
    Dim A, AfterString As String
    For A = 1 To Len(Text1)
    If Mid(Text1, A, 1) = "1" Then
    AfterString = AfterString + "0"
    Else
    AfterString = AfterString + "1"
    End If
    Next

  3. #3
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    No Problem use the following code

    Code:
    Option Explicit
    
    
    Private Sub Text1_Change()
    
    Static strOldText As String  'Holds old text string
    Dim i As Integer
    
    For i = 1 To Len(Text1.Text) 'Loop through each character in the text
    
        If Not (Mid$(Text1.Text, i, 1) = "0" Or Mid$(Text1.Text, i, 1) = "1") Then 'If Character is neither a 1 nor a 0
    
            Text1.Text = strOldText 'Set text to what it was before
    
            Text1.SelLength = 0 'Make sure no Text is selected
    
            Text1.SelStart = Len(Text1.Text) 'Move Cursor to end of text
    
            Exit Sub 'Don't do anything else
    
        End If
    
    Next i
    
    'If We've got to here then We've gone through all the text and it's all 1s and 0s
    strOldText = Text1.Text 'Set variable to hold text
    
    End Sub
    
    
    Private Sub Command1_Click()
    
    Dim i As Integer
    Dim strOutput As String  'new text
    
    For i = 1 To Len(Text1.Text) 'Loop through each character in the text
    
        Select Case Mid$(Text1.Text, i, 1)
    
            Case "0"  'Character is a 0
    
                strOutput = strOutput & "1"
    
            Case "1" 'Character is a 1
    
                strOutput = strOutput & "0"
    
        End Select
    
    Next i
    
    Text1.Text = strOutput 'Set text to output string
    
    End Sub

    Hope this Helps

  4. #4
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    India
    Posts
    298
    Hi,

    To check whether the user enters only zero or one in the
    text box, use the following code.


    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If Not (KeyAscii = 48 Or KeyAscii = 49) Then
    KeyAscii = 0
    End If
    End Sub



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