This is an interesting question to me because: to create a solution you have to work-around some of the limits of the WinForms application model. In my experience, many newcomers to WinForms .NET find those "limits" arbitrary, unexpected.
Ideally, you'd like to have the ability to have one ListView Control appear in more than Window, but that will not happen in WinForms. Yes, WinForms does allow you to move a Control from one Form to another, and it is true that the Control does not lose its data when moved, but that is not suitable for a scenario like this one which requires two ListViews be visible.
Ideally, you'd like to have one DataSet, and whenever the Data in that DataSet was modified, the two ListViews in two Forms required here would be bound to it, and update automatically. However, the WinForms ListView does not support true databinding.
Another limit of the ListView Control is that it provides no Events for adding, or deleting, a ListViewItem. And, any new ListViewItem created that needs to be added to another ListView's Item Collection must be ... cloned.
So, the challenge here is to allow creation of new Data ... a new ListViewitem ... in one Form (SettingForm), and add the new ListViewItem to the ListView shown in the second Form (Showform), and to synchronize their data content.
In the solution ... one of many possible strategies ... presented here I chose to locate the update mechanism for the second ListView in the 'ShowForm from a method defined in the MainForm; that required injecting a reference to a named executable method in the MainForm into the public variable of Type 'Action (a form of Delegate) defined in the 'SettingForm.
Note that a delegate of Type 'Action is multi-cast delegate, but we are not using it that way here: we have
assigned to it rather than
subscribed to it using +=.
The update solution also required that the MainForm have a reference to the ListView in the 'ShowForm. This is implemented by having a Public Property of Type 'ListView in the 'ShowForm which is initialized in the constructor of the Class.
I chose to create FormClosing EventHandlers for both SettingForm and ShowForm that instead of closing the Forms hides them in order to prevent data loss, and possible complications if the user at run-time closed the Forms. An obvious alternative would be to hide the CloseBox on those Forms, and handle that with other code.
using System;
using System.Windows.Forms;
namespace SynchronizedLists
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private SettingForm SettingForm = new SettingForm();
private ShowForm ShowForm = new ShowForm();
private ListView PointerToShowFormListView;
private void MainForm_Load(object sender, EventArgs e)
{
SettingForm.UpdateShowFormListView = UpdateShowFormListView;
PointerToShowFormListView = ShowForm.ShowFormListView;
}
private void UpdateShowFormListView(ListViewItem newListViewItem)
{
PointerToShowFormListView.Items.Add(newListViewItem.Clone() as ListViewItem);
}
private void btnShowSettingForm_Click(object sender, EventArgs e)
{
if (! SettingForm.Visible) SettingForm.Show();
}
private void btnShowShowForm_Click(object sender, EventArgs e)
{
if (! ShowForm.Visible) ShowForm.Show();
}
}
}
using System;
using System.Windows.Forms;
namespace SynchronizedLists
{
public partial class SettingForm : Form
{
public SettingForm()
{
InitializeComponent();
}
public Action<listviewitem> UpdateShowFormListView;
private void btnOkay_Click(object sender, EventArgs e)
{
var newItem = new ListViewItem(tBxGender.Text);
newItem.SubItems.Add(tBxAge.Text);
settingListView.Items.Add(newItem);
UpdateShowFormListView(newItem);
}
private void SettingForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}
}
}
using System;
using System.Windows.Forms;
namespace SynchronizedLists
{
public partial class ShowForm : Form
{
public ShowForm()
{
InitializeComponent();
ShowFormListView = this.listView1;
}
public ListView ShowFormListView { private set; get; }
private void ShowForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}
}
}