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.
Public Class frmCustomerStatistic
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
Friend WithEvents mnuFileOpen As System.Windows.Forms.MenuItem
Friend WithEvents mnuFileSave As System.Windows.Forms.MenuItem
Friend WithEvents mnuFileExit As System.Windows.Forms.MenuItem
Friend WithEvents lstCustomerStatistic As System.Windows.Forms.ListBox
Friend WithEvents txtCustomerNumber As System.Windows.Forms.TextBox
Friend WithEvents lblDate As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents btnAddToList As System.Windows.Forms.Button
Friend WithEvents btnClearEntry As System.Windows.Forms.Button
Friend WithEvents btnEnterCustomerNumber As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents dlgSave As System.Windows.Forms.SaveFileDialog
Friend WithEvents dlgOpen As System.Windows.Forms.OpenFileDialog
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnAddToList = New System.Windows.Forms.Button
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.mnuFile = New System.Windows.Forms.MenuItem
Me.mnuFileOpen = New System.Windows.Forms.MenuItem
Me.mnuFileSave = New System.Windows.Forms.MenuItem
Me.MenuItem3 = New System.Windows.Forms.MenuItem
Me.MenuItem4 = New System.Windows.Forms.MenuItem
Me.mnuFileExit = New System.Windows.Forms.MenuItem
Me.dlgSave = New System.Windows.Forms.SaveFileDialog
Me.dlgOpen = New System.Windows.Forms.OpenFileDialog
Me.lstCustomerStatistic = New System.Windows.Forms.ListBox
Me.txtCustomerNumber = New System.Windows.Forms.TextBox
Me.lblDate = New System.Windows.Forms.Label
Me.btnClearEntry = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.btnEnterCustomerNumber = New System.Windows.Forms.Button
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'btnAddToList
'
Me.btnAddToList.Enabled = False
Me.btnAddToList.Location = New System.Drawing.Point(8, 192)
Me.btnAddToList.Name = "btnAddToList"
Me.btnAddToList.Size = New System.Drawing.Size(128, 32)
Me.btnAddToList.TabIndex = 0
Me.btnAddToList.Text = "&Add To List"
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile})
'
'mnuFile
'
Me.mnuFile.Index = 0
Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFileOpen, Me.mnuFileSave, Me.MenuItem3, Me.MenuItem4, Me.mnuFileExit})
Me.mnuFile.Text = "&File"
'
'mnuFileOpen
'
Me.mnuFileOpen.Index = 0
Me.mnuFileOpen.Text = "&Open"
'
'mnuFileSave
'
Me.mnuFileSave.Enabled = False
Me.mnuFileSave.Index = 1
Me.mnuFileSave.Text = "&Save"
'
'MenuItem3
'
Me.MenuItem3.Index = 2
Me.MenuItem3.Text = "-"
'
'MenuItem4
'
Me.MenuItem4.Index = 3
Me.MenuItem4.Text = ""
'
'mnuFileExit
'
Me.mnuFileExit.Index = 4
Me.mnuFileExit.Text = "&Exit"
'
'dlgSave
'
Me.dlgSave.DefaultExt = "txt"
Me.dlgSave.Filter = "Text Files(*.txt)|*.txt"
Me.dlgSave.Title = "Save File"
'
'dlgOpen
'
Me.dlgOpen.Filter = "Text Files(*.txt)|*.txt"
Me.dlgOpen.Title = "Open File"
'
'lstCustomerStatistic
'
Me.lstCustomerStatistic.Location = New System.Drawing.Point(144, 136)
Me.lstCustomerStatistic.Name = "lstCustomerStatistic"
Me.lstCustomerStatistic.Size = New System.Drawing.Size(256, 212)
Me.lstCustomerStatistic.TabIndex = 2
'
'txtCustomerNumber
'
Me.txtCustomerNumber.Location = New System.Drawing.Point(144, 64)
Me.txtCustomerNumber.Name = "txtCustomerNumber"
Me.txtCustomerNumber.Size = New System.Drawing.Size(256, 20)
Me.txtCustomerNumber.TabIndex = 3
Me.txtCustomerNumber.Text = ""
'
'lblDate
'
Me.lblDate.BackColor = System.Drawing.Color.White
Me.lblDate.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.lblDate.Location = New System.Drawing.Point(144, 16)
Me.lblDate.Name = "lblDate"
Me.lblDate.Size = New System.Drawing.Size(256, 23)
Me.lblDate.TabIndex = 4
Me.lblDate.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'btnClearEntry
'
Me.btnClearEntry.Enabled = False
Me.btnClearEntry.Location = New System.Drawing.Point(8, 248)
Me.btnClearEntry.Name = "btnClearEntry"
Me.btnClearEntry.Size = New System.Drawing.Size(128, 32)
Me.btnClearEntry.TabIndex = 5
Me.btnClearEntry.Text = "&Clear Entry"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 16)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 6
Me.Label1.Text = "Date:"
'
'btnEnterCustomerNumber
'
Me.btnEnterCustomerNumber.Location = New System.Drawing.Point(8, 136)
Me.btnEnterCustomerNumber.Name = "btnEnterCustomerNumber"
Me.btnEnterCustomerNumber.Size = New System.Drawing.Size(128, 32)
Me.btnEnterCustomerNumber.TabIndex = 7
Me.btnEnterCustomerNumber.Text = "&Enter Customer Number"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(144, 104)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(64, 23)
Me.Label2.TabIndex = 8
Me.Label2.Text = "Date:"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.BottomLeft
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(264, 104)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(104, 23)
Me.Label3.TabIndex = 9
Me.Label3.Text = "Customer served:"
Me.Label3.TextAlign = System.Drawing.ContentAlignment.BottomLeft
'
'frmCustomerStatistic
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(410, 361)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.btnEnterCustomerNumber)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.btnClearEntry)
Me.Controls.Add(Me.lblDate)
Me.Controls.Add(Me.txtCustomerNumber)
Me.Controls.Add(Me.lstCustomerStatistic)
Me.Controls.Add(Me.btnAddToList)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.Menu = Me.MainMenu1
Me.Name = "frmCustomerStatistic"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Customer Statistic"
Me.ResumeLayout(False)
End Sub
#End Region
Dim noCustomerRecords As Integer
Dim arrData(364) As String
Dim arrDate(364) As String
Dim i As Integer
Private Sub frmCustomerStatistic_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblDate.Text = Format(Now, "ddd dd MMMM yyyy".ToString)
btnEnterCustomerNumber.Focus()
btnAddToList.Enabled = False
End Sub
Private Sub txtCustomerNumber_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCustomerNumber.KeyPress
Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)
'only allow numbers, backspace or enter
Select Case KeyAscii
Case Asc("0") To Asc("9"), Asc(ControlChars.Back)
'acceptable keystrokes
e.Handled = False
btnAddToList.Enabled = True
Case Asc(ControlChars.Cr)
'enter key or click on Add To List Button
btnAddToList.Enabled = False
Case Else
e.Handled = True
MsgBox("Please enter only Customer numbers!", , "Error")
txtCustomerNumber.Focus()
btnAddToList.Enabled = False
End Select
End Sub
Private Sub btnEnterCustomerNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterCustomerNumber.Click
btnEnterCustomerNumber.Enabled = True
txtCustomerNumber.Focus()
End Sub
Private Sub btnAddToList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddToList.Click
'Add Date and Customer number to the List
lstCustomerStatistic.Items.Add((lblDate.Text) + (" ") + (txtCustomerNumber.Text))
mnuFileSave.Enabled = True
btnClearEntry.Enabled = True
'test if customer entry is already made
If lstCustomerStatistic.Items.Count = +1 Then
btnAddToList.Enabled = False
End If
End Sub
Private Sub btnClearEntry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearEntry.Click
Dim Ind As Integer
'Get Index
Ind = lstCustomerStatistic.SelectedIndex
'Make sure list item is selected
If Ind >= 0 Then
'Remove it from list box
lstCustomerStatistic.Items.Remove(Ind)
End If
If btnClearEntry.Enabled = True Then
btnAddToList.Enabled = False
lstCustomerStatistic.Items.Clear()
'Clear text box
txtCustomerNumber.Text = ""
'Show message to enter data
MsgBox("Please click the Enter Customer Number button!", , "Enter Data")
End If
End Sub
Private Sub lstCustomerStatistic_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCustomerStatistic.SelectedIndexChanged
End Sub
Private Sub mnuFileSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFileSave.Click
Try
'code to save file
Catch ex1 As Data.ReadOnlyException
MsgBox("Please change the Read only property!", , "Error")
Catch ex2 As Exception
MsgBox("Error while saving your Data!", , "Contact your Administrator")
End Try
'Save Data
If dlgSave.ShowDialog() = DialogResult.OK Then
FileOpen(1, dlgSave.FileName, OpenMode.Output)
FileClose(1)
End If
End Sub
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