Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted

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

  • Leaders
Posted
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.
[sIGPIC]e[/sIGPIC]
Posted

   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.

  • Leaders
Posted

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 

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...