单击“确定"按钮时,将值值获取到列表视图中 [英] Get values values into listview when click OK button

查看:72
本文介绍了单击“确定"按钮时,将值值获取到列表视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当单击确定"按钮时,如何从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/[^]

推荐答案

所以可能有几种方法可以做到这一点,但这就是我要做的.
假设这是您收集信息的对话框
So there are probably a few ways to do this but here is what I would do.
Assume this is your dialog to gather the information
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?


在2帧之间建立关系.像:
make a relation beetween 2 frame. something like :
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 :).


这篇关于单击“确定"按钮时,将值值获取到列表视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆