I am wondering how I can update my listview in form1 by entering data via textboxes in form2. My code works fine if i put all the text boxes on the same form etc.
I figured I needed some reference to the first form on 2nd but can’t get it working.
Any tips for putting me in the right direction would be nice, also any tips for any better way of doing this.
This is the code I have so far:
Form1:
public partial class form1 : Form
{
public form1()
{
InitializeComponent();
}
public ListView MyListView
{
get
{
return taskList;
}
}
Form2:
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
form1 f;
public add(form1 f)
{
this.f = f;
}
public void AddToList()
{
ListViewItem item1 = new ListViewItem(txtName.Text);
item1.SubItems.Add(txtEmail.Text);
item1.SubItems.Add(txtPhone.Text);
f.MyListView.Items.AddRange(new ListViewItem[] { item1 });
}
The most straight forward way of doing things would be to use events. You could add an event on
form2that would fire each time an item is added, and includes the text to be inserted (you have multiple pieces of information, so a custom data type would be appropriate). You can then add a handler method toform2which adds the item to its ListView. You then tie the two together in the code that is creating the two forms, and life should be good.So, to provide some code, First up is the data structure for the event:
Then we have the event code on
form2And now we can add a handler in
form1And last but not least we need to tie them together