0

I have 2 forms, form1 and edit_form, and when the user selects an item in form1 to edit it, he is redirected to edit_form where he can make his changes and click apply. this is my code:

Form1 fs = new Form1();

        fs.update_list(serial_edit_txt.Text, Items_edit_box.SelectedItem.ToString(), quantity_edit_box.Text, units_edit_txt.Text, unit_edit_rate_txt.Text);

this code is from edit_form, im calling form1 and calling a method with the edited values

 public void update_list(string serial_edited, string item_box_edited, string quantity_edited, string units_edited, string units_rate_edited)
    {

        if (listView1.SelectedItems.Count > 0)
        {

            listView1.SelectedItems[0].SubItems[0].Text = serial_edited;
            listView1.SelectedItems[0].SubItems[1].Text = item_box_edited;
            listView1.SelectedItems[0].SubItems[2].Text = quantity_edited;
            listView1.SelectedItems[0].SubItems[3].Text = units_edited;
            listView1.SelectedItems[0].SubItems[4].Text = units_rate_edited;
        }
        else
        {

        }

    }

this is code from form1. the problem im facing is that the items arent being change, i suppose this is because there is no item selected, and im not sure how to go about changing the existing item's values

This is how the software looks

rizzla
  • 9
  • 3
  • This `Form1 fs = new Form1();` creates a new and completely different object. Not the one you're seeing and dealing with. Reconsider the design. keep the add/update/delete routines in the main Form. Pass and get the values to and from the input Form (edit_form), apply when the input Form returns `DialogResult.OK` to the caller. Think about it. – dr.null Oct 18 '21 at 08:32
  • @dr.null how can i pass the values i get from the user in edit_form to form1 without creating a new instance? because even if i do the editing process in the main form (form1) i would still need to pass the information through the edit_form (since thats where the user will enter the require details/edits) – rizzla Oct 18 '21 at 08:53
  • You need to create a new instance of the input Form not the main one. In the input Form, add public properties to set/get the input controls values. **OR**, add a parameterized constructor to pass the values and set the input controls, and add read-only properties to return the values from the input controls. Again, do not create a new instance of the **main** Form. – dr.null Oct 18 '21 at 13:48
  • [Passing variable between winforms](https://stackoverflow.com/questions/4247807/passing-variable-between-winforms). & [See also](https://stackoverflow.com/a/4588028/14171304). – dr.null Oct 18 '21 at 13:50
  • thanks! i got it to work @dr.null – rizzla Oct 19 '21 at 09:39
  • You're welcome and well done. – dr.null Oct 19 '21 at 09:54

0 Answers0