Can't Edit A Record

Posted on 2006-05-02
Last Modified: 2010-04-23
I have 3 textbox in the edit.vb and a button called save,
the textbox are filled with the info from a ini file

In main.vb I have a listbox that only shows the names, and edit button

so if I select Name1 I click edit then go to edit.vb and I see 3 textbox
Name: Name1
Last: last1
Phone: 123456

but im not able to edit the phone number with this code...
I want to be able to do this

Bob to Mike
Anderson to Piterson
123-123-123 to 456-456-456

can someone tell me why?

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click

        Dim sr As New StreamReader("C:\Accounts.ini")
        Dim input As String = sr.ReadToEnd()
        Dim FoundRec As Boolean = False
        input = Regex.Replace(input, "(\r\n)", "|")
        Dim Lines() As String = input.Split("|")
        Dim idx As Integer
        For idx = 0 To Lines.Length - 1
            Dim editline As String = Lines(idx)
            If Not editline = "" Then
                If editline.Substring(editline.LastIndexOf(";")+1).Trim = txtPhone.Text.Trim Then
                    Lines(idx) = txtName.Text.Trim & ";" & txtLast.Text.Trim & ";" & txtPhone.Text.Trim
                    FoundRec = True
                    Exit For
                End If
            End If
        If FoundRec Then
            Dim sw As New StreamWriter("C:\Accounts.ini", False)
            input = String.Join(ControlChars.CrLf, Lines)

            ' Get the record from the arrPersons ArrayList
            Dim p As frmMain.Persons = CType(frmMain.arrPersons(idx), frmMain.Persons)
            p.IntName = txtName.Text
            p.IntLast = txtLast.Text
            p.IntPhone = txtPhone.Text
            frmMain.arrPersons.Item(idx) = p
            frmMain.lstAccounts.DataSource = Nothing
            frmMain.lstAccounts.DisplayMember = "IntName"
            frmMain.lstAccounts.DataSource = frmMain.arrPersons

        End If

        frmMain.Enabled = True

    End Sub
Question by:aplelois
    LVL 5

    Expert Comment

    How do you mean, not able to edit phone nummer? Do you get an exception? Can you also post some example content from the ini file?

    Author Comment

    LVL 62

    Accepted Solution

    Hi aplelois;

    I made the following changes to your code.

    In frmMain
    Change this Sub from this:

        Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click
            Dim frmAD As frmEdit
            frmAD = New frmEdit
            frmAD.ShowDialog() ' This makes the new form modal so you don't need to disable the current form.
        End Sub

    To this:

        Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click
            Dim frmAD As New frmEdit(CType(arrPersons(lstAccounts.SelectedIndex), Persons))
            frmAD.ShowDialog() ' This makes the new form modal so you don't need to disable the current form.
        End Sub

    In frmEdit:

    After this line of code:

    Public Class frmEdit

    Add this code block:

        Private EditPerson As frmMain.Persons

        Public Sub New(ByVal Person As frmMain.Persons)
            EditPerson = Person
        End Sub

    Replace this code section:

        Public Sub Populate(ByVal Record As Integer)
            On Error Resume Next
            Dim FileNo As Integer
            Dim strLine As String = ""
            FileNo = FreeFile()
            FileOpen(FileNo, "C:\Accounts.ini", OpenMode.Input)
            Dim intRecord As Integer
            For intRecord = 0 To Record
                strLine = LineInput(FileNo)
            txtName.Text = Split(strLine, ";")(0)
            txtLast.Text = Split(strLine, ";")(1)
            txtPhone.Text = Split(strLine, ";")(2)

        End Sub

    With this code:

        Public Sub Populate()

            On Error Resume Next

            txtName.Text = EditPerson.IntName
            txtLast.Text = EditPerson.IntLast
            txtPhone.Text = EditPerson.IntPhone

        End Sub

    Replace this code:

                    'If editline.Substring(0, editline.IndexOf(";")).Trim = txtID.Text.Trim Then
                    If editline.Substring(editline.LastIndexOf(";") + 1).Trim = txtName.Text.Trim Then

    With this code:

                    If editline.Substring(0, editline.IndexOf(";")).Trim = EditPerson.IntName AndAlso _
                       editline.Substring(editline.LastIndexOf(";") + 1).Trim = EditPerson.IntPhone Then

    That is all the changes you need to do.


    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    This video discusses moving either the default database or any database to a new volume.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now