dcsimg
Results 1 to 3 of 3

Thread: [RESOLVED] Textbox change string without focus

  1. #1

    Thread Starter
    Fanatic Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    820

    Resolved [RESOLVED] Textbox change string without focus

    Hi

    I have a textbox. I want to change the text inside the textbox by clicking on it as follows:

    Code:
    If TextBox1.Text = "" Then
    TextBox1.Text = "YES"
    End If
    
    If TextBox1.Text = "YES" Then
    TextBox1.Text = "NO"
    End If
    
    If TextBox1.Text = "NO" Then
    TextBox1.Text = "YES"
    End If
    So basically I 'toggle' YES or NO by clicking.. It is working but it feels... like I can maybe do it better...


    Is there a way that I can toggle YES or NO by clicking on textbox:

    1 - without giving textbox focus? i dont like cursor blinking
    2 - without selecting text/highlight text when clicking?


    thanks
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

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

    Re: Textbox change string without focus

    1. Either set the focus to something else, or change the control from a textbox to another kind of control that doesn't get focus (such as a label).

    2. Set the Selection yourself after setting the text (using SelectionStart and SelectionLength).


    By the way, the code you posted wont behave correctly (if any of those three value are there, the final value after the If's will be "YES", because each one sets to the value that the next one checks). It should be like this:
    Code:
    If TextBox1.Text = "" Then
       TextBox1.Text = "YES"
    ElseIf TextBox1.Text = "YES" Then
       TextBox1.Text = "NO"
    ElseIf TextBox1.Text = "NO" Then
       TextBox1.Text = "YES"
    End If
    ..or:
    Code:
    Select Case TextBox1.Text
    Case "", "NO"
       TextBox1.Text = "YES"
    Case "YES"
       TextBox1.Text = "NO"
    End Select

  3. #3

    Thread Starter
    Fanatic Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    820

    Re: Textbox change string without focus

    Thank you very much SI

    Yes I did not know how to say that it was not "behave correctly" it was not feel right. I now change it to label and use your 2nd method.
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width