-
Jul 9th, 2012, 09:03 PM
#1
Thread Starter
New Member
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.
-
Jul 10th, 2012, 01:26 AM
#2
Re: listbox max value and save button question
Originally Posted by tehProxy
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:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Text = ListBox1.Items.Count
End Sub
Originally Posted by tehProxy
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strT As New System.Text.StringBuilder
For Each i In ListBox1.Items
strT.Append(i & Environment.NewLine)
Next
IO.File.WriteAllText("g:\listbox.txt", strT.ToString)
End Sub
-
Jul 10th, 2012, 07:05 AM
#3
Thread Starter
New Member
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?
-
Jul 11th, 2012, 03:48 AM
#4
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|