VS 2010 listbox max value and save button question-VBForums
Results 1 to 4 of 4

Thread: listbox max value and save button question

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2012
    Posts
    11

    Smile listbox max value and save button question

    Hi there. I managed to create a button which shows the total number of the items of a listbox.
    Code:
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
            MessageBox.Show(listBox.Items.Count.ToString())
    End Sub
    However the "listBox.Items.Count.ToString() doesn't work outside of the button. The questions are:

    1. How to make a label which will autoupdate the total number of the items of a listbox every 1 minute? I am guessing a timer will be needed and something like
    Code:
            Dim counter As Integer = 0
    
            lblTotal.Text = ""
            For counter = 0 To listCustomers.Items.Count - 1
            lblTotal.Text &= listCustomers.Items.Count.MaxValue
            'Hm this must be converted to integer ^^ ?
            Next
    2. How to create a save button which will save the items of a listbox without sql stuff.

    Thanks in advance.

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,399

    Re: listbox max value and save button question

    Quote Originally Posted by tehProxy View Post
    1. How to make a label which will autoupdate the total number of the items of a listbox every 1 minute? I am guessing a timer will be needed and something like
    Add timer to your form, set Interval to 1000 and Enabled to True, use the following code
    vb Code:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2.         Me.Text = ListBox1.Items.Count
    3.     End Sub

    Quote Originally Posted by tehProxy View Post
    2. How to create a save button which will save the items of a listbox without sql stuff.
    To save to text file, use this code
    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim strT As New System.Text.StringBuilder
    3.         For Each i In ListBox1.Items
    4.             strT.Append(i & Environment.NewLine)
    5.         Next
    6.         IO.File.WriteAllText("g:\listbox.txt", strT.ToString)
    7.     End Sub



  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2012
    Posts
    11

    Re: listbox max value and save button question

    Thank you 4x2y. The timer worked perfectly!

    However I encountered the following error for the save button: "Access to the path 'C:\listbox.txt' is denied."

    Is there any way to save the items of the listbox without sql or .txt inside the form with MySettings or something? And if this is not possible is there a way to load the .txt?

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,399

    Re: listbox max value and save button question

    C:\ is protected by windows, you have to save in different location e.g. C:\test\listbox.txt.

    It is better to save data in the Listbox in a separate file not in MySetting.

    To load from a file use,
    vb Code:
    1. ListBox1.DataSource = IO.File.ReadAllLines(My.Application.Info.DirectoryPath & "\listbox.txt")



Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.