Solved

TextBox Problems

Posted on 2004-08-25
20
186 Views
Last Modified: 2010-05-02
Right everytime i change the contents of a text box (which has a vertical scrollbar and multiline=true) the Caret and scrollbar reset to go back to the top of the text box.  

Its driving me nuts how can you stop this or work round it?
0
Comment
Question by:gerrymcd
  • 10
  • 7
  • 2
  • +1
20 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
Try adding the line:

    txtYourTextBox.SelStart = Len(txtYourTextBox.Text)

This should reposition the cursor to the end of the text and scroll it to the right position.

Hope this helps.
0
 
LVL 3

Expert Comment

by:nichia
Comment Utility
Try this to add new lines to your multiline textbox and keep the caret at the bottom:

With Text1
    .SelStart = Len(.Text)
    .SelText = vbCrLf & "New Text"
End With
0
 
LVL 2

Author Comment

by:gerrymcd
Comment Utility
No luck with any of that.
0
 
LVL 3

Expert Comment

by:nichia
Comment Utility
It's not an issue of luck.  Please post your code, and we'll see what is wrong.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Can you show us how you "change the contents of a text box" in your code?

Idle_Mind
0
 
LVL 2

Author Comment

by:gerrymcd
Comment Utility
Its quite a complicated thing im trying to do.
Bascially theres lines of lyrics in the texbox.  I click on a line and add a timestamp to the beginning of it.

So;
I'm a cold heartbreaker
and you can take the pity so farfit ta burn
and I'll rip your heart in two
and I'll leave you lyin on the bed

Becomes;
[01:13]I'm a cold heartbreaker
and you can take the pity so farfit ta burn
and I'll rip your heart in two
and I'll leave you lyin on the bed

Its the part above thats really causing problems.  Trying to append the "[01:13]" to a textbox line is tricky.  I had used the replace function but replaced all occurances and not the one line.

So i went for an array and just copied the contents of the the array into the textbox.  But this causes the textbox scroll bar to scroll up to the top.

i know theres a sendmessage function to do it at least i think.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
>> I click on a line and add a timestamp to the beginning of it.

So as soon as you click on the line you need a value inserted at the beginning of that line?

Idle_Mind
0
 
LVL 2

Author Comment

by:gerrymcd
Comment Utility
Yes.  Its as awkward as hell.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Does every line have a vbCrLf after it? ... or is it a mixutre of word wrapping and vbCrLf's?

Idle_Mind
0
 
LVL 2

Author Comment

by:gerrymcd
Comment Utility
It should have just vbCrLf
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 2

Author Comment

by:gerrymcd
Comment Utility
I think the best thing i can do is change the text box to a listbox.

On each click i can easily adda new timestamp and if anything needs to edited just past the lot into a text box?  sounds right or better?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Option Explicit

Private Sub Form_Load()
    Dim lyrics As String
   
    lyrics = "I'm a cold heartbreaker" & vbCrLf & _
            "and you can take the pity so farfit ta burn" & vbCrLf & _
            "and I'll rip your heart in two" & vbCrLf & _
            "and I'll leave you lyin on the bed"

    Text1.Text = lyrics
End Sub

Private Sub Text1_Click()
    Dim timeStamp As String
    Dim endOfLastLine As Integer
   
    timeStamp = "[" & Now() & "] "
    endOfLastLine = InStrRev(Text1.Text, vbCrLf, Text1.SelStart)
    If endOfLastLine > 0 Then
        If Mid(Text1.Text, endOfLastLine + 2, 1) <> "[" Then
            Text1.Text = Left(Text1.Text, endOfLastLine + 1) & timeStamp & Mid(Text1.Text, endOfLastLine + 2)
        End If
    Else
        If Left(Text1.Text, 1) <> "[" Then
            Text1.Text = timeStamp & Text1.Text
        End If
    End If
    Text1.SelStart = Len(Text1.Text)
End Sub
0
 
LVL 2

Author Comment

by:gerrymcd
Comment Utility
that looks good Idle_Mind, thanks for your help im off to try that out or i might use a listbox instead.  Ill give you the points later.

Thanks all.
0
 
LVL 2

Author Comment

by:gerrymcd
Comment Utility
Idle_Mind while that works for 4 lines of lyrics make the lyrics more than 4 lines and it wont work.  The scroll bar goes crazy.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 50 total points
Comment Utility
This doesn't make the scrollbars go crazy:

Option Explicit

Private Sub Form_Load()
    Dim lyrics As String
   
    lyrics = "I'm a cold heartbreaker" & vbCrLf & _
            "and you can take the pity so farfit ta burn" & vbCrLf & _
            "and I'll rip your heart in two" & vbCrLf & _
            "and I'll leave you lyin on the bed" & vbCrLf & _
            "I'm a cold heartbreaker" & vbCrLf & _
            "and you can take the pity so farfit ta burn" & vbCrLf & _
            "and I'll rip your heart in two" & vbCrLf & _
            "and I'll leave you lyin on the bed" & vbCrLf & _
            "I'm a cold heartbreaker" & vbCrLf & _
            "and you can take the pity so farfit ta burn" & vbCrLf & _
            "and I'll rip your heart in two" & vbCrLf & _
            "and I'll leave you lyin on the bed" & vbCrLf & _
            "I'm a cold heartbreaker" & vbCrLf & _
            "and you can take the pity so farfit ta burn" & vbCrLf & _
            "and I'll rip your heart in two" & vbCrLf & _
            "and I'll leave you lyin on the bed"

    Text1.Text = lyrics
End Sub

Private Sub Text1_Click()
    Dim timeStamp As String
    Dim endOfLastLine As Integer
       
    timeStamp = "[" & Now() & "] "
    endOfLastLine = InStrRev(Text1.Text, vbCrLf, Text1.SelStart)
    Text1.SelLength = 0
    If endOfLastLine > 0 Then
        If Mid(Text1.Text, endOfLastLine + 2, 1) <> "[" Then
            Text1.SelStart = endOfLastLine + 1
            Text1.SelText = timeStamp
        End If
    Else
        If Left(Text1.Text, 1) <> "[" Then
            Text1.SelStart = 0
            Text1.SelText = timeStamp
        End If
    End If
End Sub
0
 
LVL 2

Author Comment

by:gerrymcd
Comment Utility
Thanks for that it worked.
0
 
LVL 2

Author Comment

by:gerrymcd
Comment Utility
Sorry theres a problem if the line already has a time stamp can you modify your code, please
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Could you elaborate on "theres a problem if the line already has a time stamp" for me?

It works fine on my system.  If there is a stamp already on the line, it doesn't add another one.  It assumes the stamp begins with "[".

Idle_Mind
0
 
LVL 2

Author Comment

by:gerrymcd
Comment Utility
Sorry no i fixed it no probs, thanks alot for your help!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
=)
Idle_Mind
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now