Solved

TextBox Problems

Posted on 2004-08-25
20
193 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
ID: 11895194
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
ID: 11895920
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
ID: 11896269
No luck with any of that.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 3

Expert Comment

by:nichia
ID: 11896356
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
ID: 11896364
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
ID: 11896442
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
ID: 11896534
>> 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
ID: 11896556
Yes.  Its as awkward as hell.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11896610
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
ID: 11896626
It should have just vbCrLf
0
 
LVL 2

Author Comment

by:gerrymcd
ID: 11896672
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
ID: 11896740
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
ID: 11896819
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
ID: 11897099
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
ID: 11897535
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
ID: 11897912
Thanks for that it worked.
0
 
LVL 2

Author Comment

by:gerrymcd
ID: 11898319
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
ID: 11898429
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
ID: 11898433
Sorry no i fixed it no probs, thanks alot for your help!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11898436
=)
Idle_Mind
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
change vba from autofit to 13.5 width? 4 29
Put text in a picture ASP.NET C# 2 60
Passing a Text Box name to a Sub 6 99
Send outlook email from VBS Script 2 42
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

808 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