?
Solved

TextBox Problems

Posted on 2004-08-25
20
Medium Priority
?
199 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 150 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

764 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