Control scrolling in a TextBox

R

Revolvr

I am using a TextBox with a vertical scrollbar as a message area where
messages are accumulated over time as the user does things. Each
message is appended to the previous with a chr(10) in VBA. This keeps
a history of user changes available. Multiline is true.

Problem is, the TextBox seems by default to show the top, or first,
row of information rather than the bottom, most recent information. So
lines are added, but it isn’t visible to the user. Only when I click
in the box it switches to the last line.

What I want is to always be showing the last, most recent line, all
the time, unless the user scrolls up.

Is it possible to create that behavior somehow?

Thanks
 
N

Nigel

Add the new text into the box by appending at the top?

TextBox.Value = "New Message" & vbCrLf & TextBox.Value

instead of

TextBox.Value = TextBox.Value & vbCrLf & "New Message"

--
Regards,
Nigel
(e-mail address removed)




I am using a TextBox with a vertical scrollbar as a message area where
messages are accumulated over time as the user does things. Each
message is appended to the previous with a chr(10) in VBA. This keeps
a history of user changes available. Multiline is true.

Problem is, the TextBox seems by default to show the top, or first,
row of information rather than the bottom, most recent information. So
lines are added, but it isn’t visible to the user. Only when I click
in the box it switches to the last line.

What I want is to always be showing the last, most recent line, all
the time, unless the user scrolls up.

Is it possible to create that behavior somehow?

Thanks
 
R

Revolvr

Add the new text into the box by appending at the top?

TextBox.Value = "New Message" & vbCrLf & TextBox.Value

instead of

TextBox.Value =  TextBox.Value & vbCrLf & "New Message"

--
Regards,
Nigel
(e-mail address removed)



I am using a TextBox with a vertical scrollbar as a message area where
messages are accumulated over time as the user does things. Each
message is appended to the previous with a chr(10) in VBA. This keeps
a history of user changes available. Multiline is true.

Problem is, the TextBox seems by default to show the top, or first,
row of information rather than the bottom, most recent information. So
lines are added, but it isn’t visible to the user. Only when I click
in the box it switches to the last line.

What I want is to always be showing the last, most recent line, all
the time, unless the user scrolls up.

Is it possible to create that behavior somehow?

Thanks


I can try this. But what I am seeing is the scroll bar seems to
position itself neither at the top, nor at the bottom. It seems to
park a few lines from the bottom. Is there direct control over where
it scrolls to?
 
S

scattered

I am using a TextBox with a vertical scrollbar as a message area where
messages are accumulated over time as the user does things. Each
message is appended to the previous with a chr(10) in VBA. This keeps
a history of user changes available. Multiline is true.

Problem is, the TextBox seems by default to show the top, or first,
row of information rather than the bottom, most recent information. So
lines are added, but it isn’t visible to the user. Only when I click
in the box it switches to the last line.

What I want is to always be showing the last, most recent line, all
the time, unless the user scrolls up.

Is it possible to create that behavior somehow?

Thanks

Maybe the following code snippet will help:

Private Sub btnPress_Click()
Dim s As String
s = InputBox("Enter message")
TextBox1.Text = TextBox1.Text & vbCrLf & s
TextBox1.SetFocus
TextBox1.CurLine = TextBox1.LineCount - 1
btnPress.SetFocus
End Sub


-scattered
 
R

Rick Rothstein

I am using a TextBox with a vertical scrollbar as a message area where
Maybe the following code snippet will help:

Private Sub btnPress_Click()
Dim s As String
s = InputBox("Enter message")
TextBox1.Text = TextBox1.Text & vbCrLf & s
TextBox1.SetFocus
TextBox1.CurLine = TextBox1.LineCount - 1
btnPress.SetFocus
End Sub

I would suggest you issue this statement immediately after the CurLine
assignment so the caret (text cursor) is in the more normal position at the
end of the newly added text...

TextBox1.SelStart = Len(TextBox1.Text)

I would also note the this section of the (now modified) code...

.....
.....
TextBox1.SetFocus
TextBox1.CurLine = TextBox1.LineCount - 1
TextBox1.SelStart = Len(TextBox1.Text)
btnPress.SetFocus

could be replaced with this...

.....
.....
TextBox1.SetFocus
SendKeys "^{END}"
btnPress.SetFocus
 
R

Revolvr

Thanks for the help so far, but something isn't right.

When I use TextBox1.SelStart = Len(TextBox1.Text)
I get a compile error "Invalid use of property"
I cannot get the LineCount because it says control needs to have focus
first, however, when I use TextBox1.SetFocus I get a run time error
that says "Object doesn't support this property or method".

If it helps, this is Excel 2003.

Any ideas?
 
S

scattered

Thanks for the help so far, but something isn't right.

When I use TextBox1.SelStart = Len(TextBox1.Text)
I get a compile error "Invalid use of property"
I cannot get the LineCount because it says control needs to have focus
first, however, when I use TextBox1.SetFocus I get a run time error
that says "Object doesn't support this property or method".

If it helps, this is Excel 2003.

Any ideas?

The problem is that embedded text boxes work differently from those on
forms. For the embedded ones, "Activate" evidently plays the role of
SetFocus.

In any event, as an experiment I used the control toolbox to create an
embedded textbox and an embedded button which I renamed btnPress with
the following click event:


Private Sub btnPress_Click()
Dim s As String
s = InputBox("Enter message")
If TextBox1.Text = "" Then
TextBox1.Text = s
Else
TextBox1.Text = TextBox1.Text & vbCrLf & s
End If
TextBox1.Activate
TextBox1.CurLine = TextBox1.LineCount - 1
TextBox1.SelStart = Len(TextBox1.Text)
End Sub

(This incorporates Rick's excellent suggestion which leaves the cursor
at the end of the input - you can activate or select something else
afterwards of course)

This seems to work - just keep hitting button press and adding data.
The bottom row is always visible

hth

-scattered
 
R

Revolvr

The problem is that embedded text boxes work differently from those on
forms. For the embedded ones, "Activate" evidently plays the role of
SetFocus.

In any event, as an experiment I used the control toolbox to create an
embedded textbox and an embedded button which I renamed btnPress with
the following click event:

Private Sub btnPress_Click()
    Dim s As String
    s = InputBox("Enter message")
    If TextBox1.Text = "" Then
        TextBox1.Text = s
    Else
        TextBox1.Text = TextBox1.Text & vbCrLf & s
    End If
    TextBox1.Activate
    TextBox1.CurLine = TextBox1.LineCount - 1
    TextBox1.SelStart = Len(TextBox1.Text)
End Sub

(This incorporates Rick's excellent suggestion which leaves the cursor
at the end of the input - you can activate or select something else
afterwards of course)

This seems to work - just keep hitting button press and adding data.
The bottom row is always visible

hth

-scattered


That's it! Of course, Instead of using SetFocus to set focus, use
Activate, if it's an embedded text box. I would not have guessed. That
solved it. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top