![]() |
Control scrolling in a TextBox
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 |
Control scrolling in a TextBox
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 "Revolvr" wrote in message ... 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 |
Control scrolling in a TextBox
On Sep 18, 9:41*am, "Nigel" wrote:
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 "Revolvr" wrote in message ... 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? |
Control scrolling in a TextBox
On Sep 18, 12:31*pm, Revolvr wrote:
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 |
Control scrolling in a TextBox
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 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 -- Rick (MVP - Excel) |
Control scrolling in a TextBox
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? |
Control scrolling in a TextBox
On Sep 18, 6:16*pm, Revolvr wrote:
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 |
Control scrolling in a TextBox
On Sep 19, 6:43*am, scattered wrote:
On Sep 18, 6:16*pm, Revolvr wrote: 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 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. |
All times are GMT +1. The time now is 04:11 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com