Jay1b Posted March 30, 2005 Posted March 30, 2005 I have a textbox, which contains about 50 lines in it. The textbox will display the content of a logfile, therefore the part which will be viewed the most is the very bottom of the textbox, and to save the user from scrolling manually, i would like the textbox to automatically scroll to the bottom of the textbox, rather then stay at the top as it does by standard. Is this possible at all? If so how can i do this? Thanks for any help. Quote
RedLeader Posted March 30, 2005 Posted March 30, 2005 Hi try this code textBox1.ScrollToCaret(); Quote
Jay1b Posted March 30, 2005 Author Posted March 30, 2005 Thanks, but unfortunately it doesnt work :( Quote
Leaders snarfblam Posted March 30, 2005 Leaders Posted March 30, 2005 TextBox1.SelectionStart = x.Text.Length TextBox1.ScrollToCaret() 'Not even sure if this line is necessary Quote [sIGPIC]e[/sIGPIC]
Jay1b Posted March 30, 2005 Author Posted March 30, 2005 Thank you that works for when the user is manually changing the records they wish to view, but unfortunately it doesnt seem to work for the text that is displayed by default. Private Sub lvLog_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvLog.SelectedIndexChanged Dim item As ListViewItem For Each item In lvLog.SelectedItems 'Will only ever be one Dim sr As New StreamReader(item.Text) Dim strLogLine As String = sr.ReadToEnd txtlog.text = strLogLine sr.Close() 'Close StreamReader txtLog.SelectionStart = txtLog.Text.Length txtLog.ScrollToCaret() Next End Sub I have a listview displaying the filename of various logfiles, when one of these is selected the logfile is displayed in the textbox. When the frame first loads, the logfile at the top of the list is displayed, it is this one that doesnt scroll to the bottom of the record. If the user selects another one manually, it displays fine - also if the user scrolls down a logfile, then back upto the first logfile it scrolls to the bottom like it should. Thanks Quote
Leaders snarfblam Posted March 30, 2005 Leaders Posted March 30, 2005 In your initialization code when the first file's data is loaded and displayed in the textbox, just insert the code I provided above. Any time you change the text in the textbox and want to scroll to the bottom just insert that code after the textboxname.Text = string. Quote [sIGPIC]e[/sIGPIC]
Jay1b Posted March 31, 2005 Author Posted March 31, 2005 Private Sub frmLog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Create a reference to the current running directory. Dim DirBase As New DirectoryInfo(StartupPath) ' Create an array representing the files in the current directory. Dim fi As FileInfo() = DirBase.GetFiles(GetExecutingAssembly.GetName.Name & "*.log") ' Print out the names of the files in the current directory. Dim fiTemp As FileInfo For Each fiTemp In fi Dim item As New ListViewItem(fiTemp.Name) 'item.SubItems.Add(fiTemp.LastWriteTime) lvLog.Items.Add(item) Next lvLog.Focus() lvLog.Items(0).Selected = True txtLog.SelectionStart = txtLog.Text.Length txtLog.ScrollToCaret() End Sub Private Sub lvLog_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvLog.SelectedIndexChanged Dim item As ListViewItem For Each item In lvLog.SelectedItems 'Will only ever be one Dim sr As New StreamReader(item.Text) Dim strLogLine As String = sr.ReadToEnd txtlog.text = strLogLine sr.Close() 'Close StreamReader Next txtLog.SelectionStart = txtLog.Text.Length txtLog.ScrollToCaret() End Sub The form has a listview (lvLog) and a multiline textbox (txtLog). I cant see anywhere else i can insert your code, so i've pasted the code above. When the frame first loads txtLog is filled with the logfile, but not scrolled to the bottom of the textbox, but when you move down a logfile and then come back to it, it is scrolled to the bottom like it should be. Thanks again. Quote
Leaders snarfblam Posted March 31, 2005 Leaders Posted March 31, 2005 I copied and tried your code. It seems that the problem is that you are scrolling to the end of the text before the textbox is even visible. All you need to do is put a call to Me.Show() before you set the selected item. Private Sub frmLog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Create a reference to the current running directory. Dim DirBase As New DirectoryInfo(StartupPath) ' Create an array representing the files in the current directory. Dim fi As FileInfo() = DirBase.GetFiles(GetExecutingAssembly.GetName.Name & "*.log") ' Print out the names of the files in the current directory. Dim fiTemp As FileInfo For Each fiTemp In fi Dim item As New ListViewItem(fiTemp.Name) 'item.SubItems.Add(fiTemp.LastWriteTime ) lvLog.Items.Add(item) Next Me.Show() '<-------- lvLog.Focus() lvLog.Items(0).Selected = True End Sub Private Sub lvLog_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvLog.SelectedIndexChanged Dim item As ListViewItem For Each item In lvLog.SelectedItems 'Will only ever be one Dim sr As New StreamReader(item.Text) Dim strLogLine As String = sr.ReadToEnd txtlog.text = strLogLine sr.Close() 'Close StreamReader Next txtLog.SelectionStart = txtLog.Text.Length txtLog.ScrollToCaret() End Sub Quote [sIGPIC]e[/sIGPIC]
Jay1b Posted April 1, 2005 Author Posted April 1, 2005 Excellent, thank you very much for your help. That may explain a few other problems i've had in the past actually. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.