问题描述
当单击确定"按钮时,如何从CustomerFrame类(左侧)中获取值以从MainForm(右侧)中显示在Listview中?
在此先感谢
这是我的程序: http://imageshack.us/f/220/kasta3.png/ [ ^ ]
How can i get the values from my CustomerFrame class(on the left) to appear in my Listview from my MainForm (on the right)when clicking the OK button
thanks in advance
here is my program: http://imageshack.us/f/220/kasta3.png/[^]
推荐答案
public partial class Form2 : Form
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
public string HomePhone { get; private set; }
public string CellPhone { get; private set; }
public string Street { get; private set; }
public string City { get; private set; }
public string ZipCode { get; private set; }
public string Country { get; private set; }
public Form2()
{
InitializeComponent();
}
private void okButton_Click(object sender, EventArgs e)
{
//todo: perform validation to ensure values entered into the text boxes
// are valid
FirstName = firstNameTextBox.Text;
LastName = lastNameTextBox.Text;
//etc...
}
}
并假设这是您的主要形式
and assume this is your main form
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
if (frm2.ShowDialog() == DialogResult.OK)
{
//use the properties to get the values from frm2
string firstName = frm2.FirstName;
listView1.Items.Add(firstName);
//or if ListView is set to detail view and you have columns like
// First Name | Last Name | Etc...
//something like this will work.
listView1.Items.Add(new ListViewItem(new string[] { frm2.FirstName,
frm2.LastName /*, etc... */ }));
}
}
}
Does this make sense?
public class Mainform:Form
{
public hashtable SelectedValues = new hashtable();
...
CustomerFrame cframe = new CustomerFrame(this);
cframe.ShowDialog();
...
...
//add each element in hashtable to listview.
}
public class CustomerFrame:Form
{
Mainform owner = null;
public CustomerFrame(Mainform _owner)
{
this.owner = _owner;
}
void buttonClick(object sender, eventArgs e)
{
this.owner.SelectedValues.add(xxx,yyy);
}
}
或者,如果您无法在两个框架之间建立关系,则可以使用缓存机制在表单之间传递数据.
cahce xxx,然后通过Mainform中的线程检查它,以防万一:).
or if you cant make relation beetween two frames you can use a caching mecanism to transfer data beetween forms.
cahce xxx in CustomerFrame and than check it by a thread in Mainform just in case :).
这篇关于单击“确定"按钮时,将值值获取到列表视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!