Solved

TextBox Problems

Posted on 2004-08-25
20
189 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

929 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

14 Experts available now in Live!

Get 1:1 Help Now