Results 1 to 15 of 15

Thread: [RESOLVED] Text one char at a time

  1. #1

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Resolved [RESOLVED] Text one char at a time

    I am trying to get text from Textbox1 to Textbox2 one letter at a time below is what I tried but it only shows the last letter, I figure I need to move along after it prints each letter and tried this.

    Code:
    Imports System.Threading
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            For txt = 0 To TextBox1.Text.Length - 1
                TextBox2.Text = TextBox1.Text.Chars(txt) & Chr()
                Thread.Sleep(250)
            Next
    
        End Sub
    
    End Class
    Learning is a never ending subject.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Text one char at a time

    If I presented you the following three lines of code:

    Code:
    Textbox2.Text = "A"
    Textbox2.Text = "B"
    Textbox2.Text = "C"
    what is the content of Textbox2.Text at the end of those three lines of code? Hint: it isn't "ABC"

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Text one char at a time

    Don't try to get fancy with the name of the loop counter in a For loop. It is universally considered good practice to use 'i' and if you see 'i' in code then you should assume that it's a loop counter for a For loop. That means not using 'i' for something else. If you have multiple nested loops it is generally accepted that you use 'j' for the second loop and 'k' for the third. If you want to break those conventions then you should do as you should for any other variable, i.e. use a descriptive name. In this case, 'txt' is a bad name. If I read that, I would assume that it contains text. The obvious name in this case is 'charIndex'.

    You shouldn't be using a For loop in this case at all anyway. If you're using your loop counter only as an index into a single list and it steps from the start to the end of that list by 1, you should be using a For Each loop to begin with.

    The Text property of a TextBox is a String, if you use '=' assign to a String variable, what happens? Does the new value gat added to the old value? Of course not. It replaces it, just as OptionBase1 is pointing out. How do you usually add two Strings together? You obviously know because you're doing it else where in your code. That said, you should note that 'a = a & b' can be more succinctly written as 'a &= b'. Even better though, a TextBox has an AppendText method that does exactly as the name suggests. It is actually better than using & because it places the caret at the end of the text.

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

    Re: Text one char at a time

    I think he wants to see the characters show up over time, as if someone is typing, otherwise, why copy the characters one at a time.
    You should probably setup a timer, and enable it when you click the button.
    In the timer you would keep track of which letter you are copying, and append it to the output textbox.
    You would probably want to check in the click if the timer is already enabled or not, and if it is, do nothing.
    If the timer is not enabled, then clear the output textbox (if desired), and enable the timer.

    When the last character has been copied, then have the timer disable itself.
    You might want to lock the input textbox while copying, so that the user can't delete or modify the text in the middle of your "typing" the letters to the second textbox.

    You could do the following, which would be like you were trying, but it does hang up the GUI for other updates while the text is being copied.
    Code:
    Imports System.Threading
    Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            TextBox2.Clear()
            For Each c As Char In TextBox1.Text
                TextBox2.AppendText(c.ToString)
                TextBox2.Refresh()
                Thread.Sleep(250)
            Next
        End Sub
    End Class
    Using a timer is almost always a better option when updating the GUI over time, or using a Background thread, but a background thread would normally be best for code that doesn't update the GUI.
    Last edited by passel; Dec 15th, 2018 at 10:19 PM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Text one char at a time

    Quote Originally Posted by passel View Post
    I think he wants to see the characters show up over time, as if someone is typing, otherwise, why copy the characters one at a time.
    I agree. I didn't address the UI freezing issue because it wasn't raised in the question and it may actually be that that is desirable. If you don't freeze the UI in this case, the user might actually edit one or other of the TextBoxes as the characters were being transferred. I figured I'd let the OP raise that as a separate issue if it became one, but it doesn't hurt for them to be aware of the potential issues and solutions.

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

    Re: Text one char at a time

    I guess you're right. I was presuming that if you had a sleep in the loop, that the second textbox would not update so was anticipating a different problem. But I just commented out the "Textbox2.Refresh" that I added, and the textbox updated anyway. I'm not sure I understand why that is the case, but the original code, with the correction for appending text, does show the characters being added one by one, which I didn't expect.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Text one char at a time

    Quote Originally Posted by passel View Post
    I was presuming that if you had a sleep in the loop, that the second textbox would not update so was anticipating a different problem. But I just commented out the "Textbox2.Refresh" that I added, and the textbox updated anyway. I'm not sure I understand why that is the case
    It appears to be the case that AppendText forces a repaint because I just tested using '.Text &=' and it did not display anything until the end of the loop while AppendText did. That's another point in favour of AppendText that I wasn't aware of.

  8. #8

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Re: Text one char at a time

    Thanks to all who contributed to this thread.
    OptionBase1, Not sure how that helped? but thanks for the input

    John
    Don't try to get fancy with the name of the loop counter in a For loop.
    Thanks for the brilliant little tutorial. Which I will use in future.

    Passel,
    I think he wants to see the characters show up over time, as if someone is typing, otherwise, why copy the characters one at a time.
    This is not the reason i want to show each letter one at a time, it was just for me to make sure that it was only reading one at a time, this is the base for what I want to do next, which starts to get complicated (at least for me it will be)
    But thanks for the example and clear explanation.

    John and Passel,
    That's another point in favour of AppendText that I wasn't aware of.
    Surprising what simple question can reveal.

    This is all I really need, the Thread.Sleep(250) was just for me to make sure I was only reading one character at a time.
    Code:
            For i = 0 To TextBox1.Text.Length - 1
                TextBox2.AppendText(TextBox1.Text.Chars(i))
                'Thread.Sleep(250)
            Next
    Last edited by Mallard8; Dec 16th, 2018 at 04:33 AM.
    Learning is a never ending subject.

  9. #9
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Text one char at a time

    Quote Originally Posted by Mallard8 View Post
    OptionBase1, Not sure how that helped? but thanks for the input
    Your code, while "obscured" inside of a For-Loop, was doing exactly the same thing as the three sequential lines of code I posted as an example. Each line was replacing the contents of Text2.Text rather than adding to it, just like the code you initially posted was doing. My intent was to get you to recognize that without just coming out and giving you the code to fix your problem. Glad you got it working with other help.

  10. #10
    Lively Member
    Join Date
    Apr 2009
    Posts
    73

    Re: Text one char at a time

    Code:
    Imports System.Threading
    Public Class Form1
        dim Counter as Long = 0
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
                if Counter >= TextBox1.Length then Counter = 0 'reached end start from beginning
                TextBox2.Text = TextBox1.Text.Chars(Counter)
                Counter += 1
        End Sub
    End Class
    I thought you wanted something like this everytime you press the button it gives you the next letter in the textbox1

  11. #11

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Re: Text one char at a time

    OptionBase1,
    I did realise what was happening with my code and what you were pointing out, it was how I changed it to work one character at a time that I couldn't grasp, but anyone who replies to a thread deserves a thank you for offering their help.

    pkedpker,
    Thanks for the code example but as I have said it was only a test for something else I want to develop.
    Learning is a never ending subject.

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Text one char at a time

    Quote Originally Posted by Mallard8 View Post
    OptionBase1,
    I did realise what was happening with my code and what you were pointing out, it was how I changed it to work one character at a time that I couldn't grasp, but anyone who replies to a thread deserves a thank you for offering their help.

    pkedpker,
    Thanks for the code example but as I have said it was only a test for something else I want to develop.
    I think it is post #4 where passel shows the

    For Each c As Char In somestring

    construct. That would be a good way to do this.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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

    Re: Text one char at a time

    Based on jmcilhinney's suggestion.

  14. #14
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Text one char at a time

    Quote Originally Posted by passel View Post
    Based on jmcilhinney's suggestion.
    OIC. Sorry JMC
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Text one char at a time

    Quote Originally Posted by dbasnett View Post
    OIC. Sorry JMC
    I never gets credit for nothin'.

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