我的订单中有一个完整列表框。
我想把列表框中的所有项目都转移到我的列表视图中。
然后我想拿下我的listview并以另一种形式(我的消息框)显示它。
我的新列表视图:
private void CustomerInfo_Click(object sender, EventArgs e)
{
ListViewItem customers = new ListViewItem(fullName.Text);
customers.SubItems.Add(totalcount.ToString());
customers.SubItems.Add(total.ToString());
customers.SubItems.Add(Address.Text);
customers.SubItems.Add(telephone.Text);
for (int i = 0; i < OrderlistBox.Items.Count; i++)
{
customers.SubItems.Add(OrderlistBox.Items[i].ToString());
}
Customers.Items.Add(customers);
//CLEAR ALL FIELDS
OrderlistBox.Items.Clear();
fullName.Text = "";
Address.Text = "";
telephone.Text = "";
totalDue.Text = "";
totalItems.Text = "";
}
我的contextMenuStrip,所以当我点击客户时,我可以得到它的信息(姓名,地址,订单等):
private void customerInformationToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Customers.SelectedItems.Count != 0)
{
var myformmessagedialog = new MessageBoxForm
{
name = Customers.SelectedItems[0].SubItems[0].Text,
address = Customers.SelectedItems[0].SubItems[3].Text,
telephone = Customers.SelectedItems[0].SubItems[4].Text,
};
myformmessagedialog.ShowDialog();
}
}
我的新表单,我将在其中显示客户端的所有信息的消息框:
public partial class MessageBoxForm : Form
{
public MessageBoxForm()
{
InitializeComponent();
}
public string name;
public string address;
public string telephone;
public ListViewItem order = new ListViewItem();
private void MessageBoxForm_Load(object sender, EventArgs e)
{
lblName.Text = name;
lbladdress.Text = address;
lbltelephone.Text = telephone;
orderListView.Items.Add(order);
}
}
如果这看起来令人困惑,我很抱歉,但我只是在寻求帮助,朝着正确的方向前进。任何帮助表示赞赏。
答案 0 :(得分:4)
执行此操作的一种方法是将要显示的数据放在某种ViewModel中,基本上是一类或一组具有您要显示的数据的类。然后主窗体可以显示它,您可以将对该ViewModel的引用传递给消息框,它也可以显示它。
通常,您希望避免使用任何类型的代码将不同形式的控件直接绑定在一起。
答案 1 :(得分:1)
基本答案是你没有。
您维护一系列项目(无论它们是什么)。
您可以在列表框中显示它们。
您可以在列表视图中显示它们。
如果你想说从列表框中选择一些,只将它们移动到列表视图。
然后使用列表框选择在项目集合中查找它们,创建所选项目列表,然后将其传递给带有列表视图的表单。
不要使用UI控件来存储您的数据,并且尽量不要让一个表单的UI直接依赖于另一个。
答案 2 :(得分:1)
基于您当前设置的最简单方法是将列表视图数据简单地传递到MessageBoxForm
,例如
public partial class MessageBoxForm : Form
{
...
public void LoadListView(ListViewItemCollection items)
{
orderListView.Clear();
orderListView.AddRange(items);
}
}
....
private void customerInformationToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Customers.SelectedItems.Count != 0)
{
var myformmessagedialog = new MessageBoxForm
{
name = Customers.SelectedItems[0].SubItems[0].Text,
address = Customers.SelectedItems[0].SubItems[3].Text,
telephone = Customers.SelectedItems[0].SubItems[4].Text,
};
myformmessagedialog.LoadListView(Customers.Items);
myformmessagedialog.ShowDialog();
}
}
答案 3 :(得分:0)
我猜你需要什么(我可能误解了你要找的东西)是你MessageBoxForm
传递Customers
对象的新方法:
private void customerInformationToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Customers.SelectedItems.Count != 0)
{
var myformmessagedialog = new MessageBoxForm;
myformmessagedialog.Customers = Customers;
if (myformmessagedialog.ShowDialog() == DialogResult.OK)
{
Customers = myformmessagedialog.Customers;
}
}
}
如果是这样,只需将您的类修改为:
public partial class MessageBoxForm : Form
{
public MessageBoxForm()
{
InitializeComponent();
}
private void MessageBoxForm_Load(object sender, EventArgs e)
{
if (Customers != null)
{
// add your code here to add your Customers as needed
}
}
public Customers Customers { get; set; }
}
答案 4 :(得分:0)
要从父表单访问任何内容,您需要将其传递给子表单,以便
myformmessagedialog.ShowDialog();
变为
myformmessagedialog dialog = new myformmessagedialg(this);
dialog.ShowDialog();
并且您的类构造函数变为:
public MessageBoxForm(myformmessagedialog parent){
name=parent.fullName.Text;
address=parent.address.Text;
...etc...
InitializeComponent();
}
虽然传递名称,地址等而不是整个表单可能会更好,但这种方式对于改变事物很有好处,因为你只需要更少的地方来添加另一个变量来传递。 / p>