blah blah blah is here! blah blah » Close

up0down
link

Hi i want to edit the selected item which is inserted in my listview. I want the other Form (Form2) to appear when before editing with information of the item in each textbox

Form1 = listview and the items

Form2 = the textboxes

Here is some code

Form1

private void MainForm_Load(object sender, EventArgs e)
{

ListViewItem listviewitem;
listView1.LabelEdit = true;

// Ensure that the view is set to show details.
listView1.View = View.Details;

// Create column headers for sorting the listview data.
ColumnHeader IDHeader, FirstNameHeader, LastNameHeader, HomePhoneHeader, CellPhoneHeader, CountryHeader, ZipCodeHeader, CityHeader, StreetHeader, EmailHeader;
IDHeader = new ColumnHeader();
FirstNameHeader = new ColumnHeader();
LastNameHeader = new ColumnHeader();
HomePhoneHeader = new ColumnHeader();
CellPhoneHeader = new ColumnHeader();
CountryHeader = new ColumnHeader();
ZipCodeHeader = new ColumnHeader();
CityHeader = new ColumnHeader();
StreetHeader = new ColumnHeader();
EmailHeader = new ColumnHeader();

IDHeader.Text = "ID";
this.listView1.Columns.Add(IDHeader);
IDHeader.Width = 50;

FirstNameHeader.Text = "First name";
this.listView1.Columns.Add(FirstNameHeader);
FirstNameHeader.Width = 110;

LastNameHeader.Text = "Last name";
this.listView1.Columns.Add(LastNameHeader);
LastNameHeader.Width = 110;

HomePhoneHeader.Text = "Home phone";
this.listView1.Columns.Add(HomePhoneHeader);
HomePhoneHeader.Width = 120;

CellPhoneHeader.Text = "Cell phone";
this.listView1.Columns.Add(CellPhoneHeader);
CellPhoneHeader.Width = 110;

CountryHeader.Text = "Country";
this.listView1.Columns.Add(CountryHeader);
CountryHeader.Width = 110;

ZipCodeHeader.Text = "Zip-code";
this.listView1.Columns.Add(ZipCodeHeader);
ZipCodeHeader.Width = 100;

CityHeader.Text = "City";
this.listView1.Columns.Add(CityHeader);
CityHeader.Width = 110;

StreetHeader.Text = "Street";
this.listView1.Columns.Add(StreetHeader);
StreetHeader.Width = 110;

EmailHeader.Text = "Email";
this.listView1.Columns.Add(EmailHeader);
EmailHeader.Width = 150;

using(var customerframe = new CustomerFrame())
{
//if button OK is clicked then value will be inserted
if (customerframe.ShowDialog() == DialogResult.OK)
{
listView1.Items.Clear();

CustomerFiles.Contact contact = customerframe.GetContact();
CustomerFiles.Address address = customerframe.GetAddress();
CustomerFiles.Phone phone = customerframe.GetPhone();
CustomerFiles.Email email = customerframe.GetEmail();

//Items in my listview
listviewitem = new ListViewItem();
listviewitem.SubItems.Add(contact.FirstName);
listviewitem.SubItems.Add(contact.LastName);
listviewitem.SubItems.Add(phone.Home);
listviewitem.SubItems.Add(phone.Mobile);
listviewitem.SubItems.Add(address.Country);
listviewitem.SubItems.Add(address.ZipCode);
listviewitem.SubItems.Add(address.City);
listviewitem.SubItems.Add(address.Street);
listviewitem.SubItems.Add(email.Personal);

this.listView1.Items.Add(listviewitem);
}
}
}

Form2

internal CustomerFiles.Contact GetContact()
{
CustomerFiles.Contact contact = new CustomerFiles.Contact();
contact.FirstName = tbFirstName.Text;
contact.LastName = tbLastName.Text;

return contact;

}
internal CustomerFiles.Address GetAddress()
{
address.City = tbCity.Text;
address.Street = tbStreet.Text;
address.ZipCode = tbZipCode.Text;
address.country = cbCountry.Text;

return address;
}
internal CustomerFiles.Phone GetPhone()
{
CustomerFiles.Phone phone = new CustomerFiles.Phone();
phone.Home = tbHomePhone.Text;
phone.Mobile = tbCellPhone.Text;

return phone;

}
internal CustomerFiles.Email GetEmail()
{
CustomerFiles.Email email = new CustomerFiles.Email();
email.Personal = tbEmail.Text;

return email;
}

last answered one month ago

1 answers

up0down
link

The simple answer to your problem is to create a populating method in Form 2 to put any data you want to appear into the form. You also add a delegate and an event to fire upon adding the new information which will return any data you changed into the record you were editing. I won't write code for your homework but the pseudocode would be:

Form 1

Create Form2 variable

instattiate form2 variable
form2.event += method to update record;
form2.show


method to update record(var 1, var 2, var 3, etc.)
{
record.var = var1;
etc.

form2.event -= method to update record;
}


Form 2
delegate with structure that includes all variables to update

event of type of above delegate

when button apply is clicked
{
event(var 1, var 2, var 3, etc.)
}

Feedback