添加到列表视图,从另一种形式 [英] Add to listview from another form

查看:115
本文介绍了添加到列表视图,从另一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何我可以通过文本框在窗体2输入数据在Form1更新我的列表视图。如果我把所有的文本框相同的形式等对我的code正常工作。

我想我需要2日一些参考第一种形式,但不能让它的工作。

有关把我在正确的方向任何提示就好了,也这样做的没有更好的办法任何提示。

这是code我到目前为止有:

Form1中:

 公共部分Form1类:表格
    {
        公共Form1中()
        {
            的InitializeComponent();
        }        公众的ListView MyListView
        {
            得到
            {
                返回任务列表;
            }
        }

窗体2:

 公共部分类窗口2:表
    {
        公共窗口2()
        {
            的InitializeComponent();
        }        form1的F;        公共插件(Form1的F)
        {
            this.f = F;
        }        公共无效AddToList()
        {
                ListViewItem的ITEM1 =新的ListViewItem(txtName.Text);
                item1.SubItems.Add(txtEmail.Text);
                item1.SubItems.Add(txtPhone.Text);
                f.MyListView.Items.AddRange(新的ListViewItem [] {ITEM1});
        }


解决方案

做的事情是使用事件的最直接的方式。你可以在窗口2 添加事件,将每次添加一个项目时火了,包括要插入的文本(你必须多条信息,因此自定义数据类型将是适当的)。然后,您可以添加一个处理方法窗口2 它增加了项目的ListView控件。然后,将两者一起在code,它是建立在两种形式,生活应该是不错的。

所以,提供一些code,首先是事件的数据结构:

 公共委托无效HandleItemAdded(对象发件人,ItemAddedEventArgs E);公共结构ItemAddedEventArgs:EventArgs的
{
    公共字符串名称;
    公共字符串的电子邮件;
    公共字符串电话;    公共ItemAddedEventArgs(字符串名称,字符串email,字符串电话)
    {
        名称=名称;
        电子邮件=电子邮件;
        手机=手机;
    }
}

然后,我们对窗口2

事件code

 公共事件HandleItemAdded ItemAdded;
// ..一些其他的东西
公共无效RaiseItemAdded(ItemAddedEventArgs E)
{
    如果(ItemAdded!= NULL)
        ItemAdded(这一点,E);
}// ...现在你AddToList
公共无效AddToList()
{    RaiseItemAdded(新ItemAddedEventArgs(txtName.Text,txtEmail.Text,txtPhone.Text);
}

现在我们可以在 Form1中

添加一个处理程序

 公共无效HandleItemAdded(对象发件人,ItemAddedEventArgs E)
{
    ListViewItem的ITEM1 =新的ListViewItem(txtName.Text);
    item1.SubItems.Add(txtEmail.Text);
    item1.SubItems.Add(txtPhone.Text);
    MyListView.Add(ITEM1);
}

和最后但并非最不重要,我们需要绑在一起

  // ...不知道你的code看起来像什么,但我们假设我们有名为form1Form和form2Form两种形式实例
form2Form.ItemAdded + = form1Form.HandleItemAdded

I am wondering how I can update my listview in form1 by entering data via textboxes in form2. My code works fine if i put all the text boxes on the same form etc.

I figured I needed some reference to the first form on 2nd but can't get it working.

Any tips for putting me in the right direction would be nice, also any tips for any better way of doing this.

This is the code I have so far:

Form1:

public partial class form1 : Form
    {
        public form1()
        {
            InitializeComponent();
        }

        public ListView MyListView
        {
            get
            {
                return taskList;
            }
        }

Form2:

public partial class form2 : Form
    {
        public form2()
        {
            InitializeComponent();
        }

        form1 f;

        public add(form1 f)
        {
            this.f = f;
        }        

        public void AddToList()
        {
                ListViewItem item1 = new ListViewItem(txtName.Text);
                item1.SubItems.Add(txtEmail.Text);
                item1.SubItems.Add(txtPhone.Text);
                f.MyListView.Items.AddRange(new ListViewItem[] { item1 });
        }

解决方案

The most straight forward way of doing things would be to use events. You could add an event on form2 that would fire each time an item is added, and includes the text to be inserted (you have multiple pieces of information, so a custom data type would be appropriate). You can then add a handler method to form2 which adds the item to its ListView. You then tie the two together in the code that is creating the two forms, and life should be good.

So, to provide some code, First up is the data structure for the event:

public delegate void HandleItemAdded(object sender, ItemAddedEventArgs e);    

public struct ItemAddedEventArgs : EventArgs
{
    public string Name;
    public string Email;
    public string Phone;

    public ItemAddedEventArgs(string name, string email, string phone)
    {
        Name = name;
        Email = email;
        Phone = phone;
    }
}

Then we have the event code on form2

public event HandleItemAdded ItemAdded;
// .. some other stuff
public void RaiseItemAdded(ItemAddedEventArgs e)
{
    if(ItemAdded != null)
        ItemAdded(this,e);
}

// ... now for your AddToList
public void AddToList()
{

    RaiseItemAdded(new ItemAddedEventArgs(txtName.Text,txtEmail.Text, txtPhone.Text);
}

And now we can add a handler in form1

public void HandleItemAdded(object sender, ItemAddedEventArgs e)
{
    ListViewItem item1 = new ListViewItem(txtName.Text);
    item1.SubItems.Add(txtEmail.Text);
    item1.SubItems.Add(txtPhone.Text);
    MyListView.Add(item1);
}

And last but not least we need to tie them together

//...not sure what your code looks like, but we'll assume we have instances of the two forms named form1Form and form2Form
form2Form.ItemAdded += form1Form.HandleItemAdded

这篇关于添加到列表视图,从另一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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