我已经提出了许多关于如何做到这一点的问题,但它们似乎都没有效果。我在主视图上有一个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);
}
答案 0 :(得分:1)
在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个项目作为该列中的行。
答案 1 :(得分:0)
要从其他类添加项目,只需访问另一个类的方法即可从Program
添加。像这样:
//我们要添加的类:
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);
}
}
字符串数组将作为ListView
添加到SubItems
。