将项目添加到另一个类的ListView [英] Add item to ListView from another class
问题描述
我已经找到了许多有关如何执行此操作的问题,但是似乎都没有.我在主视图上有一个ListView,并且通过从其他表单向其添加数据来填充它.在另一种形式上,我有几个文本字段要传递给它的字符串.这是我的代码:
I've looked up many question on how to do this, but none of them seem to work. I have a ListView on my main view, and I'm populating it from adding data to it from another form. On the other form, I have a couple strings from text fields I'd like to pass to it. Here's my code:
//添加新的预订表格:
// Add new booking form:
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表单:
// ListView form:
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);
}
推荐答案
要从另一个类添加项目,只需访问另一个类的方法即可从 Program
添加.像这样:
To add items from another class, just access the other class' method to add from Program
. Like this:
//我们要添加的类:
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
.
The string array will be added to the ListView
as SubItems
.
这篇关于将项目添加到另一个类的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!