Results 1 to 4 of 4

Thread: Tab pages reading txt to list box ?

  1. #1

    Thread Starter
    Lively Member polecat's Avatar
    Join Date
    Jul 2005
    Location
    Wolverhampton
    Posts
    83

    Resolved Tab pages reading txt to list box ?

    Hi forgive me im still new to vb.net and progging!! I have my app using tabpages thought it was easier for my project,
    on tab 1 i have 2 text boxes with and output from other text boxes
    I have a button to save to file this works great with snippets from you guys on here .

    Now on tab2 i have a listbox the list box is populated from the saved text file works ok apart from the saved items from tab1 dont appear till the program is next run? I have hidden button3 as i have put the TabControl1.Enter there to trigger the button action as i wanted the list to load on clicking the tab not a button if you know what i mean?
    The code im using is below if any body can help with away of it kind refreshing the file before populating thre list?

    VB Code:
    1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, TabControl1.Enter
    2.  
    3.         ' Load the ListBox from a file.
    4.         Try
    5.  
    6.             Dim file_name As String = DataFile()
    7.             Dim stream_reader As New IO.StreamReader(Application.StartupPath & "\mysaved.txt")
    8.  
    9.             ListBox1.Items.AddRange(Split(stream_reader.ReadToEnd, _
    10.                 vbCrLf))
    11.             ListBox1.SelectedIndex = 0
    12.             stream_reader.Close()
    13.         Catch exc As Exception
    14.             ' Report all errors.
    15.             MsgBox(exc.Message, MsgBoxStyle.Exclamation, "Read " & _
    16.                 "Error")
    17.         End Try
    18.  
    19.     End Sub
    20.  
    21.     ' Return the data file name.
    22.     Private Function DataFile() As String
    23.         Dim file_name As String = Application.StartupPath
    24.  
    25.         If file_name.EndsWith("\bin") Then file_name = _
    26.             file_name.Remove(file_name.Length - 4, 4)
    27.         file_name &= "\mysaved.txt"
    28.         Return file_name
    29.     End Function
    Last edited by polecat; May 22nd, 2006 at 03:16 PM. Reason: Resolved

  2. #2

    Thread Starter
    Lively Member polecat's Avatar
    Join Date
    Jul 2005
    Location
    Wolverhampton
    Posts
    83

    Re: Tab pages reading txt to list box ?

    Let me clarify a bit right tab page 1 saves to text file on tab3 it reads the file into my list box but it only shows items in the text file that were there before the prog is run not items saved while the prog is run so to view the saved items from tab1 i have to close the prog and restart it...Any help or way round it?

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Tab pages reading txt to list box ?

    You might want to clear your items first before loading them again into the listbox...
    VB Code:
    1. .....
    2.             .....
    3.             Dim file_name As String = DataFile()
    4.             Dim stream_reader As New IO.StreamReader(Application.StartupPath & "\mysaved.txt")
    5.             ListBox1.Items.Clear() 'add this line to clear the listbox before you add items to it again
    6.             ListBox1.Items.AddRange(Split(stream_reader.ReadToEnd, _
    7.                 vbCrLf))
    8.            .....
    9.            .....
    Could just be that they are adding, but adding to the bottom of the listbox and you dont see them, since it retains the current items as well...

  4. #4

    Thread Starter
    Lively Member polecat's Avatar
    Join Date
    Jul 2005
    Location
    Wolverhampton
    Posts
    83

    Re: Tab pages reading txt to list box ?

    Thanks but that still didnt work m8 but thanks

    Have added new button to the tab page labled it refresh and added your code to its onclick event and it works
    VB Code:
    1. Dim file_name As String = DataFile()
    2.             Dim stream_reader As New IO.StreamReader(Application.StartupPath & "\mysaved.txt")
    3.             ListBox1.Items.Clear() 'add this line to clear the listbox before you add items to it again
    4.             ListBox1.Items.AddRange(Split(stream_reader.ReadToEnd, _
    5.                 vbCrLf))

    So its a problem with tab click event so need to find that!
    Last edited by polecat; Nov 3rd, 2005 at 06:00 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width