Click here to Skip to main content
15,886,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The form allows me to register customers and save their relevant details into a text file, once I save customers I display this information into a list box. My main aim is now to be able to amend information relative to a customer I have already saved therefore when I select one customer's data from the list box these details are put into numbered text boxes (txtBox1 which is for the customer name and txtBox2 for customer date of birth). I have tried using the code below in (btnUpdate_Click) to only change the parts of data that are amended and avoiding any repetition but it does not seem to work, does anyone got any suggestions? Thank you.

What I have tried:

Dim strAmendedRecord As String = ""
        For N As Integer = 1 To 5
            Dim txtBoxN As TextBox = Me.Controls.Find("txtBox" & N, True).FirstOrDefault
            If Not IsNothing(txtBoxN) Then
                strAmendedRecord += txtBoxN.Text & (",")
            End If

        Next
        MsgBox(strAmendedRecord)
       
        ListBox1.SelectedIndex = ListBox1.SelectedIndex - 1
        ListBox1.Items.RemoveAt(ListBox1.SelectedIndex - 1)
        ListBox1.Items.Add(ListBox1.SelectedIndex + 1)



        Dim data As String = ""
        For Each item In ListBox1.Items
            data &= item.ToString() & vbNewLine

        Next
Posted
Updated 7-Feb-18 23:38pm

1 solution

You have 2 different problems :

1st:
If you want to enter a new customer to your List you enter your data into the textboxes.
After that you should have a Button which either must save this data to your file and also modify your Listbox.
Perhaps you should also check if the "new" entered data is still existing inside your file.

2nd:
You select one of your Listbox-entries. In this case you get the Listbox-Event SelectedIndexChanged. Depending on this Event you load the relevant data from your file and display it in your textboxes.

Nothing of this I can find in your code-snippet ... :(
 
Share this answer
 
Comments
faiqaa 8-Feb-18 6:50am    
Yes I am using a submit button, this is hat I have used for Listbox-Event SelectedIndexChanged:
Dim curItem As String = ListBox1.SelectedItem.ToString()
Dim parts As String() = curItem.Split(New String(){","}, StringSplitOptions.RemoveEmptyEntries)

For i As Integer = 0 To parts.Count()-1
Dim txt As TextBox = DirectCast(Me.Controls("txtBox" & i+1), System.Windows.Forms.TextBox)
txt.Text = parts(i)
Next
I don't know what you mean by "Perhaps you should also check if the "new" entered data is still existing inside your file."
Ralf Meier 8-Feb-18 7:04am    
If you use the Listbox-Event SelectedIndexChanged you have selected one of your still existing entries and want to transfer it to your textboxes. Here you don't need a "Submit"-Button. The Event is raised when the Selection inside the Listbox changes (as the Name of the Event tells).
This is what I have described under the point "2nd"

But (perhaps I understood your requirement wrong) you also want to add new data to your list. In this case (I suppose) you enter the new data into your textboxes and press "Submit" when the input is completed. In this case you should use the Click-Event from the Button and transfer the data to your list. Now ... I would check if this "new entered data" is really new ... or with other words I would check if this data still exists in your list to prevent double entries ...
faiqaa 8-Feb-18 10:40am    
Hi, I'll try explaining everything in detail as I think you did not understand what I'm meant to do, I am using three buttons( SUBMIT,AMEND and DISPLAY) , I use the same numbered text boxes to register a customer(customer name,date of birth..) and then press the SUBMIT button, the relevant data is saved into a text file. I later press the DISPLAY button which basically means that the data from the text file is read and displayed in a list box. Now my problem is not being able to "Amend" the data I've already saved, for example I would need this in case a customer's address changes therefore instead of adding a new customer I would only need to change this customer's address. For this reason whenever I select a customer from the list box, the row of data (customer name,date of birth,address) are split and copied into the relevant text boxes, for example the customer name would be automatically copied into the txtBox1 and the date of birth in txtBox2. I have only decided to use this copy function so that I would be able to amend but I'm having issues with it, I have tried using the code that you can see above but it's not helping. Thank you for your time.
Ralf Meier 9-Feb-18 2:01am    
Hi,
I'm sure I understood your issue complete. The last part you desribed is the same as what I meant with "you should also check if the "new" entered data is still existing inside your file."
Here are 2 possibilities :
1st: you have data entered in your textboxes which is allready existing in your List - and you press "Submit". In this case (without a check) you get double entries. So you should generally iterate through your list and compare the new data-block with each existing one. If you get a match at a point you should NOT add the "new" data to your list ...

2nd: you want to change parts of your data - here you have to decide which part of your data is the "Index". I would suggest you take the customer-name. So if you press "Amend" (or "Change") you have to look if there is an entry with the same customer-name. If Yes you replace this entry with your new data-block - but: what if you really have 2 customer-entries which have the same name but really different addresses ? What do you want to do now ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900