none
How to enter data from a structured array and store data in a textfile RRS feed

  • Question

  • Hello my name is thabang

    I have a problem with storing or caprture my array structure

    Public Class frmProduct
    
        Structure Products
            Public strCode As String
            Public strName As String
            Public strDescription As String
            Public strDateofDelivery As String
            Public strStockType As String
            Public intQuantity As Integer
            Public decPrice As Decimal
        End Structure
        Public strCode As String
        Public strName As String
        Public strDescription As String
        Public strDateofDelivery As String
        Public strStockType As String
        Public intQuantity As Integer
        Public decPrice As Decimal
    
    
        Public strArrProducts() As Products
        Public intArrayIndex, intCountIndex, intRemoveIndex As Integer
        Public blnValidProcessing As Boolean = False
    
        Public stdList As String = "{0,-25}{1,-25}{2,-25}{3,-25}{4,-20}{5,-10}{6,-10}"
        Public stdDisplay As String = "{0,-25}{1,-27}{2,-27}{3,-27}{4,-25}{5,-10}{6,-10}"
    
        Private Sub Display()
    
            'Display the information on the listbox
    
            lstDispayProducts.Items.Add("================================ Welcome ==============================")
            lstDispayProducts.Items.Add("")
            lstDispayProducts.Items.Add("====================== Main Products Added =============================")
            lstDispayProducts.Items.Add("")
            lstDispayProducts.Items.Add(String.Format(stdList, "Poduct code", "Product Name", "Product Description", "Date of Deliverty", "Stock type", "Quantity", "Price"))
            lstDispayProducts.Items.Add("")
            For x As Integer = 0 To intArrayIndex
                lstDispayProducts.Items.Add(String.Format(stdDisplay, strArrProducts(x).strCode, _
                                                          strArrProducts(x).strName, _
                                                          strArrProducts(x).strDescription, _
                                                          strArrProducts(x).strDateofDelivery, _
                                                          strArrProducts(x).strStockType, _
                                                          strArrProducts(x).intQuantity.ToString, _
                                                          strArrProducts(x).decPrice.ToString("C2")))
            Next
    
            lstDispayProducts.Items.Add("")
            lstDispayProducts.Items.Add("Average of Stock in store:                " & CalcAverageStock(intArrayIndex))
            lstDispayProducts.Items.Add("")
            lstDispayProducts.Items.Add("Total Amount for Delivered stock:        " & CalcTotalAmount())
    
    
    
        End Sub
        Private Sub AddNewProduct()
    
            Dim Dialogs As Integer
            Dialogs = MessageBox.Show("Are you sure you want to enter a new product", "Incredible Connection", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            'if te person wants to exit
            If Dialogs = DialogResult.Yes Then
                TabControl1.SelectedIndex = 0
                Exit Sub
            End If
    
    
        End Sub
    
        Private Function CalcAverageStock(ByVal intArrayIndex As Integer) As Integer
            Dim intAverageTotal As Integer
            Dim intTotalQuantity As Integer
    
            For x As Integer = 0 To intArrayIndex
                intTotalQuantity = intTotalQuantity + strArrProducts(x).intQuantity
            Next
    
            intAverageTotal = intTotalQuantity / intArrayIndex
            Return intAverageTotal
    
    
        End Function
    
        Private Function CalcTotalAmount() As Decimal
            'Declare a Variable for total amount
            Dim decTotalAmount As Decimal
    
            For x As Integer = 0 To intArrayIndex
                decTotalAmount = decTotalAmount + strArrProducts(x).decPrice
            Next
    
            Return decTotalAmount
    
        End Function
    
    
        Private Sub SearchProduct()
    
            'Declaring the variables for the search items
            Dim strCodeSearch As String
            Dim blnFoundSearch As Boolean = False
    
            'assign the variable fo search
            strCodeSearch = txtSearch.Text
    
            'Display elements
            lstDispayProducts.Items.Add("=============================Searching==============================")
            lstDispayProducts.Items.Add("")
            lstDispayProducts.Items.Add(String.Format(stdList, "Poduct code", "Product Name", "Product Description", "Date of Deliverty", "Stock type", "Quantity", "Price"))
            lstDispayProducts.Items.Add("")
    
            'Creating a For loop to display the item
    
            For i As Integer = 0 To intArrayIndex
                'if the code macthes the coe entered
                If strArrProducts(i).strCode = strCodeSearch Then
                    blnFoundSearch = True
                    lstDispayProducts.Items.Add(String.Format(stdDisplay, strArrProducts(i).strCode, _
                                                              strArrProducts(i).strName, _
                                                              strArrProducts(i).strDescription, _
                                                              strArrProducts(i).strDateofDelivery, _
                                                              strArrProducts(i).strStockType, _
                                                              strArrProducts(i).intQuantity.ToString, _
                                                              strArrProducts(i).decPrice.ToString("C2")))
    
    
                    Exit For
                End If
            Next i
    
            'Display messages whether the product is found or not Found
            If blnFoundSearch = True Then
                MessageBox.Show("The product " & strCodeSearch & " is Captured", "Search", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
            Else
                MessageBox.Show("The product " & strCodeSearch & " is not Captured", "Searsh Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                GrpEnterDetailsMain.Enabled = True
                btnDisplay.Enabled = False
            End If
    
            'clear and reset the cursere on the search textbox
            txtSearch.Clear()
            txtSearch.Focus()
    
    
    
        End Sub
    
        Private Sub closeprogram()
    
            'Declaring Dialog
            Dim Dialogs As Integer
            Dialogs = MessageBox.Show("Are you sure you want to exit", "Incredible Connection", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            'if te person wants to exit
            If Dialogs = DialogResult.Yes Then
                Close()
            End If
    
    
        End Sub
    
    
        Private Sub frmProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim InProducts As System.IO.StreamReader
            Dim strOnecompleteLine As String
            Dim intPosition As Integer
            If System.IO.File.Exists("E:\PCP ISAT 2019 Thabang\Products.txt") Then
                InProducts = System.IO.File.OpenText("E:\PCP ISAT 2019 Thabang\Products.txt")
                strOnecompleteLine = InProducts.ReadLine
                Do While InProducts.Peek <> -1
                    ReDim Preserve strArrProducts(intArrayIndex)
                    intPosition = strOnecompleteLine.IndexOf("*")
                    strArrProducts(intArrayIndex).strCode = _
                    strOnecompleteLine.Substring(0, intPosition)
                    strOnecompleteLine = strOnecompleteLine.Remove(0, intPosition + 1)
    
                    intPosition = strOnecompleteLine.IndexOf("*")
                    strArrProducts(intArrayIndex).strName = _
                    strOnecompleteLine.Substring(0, intPosition)
                    strOnecompleteLine = strOnecompleteLine.Remove(0, intPosition + 1)
    
                    intPosition = strOnecompleteLine.IndexOf("*")
                    strArrProducts(intArrayIndex).strDescription = _
                    strOnecompleteLine.Substring(0, intPosition)
                    strOnecompleteLine = strOnecompleteLine.Remove(0, intPosition + 1)
    
    
                    intPosition = strOnecompleteLine.IndexOf("*")
                    strArrProducts(intArrayIndex).strDateofDelivery = _
                    strOnecompleteLine.Substring(0, 1)
    
                    strOnecompleteLine = strOnecompleteLine.Remove(0, intPosition + 1)
    
                    intPosition = strOnecompleteLine.IndexOf("*")
                    strArrProducts(intArrayIndex).strStockType = _
                    strOnecompleteLine.Substring(0, 1)
    
                    strOnecompleteLine = strOnecompleteLine.Remove(0, intPosition + 1)
    
                    intPosition = strOnecompleteLine.IndexOf("*")
                    strArrProducts(intArrayIndex).intQuantity = _
                    CInt(strOnecompleteLine.Substring(0, intPosition))
                    strOnecompleteLine = strOnecompleteLine.Remove(0, intPosition + 1)
    
                    intPosition = strOnecompleteLine.IndexOf("*")
                    strArrProducts(intArrayIndex).decPrice = _
                    CDec(strOnecompleteLine.Substring(0, 1))
                    strOnecompleteLine = strOnecompleteLine.Remove(0, intPosition + 1)
    
    
                    strOnecompleteLine = InProducts.ReadLine
                    intArrayIndex += 1
                Loop
    
                intArrayIndex = intArrayIndex - 1
    
                InProducts.Close()
                MessageBox.Show("Array elements loaded")
                blnValidProcessing = True
            Else
    
                MessageBox.Show("Products file does not exit, create it first")
                blnValidProcessing = False
            End If
    
    
            btnDisplay.Enabled = False
    
        End Sub
    
        Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            If blnValidProcessing Then
                Dim OutEmpFile As System.IO.StreamWriter
                Dim strOutLine As String
                OutEmpFile = System.IO.File.CreateText("E:\PCP ISAT 2019 Thabang\Products.txt")
                For x As Integer = 0 To intArrayIndex
                    strOutLine = strArrProducts(x).strCode & "*" & _
                                 strArrProducts(x).strName & "*" & _
                                 strArrProducts(x).strDescription & "*" & _
                                 strArrProducts(x).strDateofDelivery & "*" & _
                                 strArrProducts(x).strStockType & "*" & _
                                 strArrProducts(x).intQuantity.ToString & "*" & _
                                 strArrProducts(x).decPrice.ToString("F2") & "*"
                    OutEmpFile.WriteLine(strOutLine)
                Next
                OutEmpFile.Close()
                MessageBox.Show("Thank you, all new products have added added")
            End If
        End Sub
    
    
    
        Private Sub validationInput(ByVal intArrayIndex As Integer)
    
    
    
    
    
        End Sub
    
        Private Sub btnEnterProduct_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterProduct.Click
    
            If blnValidProcessing Then
    
                'Show message Error when enering incorrect or blant textboxes
                'tf there is no text in the textbox
                If txtCode.Text = Nothing Then
                    Beep()
                    MessageBox.Show("Pease enter Product Code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    txtCode.Focus()
                    Exit Sub
                End If
    
                If txtName.Text = Nothing Then
                    Beep()
                    MessageBox.Show("Please enter the Product name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    txtName.Focus()
                    Exit Sub
                End If
    
                'if the combo box has selected the 1st index
                If cmbDescription.SelectedIndex = Nothing Or cmbDescription.SelectedIndex = 0 Then
                    MessageBox.Show("Please Select the Products Description", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    cmbDescription.SelectedIndex = 0
                    Exit Sub
                End If
    
    
                If dtpDateofDelivery.Value = Nothing Then
                    MessageBox.Show("Please Select Date of delivery", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    cmbStockType.SelectedIndex = 0
                    Exit Sub
    
                End If
    
                'if the combo box has selected the 1st index
    
                If cmbStockType.SelectedIndex = Nothing Or cmbStockType.SelectedIndex = 0 Then
                    MessageBox.Show("Please Select the Type of stock", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    cmbStockType.SelectedIndex = 0
                    Exit Sub
                End If
    
                If txtQuantity.Text = Nothing Then
                    Beep()
                    MessageBox.Show("Pease Enter number of Quantity", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    txtCode.Focus()
                    Exit Sub
                End If
    
                If txtPrice.Text = Nothing Then
                    Beep()
                    MessageBox.Show("Pease enter Product Code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    txtCode.Focus()
                    Exit Sub
                End If
    
                strCode = txtCode.Text
                strName = txtName.Text
                strDescription = cmbDescription.SelectedItem
                strDateofDelivery = dtpDateofDelivery.Value
                strStockType = cmbStockType.SelectedItem
    
    
    
                'check if the text is in numbers
    
    
    
                If IsNumeric(txtQuantity.Text) Then
                    Integer.TryParse(intQuantity, txtQuantity.Text)
                Else
                    Beep()
                    MessageBox.Show("Please enter the correct Quantity", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    txtQuantity.Clear()
                    txtQuantity.Focus()
                    Exit Sub
                End If
    
                If IsNumeric(txtPrice.Text) Then
                    Decimal.TryParse(decPrice, txtPrice.Text)
                Else
                    Beep()
                    MessageBox.Show("Please enter the price of the Product", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    txtPrice.Clear()
                    txtPrice.Focus()
                    Exit Sub
                End If
    
                ReDim strArrProducts(intArrayIndex)
    
                strArrProducts(intArrayIndex).strCode = strCode
                strArrProducts(intArrayIndex).strName = strName
                strArrProducts(intArrayIndex).strDescription = strDescription
                strArrProducts(intArrayIndex).strDateofDelivery = strDateofDelivery
                strArrProducts(intArrayIndex).strStockType = strStockType
                strArrProducts(intArrayIndex).intQuantity = intQuantity
                strArrProducts(intArrayIndex).decPrice = decPrice
    
    
                intArrayIndex += 1
    
                txtCode.Clear()
                txtName.Clear()
                cmbDescription.SelectedIndex = 0
                dtpDateofDelivery.Format = DateTimePickerFormat.Short
                cmbStockType.SelectedIndex = 0
                txtQuantity.Clear()
                txtPrice.Clear()
                txtCode.Focus()
    
    
            End If
    
    
            btnDisplay.Enabled = True
    
    
        End Sub
    
        Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
    
    
            TabControl1.SelectedIndex = 1
    
            Display()
        End Sub
    
        Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
            Dim strNameRemove As String
    
            If blnValidProcessing = True Then
                'Test to see if the user has selected a member to be removed
                If lstDispayProducts.SelectedIndex <> -1 Then
                    strNameRemove = strArrProducts(intRemoveIndex).strName
                    For intNum As Integer = intRemoveIndex To intArrayIndex - 1
                        strArrProducts(intNum) = strArrProducts(intNum + 1)
                    Next
                    'Update the size of the array – an element has been removed
                    intArrayIndex = intArrayIndex - 1
                    ReDim Preserve strArrProducts(intArrayIndex)
                    MessageBox.Show("Thank you, " & strNameRemove & _
                    " has been removed ", " ", MessageBoxButtons.OK, _
                    MessageBoxIcon.Information)
    
                    lstDispayProducts.Items.Clear()
    
                    lstDispayProducts.Items.Add("================================ Welcome ==============================")
                    lstDispayProducts.Items.Add("")
                    lstDispayProducts.Items.Add("====================== Main Products Added =============================")
                    lstDispayProducts.Items.Add("")
                    lstDispayProducts.Items.Add(String.Format(stdList, "Poduct code", "Product Name", "Product Description", "Date of Deliverty", "Stock type", "Quantity", "Price"))
                    lstDispayProducts.Items.Add("")
    
                    For x As Integer = 0 To intArrayIndex
                        lstDispayProducts.Items.Add(String.Format(stdDisplay, strArrProducts(x).strCode, _
                                                              strArrProducts(x).strName, _
                                                              strArrProducts(x).strDescription, _
                                                              strArrProducts(x).strDateofDelivery, _
                                                              strArrProducts(x).strStockType, _
                                                              strArrProducts(x).intQuantity.ToString, _
                                                              strArrProducts(x).decPrice.ToString("C2")))
                    Next
                Else
                    MessageBox.Show("You must first select the Product that You wish to Remove", _
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
    
    
            End If
        End Sub
    
        Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
    
            AddNewProduct()
    
        End Sub
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    
            closeprogram()
    
        End Sub
    End Class
    

    Please help me out im stuck

    Tuesday, October 8, 2019 9:17 AM

All replies

  • Myself, I would use a List of custom objects and XML serialized the list of objects to a XML file, which can be deserialized back into a List of objects.

    http://vb.net-informations.com/collections/list.htm

    https://stackoverflow.com/questions/25924544/how-to-serialize-a-list-in-vb

    I would make the custom class using auto properties.

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/auto-implemented-properties

    As far as doing a search on a List or array, I would use Linq.

    https://www.dotnetperls.com/linq-vbnet

    https://www.codeproject.com/Articles/26652/Using-LINQ-to-Objects-in-Visual-Basic

    Tuesday, October 8, 2019 9:59 AM
  • Hello,

    Are you open to other ideas other than a structure? Seems that there is room for improvement both in how the data is stored along with how data is presented in a form.

    A class would be a better container and a ListView would be a better control rather than a ListBox.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, October 8, 2019 10:01 AM
    Moderator