Solved

TextBox Problems

Posted on 2004-08-25
20
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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 86

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 86

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 86

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 86

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 86

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 86

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 86

Expert Comment

by:Mike Tomlinson
ID: 11898436
=)
Idle_Mind
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

729 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