• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

Can't Edit A Record

Hello,
I have 3 textbox in the edit.vb and a button called save,
the textbox are filled with the info from a ini file
Name;Last;Phone.

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

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()
        sr.Close()
        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
        Next
        If FoundRec Then
            Dim sw As New StreamWriter("C:\Accounts.ini", False)
            input = String.Join(ControlChars.CrLf, Lines)
            sw.WriteLine(input)
            sw.Close()

            ' 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.BeginUpdate()
            frmMain.lstAccounts.DataSource = Nothing
            frmMain.lstAccounts.Items.Clear()
            frmMain.lstAccounts.DisplayMember = "IntName"
            frmMain.lstAccounts.DataSource = frmMain.arrPersons
            frmMain.lstAccounts.EndUpdate()

        End If

        frmMain.Enabled = True
        frmMain.Activate()
        Me.Close()

    End Sub
0
aplelois
Asked:
aplelois
1 Solution
 
tsayCommented:
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?
0
 
apleloisAuthor Commented:
Bob1;Anderson1;123456
Bob2;Anderson2;236548
0
 
Fernando SotoCommented:
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.Populate((lstAccounts.SelectedIndex))
        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)
        Me.InitializeComponent()
        EditPerson = Person
        Populate()
    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)
        Next
        txtName.Text = Split(strLine, ";")(0)
        txtLast.Text = Split(strLine, ";")(1)
        txtPhone.Text = Split(strLine, ";")(2)
        FileClose(FileNo)

    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.

Fernando
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now