Results 1 to 6 of 6

Thread: [RESOLVED] View end of text after new text

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    61

    Resolved [RESOLVED] View end of text after new text

    Hi there,
    I have a text on a textobox and append some new one by using
    TextBox1.Text = TextBox1.Text & vbNewLine & "new string"

    When i do that sometimes the new text exceeds the lines of the textbox so i need the scrollbars to look through all of it.
    The problem is that when i get new text the textbox automatically scrolls all the way up. I want the scrollbar to stay at the end so i can see the new text directly.
    How can i do that?
    NOTE: I am programmin on the compact framework, programmin for a pda.
    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: View end of text after new text

    Don't add text to a TextBox like that. Use its AppendText method, which will also sort out the issue you are having with scrolling.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: View end of text after new text

    It is also more preferable to use
    VB Code:
    1. Environment.NewLine
    instead of vbNewLine
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    61

    Re: View end of text after new text

    I am using the compact framework, programming for my pda so Append doesnt exist on the TextBox methods. Neither Enviroment.Newline exists.
    Any other ideas?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: View end of text after new text

    I haven't actually dealt with this in the CF, but I have a question, and maybe a suggestion.

    Am I correct in reading your original post to mean that you have the scrollbar, scroll to the end, add text, and the scrollbar automatically snaps back to the top?

    The CF is a weird place to work, and some of the solutions are not at all obvious. If the textbox default behavior is not to your liking, see whether or not there is a bizarre alternative using multiple controls. As an example, there is no multi-select listbox in the CF. I got around this by adding a panel with a scroll bar. On the panel, I added a bunch on small panels with labels on them. I then filled the labels from an array of strings, starting with the array index of the scroll bar value. While it was a weird way to do things, it worked great, and looked just like a multi-select listbox.

    In your case, it appears fairly obvious that when you add text, the textbox control is being set back to a default state, but what is that default state? If you were to implement a textbox with other types of controls, you might have a panel with a label on it (to hold the text), and a scrollbar on it. The difficulty would be in linking the scrollbar value to what text you start writing in the label. You could use the same technique that I did for the multi-select list box, as long as you can figure out the word wrap of the text yourself. This could be done by holding the string in a custom class that fed out the string in chunks of size x, where x is the length of the label, and where each chunk is terminated by a space, and is less than or equal to x.

    This solution could be a slow one to implement, but it would work. If labels wrap words themselves (they do don't they? I can't remember), then you would only need one label on the panel, and get the substring of the main string based on the width of the label times the value of the scrollbar (5 label widths would be the sixth line down, and if the scrollbar had the value of 0, you would start with main string.substring(0)).

    If nobody has a direct solution for your problem, I'd be looking at a goofy solution like that, with a panel, a label, and a scrollbar. If that failed, I'd be looking at a panel with multiple one-line panels with labels on them, a scrollbar, and a class in the background feeding lines to the labels.
    My usual boring signature: Nothing

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: View end of text after new text

    Ah, apologies. Given that there's a .NET Mobile Development forum it's generally better to post CF questions there, although you did specify you were using the CF so I should have noticed.

    In that case do this:
    VB Code:
    1. myTextBox.Text &= newText
    2. myTextBox.SelectionStart = myTextBox.Text.Length
    3. myTextBox.ScrollToCaret()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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