将listview中的文本复制到另一个winform中

时间:2011-09-27 18:22:57

标签: c# winforms

我真的很困惑,所以我希望有人可以提供帮助。我有2个winforms,一个有listview,另一个有文本框。我想检查列表视图中选中的项目,并将此文本复制到第二个表单中。我已经尝试过这段代码,但它不起作用,非常感谢任何帮助!

// 1st form

private void button5_Click(object sender, EventArgs e) // Brings up the second form
{
   Form4 editItem = new Form4();
   editItem.Show();
} 

public string GetItemValue()
{
   for (int i = 0; i < listView1.Items.Count; i++)
   {
      if (listView1.Items[i].Checked == true)
      {
         return listView1.Items[i].Text;
      }
   }
   return "Error";
}

// 2nd form

private void Form4_Load(object sender, EventArgs e)
{
   Form1 main = new Form1();
   textBox1.Text = main.GetItemValue();
}

3 个答案:

答案 0 :(得分:3)

加载后,您在Form1内创建了一个新的Form4。您需要对原始Form1的引用。这可以通过多种方式实现,最简单的方法是将引用传递给Form4构造函数。

// Form 1

// This button creates a new "Form4" and shows it
private void button5_Click(object sender, EventArgs e)
{
   Form4 editItem = new Form4(this);
   editItem.Show();
} 

public string GetItemValue()
{
   for (int i = 0; i < listView1.Items.Count; i++)
   {
      if (listView1.Items[i].Checked == true)
      {
         return listView1.Items[i].Text;
      }
   }
   return "Error";
}

-

// Form 2 (Form4)

// Private member variable / reference to a Form1
private Form1 _form;

// Form4 Constructor: Assign the passed-in "Form1" to the member "Form1"
public Form4(Form1 form)
{
   this._form = form;
}

// Take the member "Form1," get the item value, and write it in the text box
private void Form4_Load(object sender, EventArgs e)
{
   textBox1.Text = this._form.GetItemValue();
}

答案 1 :(得分:1)

试试这个

FORM 1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        //Fields
        public List<string> itemTexts;

        public Form1()
        {
            InitializeComponent();

            //Generate some items
            for (int i = 0; i < 10; i++)
            {
                ListViewItem item = new ListViewItem();

                item.Text = "item number #" + i;

                listView1.Items.Add(item);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                if (item.Checked)
                {
                    itemTexts.Add(item.Text);
                }
            }

            Form2 TextBoxForm = new Form2(itemTexts);
            TextBoxForm.Show();
        }


    }
}

表格2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
        //Fields
        List<string> itemTexts;

        public Form2(List<string> itemTexts)
        {
            InitializeComponent();

            this.itemTexts = itemTexts;

            foreach (string text in itemTexts)
            {
                textBox1.Text += text + Environment.NewLine;
            }
        }    
    }
}

答案 2 :(得分:1)

您只需要进行一些更改。无需添加您自己的存储所有者表单的方式,因为此功能已存在。

private void button5_Click(object sender, EventArgs e) // Brings up the second form 
{
    Form4 editItem = new Form4();
    editItem.Show(this); //passes a reference to this form to be stored in owner
}  

然后在另一个表格中引用它。

private void Form4_Load(object sender, EventArgs e)
{
    textBox1.Text = ((Form1)owner).GetItemValue();
}