从另一个类向ListView添加项

本文关键字:添加 ListView 另一个 | 更新日期: 2023-09-27 18:08:56

我查了很多关于如何做到这一点的问题,但似乎没有一个是有效的。我在主视图上有一个ListView,我从另一个表单向它添加数据来填充它。在另一种形式,我有几个字符串从文本字段,我想传递给它。下面是我的代码:

//添加新的预订表单:

namespace Booker
{
  public partial class newBookingForm : Form
{
    public newBookingForm()
    {
        InitializeComponent();
    }
    private void NewBookingForm_Load(object sender, EventArgs e)
    {
        roomField.Focus();
    }
    private void newBookingButton_Click(object sender, EventArgs e)
    {
        // Add to ListView
        // Add to Parse
        string room = this.roomField.Text;
        string date = this.datePicker.Value.ToShortDateString();
        string time = this.timeField.Text;
        string person = "<username>"; // Get current Parse user
        Booker booker = new Booker();
        string[] array = new string[4] { room, date, time, person };
        booker.UpdatingListView(array);
        this.Hide();
    }
}

}

//ListView表单:

namespace Booker
{
public partial class Booker : Form
{
    delegate void MyDelegate(string[] array);
    public Booker()
    {
        InitializeComponent();
    }
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        base.OnFormClosing(e);
        Environment.Exit(0);
    }
    private void Booker_Load(object sender, EventArgs e)
    {
        listView.View = View.Details;
        listView.AllowColumnReorder = false;
    }
    /* Methods for the Booker form */
    private void newBookingButton_Click(object sender, EventArgs e)
    {
        newBookingForm nbf = new newBookingForm();
        nbf.Show();
    }
    public void UpdatingListView(string[] array)
    {
        if (this.listView.InvokeRequired)
            this.listView.Invoke(new MyDelegate(UpdatingListView), new object[] { array });
        else
        {
            ListViewItem lvi = new ListViewItem(array[0]);
            lvi.SubItems.Add(array[1]);
            this.listView.Items.Add(lvi);
        }
    }
    private void exitButton_Click(object sender, EventArgs e)
    {
        // Error - no action being sent
    }
    private void helpButton_Click(object sender, EventArgs e)
    {
        // Display help panel
    }
    private void contactButton_Click(object sender, EventArgs e)
    {
        // Open panel/email
    }
    private void newBooking_Click(object sender, EventArgs e)
    {
        newBookingButton_Click(sender, e);
        //string[] row = { "Meeting room", "April 11, 2013", "12:00PM-1:00PM", "Ryan" };
        //var listViewItem = new ListViewItem(row);
        //listView.Items.Add(listViewItem);
    }

从另一个类向ListView添加项

UpdatingListView:

ListViewItem lvi = new ListViewItem(array[0],0);
lvi.SubItems.Add(array[1]);
lvi.SubItems.Add(array[2]);
lvi.SubItems.Add(array[3]);
this.listView.Items.Add(lvi);

这将在ListView的第一个位置创建一个列,数组中的4个项目作为该列的行。

要从另一个类添加项,只需访问其他类的方法从Program添加项。这样的:

//添加from类:

string name = "Ryan";
string site = "imryan.net";
string[] array = new string[2] = { name, site };
Program.classWithListView.UpdatingListView(array);

//我们要添加的类:

public void UpdatingListView(string[] array)
    {
        if (this.listView.InvokeRequired)
        {
            this.listView.Invoke(new MyDelegate(UpdatingListView), new object[] { array });
        } else {
            ListViewItem lvi = new ListViewItem(array[0]);
            lvi.SubItems.Add(array[1]);
            lvi.SubItems.Add(array[2]);
            this.listView.Items.Add(lvi);
        }
    }

字符串数组将作为SubItems添加到ListView