944,006 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 444
  • VB.NET RSS
Jul 30th, 2008
0

Help needed for how to store lstbox with 2 arrays

Expand Post »
Hi everyone!
First i am a beginner so please dont be too angry if i make mistakes, I am willing to learn. Thanks.
My problem: I am learning VB.Net 2003, I am creating a program to save daily customer numbers with the date(now). I attach code! I have problem to sae the lstCustomerStatistic to a txt.file, and later read it agin in for further use.
VB.NET Syntax (Toggle Plain Text)
  1. Public Class frmCustomerStatistic
  2. Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6. Public Sub New()
  7. MyBase.New()
  8.  
  9. 'This call is required by the Windows Form Designer.
  10. InitializeComponent()
  11.  
  12. 'Add any initialization after the InitializeComponent() call
  13.  
  14. End Sub
  15.  
  16. 'Form overrides dispose to clean up the component list.
  17. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18. If disposing Then
  19. If Not (components Is Nothing) Then
  20. components.Dispose()
  21. End If
  22. End If
  23. MyBase.Dispose(disposing)
  24. End Sub
  25.  
  26. 'Required by the Windows Form Designer
  27. Private components As System.ComponentModel.IContainer
  28.  
  29. 'NOTE: The following procedure is required by the Windows Form Designer
  30. 'It can be modified using the Windows Form Designer.
  31. 'Do not modify it using the code editor.
  32. Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
  33. Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
  34. Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
  35. Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
  36. Friend WithEvents mnuFileOpen As System.Windows.Forms.MenuItem
  37. Friend WithEvents mnuFileSave As System.Windows.Forms.MenuItem
  38. Friend WithEvents mnuFileExit As System.Windows.Forms.MenuItem
  39. Friend WithEvents lstCustomerStatistic As System.Windows.Forms.ListBox
  40. Friend WithEvents txtCustomerNumber As System.Windows.Forms.TextBox
  41. Friend WithEvents lblDate As System.Windows.Forms.Label
  42. Friend WithEvents Label1 As System.Windows.Forms.Label
  43. Friend WithEvents btnAddToList As System.Windows.Forms.Button
  44. Friend WithEvents btnClearEntry As System.Windows.Forms.Button
  45. Friend WithEvents btnEnterCustomerNumber As System.Windows.Forms.Button
  46. Friend WithEvents Label2 As System.Windows.Forms.Label
  47. Friend WithEvents Label3 As System.Windows.Forms.Label
  48. Friend WithEvents dlgSave As System.Windows.Forms.SaveFileDialog
  49. Friend WithEvents dlgOpen As System.Windows.Forms.OpenFileDialog
  50. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  51. Me.btnAddToList = New System.Windows.Forms.Button
  52. Me.MainMenu1 = New System.Windows.Forms.MainMenu
  53. Me.mnuFile = New System.Windows.Forms.MenuItem
  54. Me.mnuFileOpen = New System.Windows.Forms.MenuItem
  55. Me.mnuFileSave = New System.Windows.Forms.MenuItem
  56. Me.MenuItem3 = New System.Windows.Forms.MenuItem
  57. Me.MenuItem4 = New System.Windows.Forms.MenuItem
  58. Me.mnuFileExit = New System.Windows.Forms.MenuItem
  59. Me.dlgSave = New System.Windows.Forms.SaveFileDialog
  60. Me.dlgOpen = New System.Windows.Forms.OpenFileDialog
  61. Me.lstCustomerStatistic = New System.Windows.Forms.ListBox
  62. Me.txtCustomerNumber = New System.Windows.Forms.TextBox
  63. Me.lblDate = New System.Windows.Forms.Label
  64. Me.btnClearEntry = New System.Windows.Forms.Button
  65. Me.Label1 = New System.Windows.Forms.Label
  66. Me.btnEnterCustomerNumber = New System.Windows.Forms.Button
  67. Me.Label2 = New System.Windows.Forms.Label
  68. Me.Label3 = New System.Windows.Forms.Label
  69. Me.SuspendLayout()
  70. '
  71. 'btnAddToList
  72. '
  73. Me.btnAddToList.Enabled = False
  74. Me.btnAddToList.Location = New System.Drawing.Point(8, 192)
  75. Me.btnAddToList.Name = "btnAddToList"
  76. Me.btnAddToList.Size = New System.Drawing.Size(128, 32)
  77. Me.btnAddToList.TabIndex = 0
  78. Me.btnAddToList.Text = "&Add To List"
  79. '
  80. 'MainMenu1
  81. '
  82. Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile})
  83. '
  84. 'mnuFile
  85. '
  86. Me.mnuFile.Index = 0
  87. Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFileOpen, Me.mnuFileSave, Me.MenuItem3, Me.MenuItem4, Me.mnuFileExit})
  88. Me.mnuFile.Text = "&File"
  89. '
  90. 'mnuFileOpen
  91. '
  92. Me.mnuFileOpen.Index = 0
  93. Me.mnuFileOpen.Text = "&Open"
  94. '
  95. 'mnuFileSave
  96. '
  97. Me.mnuFileSave.Enabled = False
  98. Me.mnuFileSave.Index = 1
  99. Me.mnuFileSave.Text = "&Save"
  100. '
  101. 'MenuItem3
  102. '
  103. Me.MenuItem3.Index = 2
  104. Me.MenuItem3.Text = "-"
  105. '
  106. 'MenuItem4
  107. '
  108. Me.MenuItem4.Index = 3
  109. Me.MenuItem4.Text = ""
  110. '
  111. 'mnuFileExit
  112. '
  113. Me.mnuFileExit.Index = 4
  114. Me.mnuFileExit.Text = "&Exit"
  115. '
  116. 'dlgSave
  117. '
  118. Me.dlgSave.DefaultExt = "txt"
  119. Me.dlgSave.Filter = "Text Files(*.txt)|*.txt"
  120. Me.dlgSave.Title = "Save File"
  121. '
  122. 'dlgOpen
  123. '
  124. Me.dlgOpen.Filter = "Text Files(*.txt)|*.txt"
  125. Me.dlgOpen.Title = "Open File"
  126. '
  127. 'lstCustomerStatistic
  128. '
  129. Me.lstCustomerStatistic.Location = New System.Drawing.Point(144, 136)
  130. Me.lstCustomerStatistic.Name = "lstCustomerStatistic"
  131. Me.lstCustomerStatistic.Size = New System.Drawing.Size(256, 212)
  132. Me.lstCustomerStatistic.TabIndex = 2
  133. '
  134. 'txtCustomerNumber
  135. '
  136. Me.txtCustomerNumber.Location = New System.Drawing.Point(144, 64)
  137. Me.txtCustomerNumber.Name = "txtCustomerNumber"
  138. Me.txtCustomerNumber.Size = New System.Drawing.Size(256, 20)
  139. Me.txtCustomerNumber.TabIndex = 3
  140. Me.txtCustomerNumber.Text = ""
  141. '
  142. 'lblDate
  143. '
  144. Me.lblDate.BackColor = System.Drawing.Color.White
  145. Me.lblDate.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  146. Me.lblDate.Location = New System.Drawing.Point(144, 16)
  147. Me.lblDate.Name = "lblDate"
  148. Me.lblDate.Size = New System.Drawing.Size(256, 23)
  149. Me.lblDate.TabIndex = 4
  150. Me.lblDate.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
  151. '
  152. 'btnClearEntry
  153. '
  154. Me.btnClearEntry.Enabled = False
  155. Me.btnClearEntry.Location = New System.Drawing.Point(8, 248)
  156. Me.btnClearEntry.Name = "btnClearEntry"
  157. Me.btnClearEntry.Size = New System.Drawing.Size(128, 32)
  158. Me.btnClearEntry.TabIndex = 5
  159. Me.btnClearEntry.Text = "&Clear Entry"
  160. '
  161. 'Label1
  162. '
  163. Me.Label1.Location = New System.Drawing.Point(16, 16)
  164. Me.Label1.Name = "Label1"
  165. Me.Label1.TabIndex = 6
  166. Me.Label1.Text = "Date:"
  167. '
  168. 'btnEnterCustomerNumber
  169. '
  170. Me.btnEnterCustomerNumber.Location = New System.Drawing.Point(8, 136)
  171. Me.btnEnterCustomerNumber.Name = "btnEnterCustomerNumber"
  172. Me.btnEnterCustomerNumber.Size = New System.Drawing.Size(128, 32)
  173. Me.btnEnterCustomerNumber.TabIndex = 7
  174. Me.btnEnterCustomerNumber.Text = "&Enter Customer Number"
  175. '
  176. 'Label2
  177. '
  178. Me.Label2.Location = New System.Drawing.Point(144, 104)
  179. Me.Label2.Name = "Label2"
  180. Me.Label2.Size = New System.Drawing.Size(64, 23)
  181. Me.Label2.TabIndex = 8
  182. Me.Label2.Text = "Date:"
  183. Me.Label2.TextAlign = System.Drawing.ContentAlignment.BottomLeft
  184. '
  185. 'Label3
  186. '
  187. Me.Label3.Location = New System.Drawing.Point(264, 104)
  188. Me.Label3.Name = "Label3"
  189. Me.Label3.Size = New System.Drawing.Size(104, 23)
  190. Me.Label3.TabIndex = 9
  191. Me.Label3.Text = "Customer served:"
  192. Me.Label3.TextAlign = System.Drawing.ContentAlignment.BottomLeft
  193. '
  194. 'frmCustomerStatistic
  195. '
  196. Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
  197. Me.ClientSize = New System.Drawing.Size(410, 361)
  198. Me.Controls.Add(Me.Label3)
  199. Me.Controls.Add(Me.Label2)
  200. Me.Controls.Add(Me.btnEnterCustomerNumber)
  201. Me.Controls.Add(Me.Label1)
  202. Me.Controls.Add(Me.btnClearEntry)
  203. Me.Controls.Add(Me.lblDate)
  204. Me.Controls.Add(Me.txtCustomerNumber)
  205. Me.Controls.Add(Me.lstCustomerStatistic)
  206. Me.Controls.Add(Me.btnAddToList)
  207. Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
  208. Me.Menu = Me.MainMenu1
  209. Me.Name = "frmCustomerStatistic"
  210. Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
  211. Me.Text = "Customer Statistic"
  212. Me.ResumeLayout(False)
  213.  
  214. End Sub
  215.  
  216. #End Region
  217. Dim noCustomerRecords As Integer
  218. Dim arrData(364) As String
  219. Dim arrDate(364) As String
  220. Dim i As Integer
  221.  
  222.  
  223. Private Sub frmCustomerStatistic_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
  224. lblDate.Text = Format(Now, "ddd dd MMMM yyyy".ToString)
  225. btnEnterCustomerNumber.Focus()
  226. btnAddToList.Enabled = False
  227.  
  228.  
  229.  
  230. End Sub
  231.  
  232. Private Sub txtCustomerNumber_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCustomerNumber.KeyPress
  233. Dim KeyAscii As Integer
  234. KeyAscii = Asc(e.KeyChar)
  235. 'only allow numbers, backspace or enter
  236. Select Case KeyAscii
  237. Case Asc("0") To Asc("9"), Asc(ControlChars.Back)
  238. 'acceptable keystrokes
  239. e.Handled = False
  240. btnAddToList.Enabled = True
  241. Case Asc(ControlChars.Cr)
  242. 'enter key or click on Add To List Button
  243. btnAddToList.Enabled = False
  244. Case Else
  245. e.Handled = True
  246. MsgBox("Please enter only Customer numbers!", , "Error")
  247. txtCustomerNumber.Focus()
  248. btnAddToList.Enabled = False
  249.  
  250. End Select
  251.  
  252. End Sub
  253.  
  254. Private Sub btnEnterCustomerNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterCustomerNumber.Click
  255. btnEnterCustomerNumber.Enabled = True
  256. txtCustomerNumber.Focus()
  257.  
  258.  
  259. End Sub
  260.  
  261. Private Sub btnAddToList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddToList.Click
  262. 'Add Date and Customer number to the List
  263. lstCustomerStatistic.Items.Add((lblDate.Text) + (" ") + (txtCustomerNumber.Text))
  264. mnuFileSave.Enabled = True
  265. btnClearEntry.Enabled = True
  266. 'test if customer entry is already made
  267. If lstCustomerStatistic.Items.Count = +1 Then
  268. btnAddToList.Enabled = False
  269. End If
  270.  
  271.  
  272. End Sub
  273.  
  274. Private Sub btnClearEntry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearEntry.Click
  275. Dim Ind As Integer
  276. 'Get Index
  277. Ind = lstCustomerStatistic.SelectedIndex
  278. 'Make sure list item is selected
  279. If Ind >= 0 Then
  280. 'Remove it from list box
  281. lstCustomerStatistic.Items.Remove(Ind)
  282. End If
  283. If btnClearEntry.Enabled = True Then
  284. btnAddToList.Enabled = False
  285. lstCustomerStatistic.Items.Clear()
  286. 'Clear text box
  287. txtCustomerNumber.Text = ""
  288. 'Show message to enter data
  289. MsgBox("Please click the Enter Customer Number button!", , "Enter Data")
  290.  
  291. End If
  292. End Sub
  293.  
  294. Private Sub lstCustomerStatistic_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCustomerStatistic.SelectedIndexChanged
  295.  
  296.  
  297. End Sub
  298.  
  299. Private Sub mnuFileSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFileSave.Click
  300. Try
  301. 'code to save file
  302. Catch ex1 As Data.ReadOnlyException
  303. MsgBox("Please change the Read only property!", , "Error")
  304. Catch ex2 As Exception
  305. MsgBox("Error while saving your Data!", , "Contact your Administrator")
  306. End Try
  307.  
  308. 'Save Data
  309. If dlgSave.ShowDialog() = DialogResult.OK Then
  310. FileOpen(1, dlgSave.FileName, OpenMode.Output)
  311. FileClose(1)
  312. End If
  313. End Sub
  314. End Class
I try to solve this problem now for 2 weeks nearly, PLease give a push in the right direction
Thanks a lot
MIchael
Last edited by Ezzaral; Apr 11th, 2011 at 1:55 pm. Reason: Added code tags. Please use them to format any code that you post.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
michael0101 is offline Offline
3 posts
since Jul 2008
Apr 11th, 2011
0
Re: Help needed for how to store lstbox with 2 arrays
>>I have problem to sae the lstCustomerStatistic to a txt.file, and later read it agin in for further use.

I know this is a really old thread, but with a total of Views: 425 as of today, this link might help others to save and load a ListBox's items from a File.
how do I save a list in a listbox and it keep saved when I close the application
Reputation Points: 250
Solved Threads: 368
Nearly a Posting Virtuoso
codeorder is offline Offline
1,430 posts
since Aug 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC