开发者

Copy text from listview into another winform

I am really stuck with this so I hope someone can help. I have 2 winforms, one has a listview and another has a textbox. I want to check which item is checked in the listview and copy this text into the second form. I have tried this code but it won't work, any help is greatly appreciated!

// 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 for开发者_StackOverflowm

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


You are creating a new Form1 inside of Form4 after it has been loaded. You need a reference to the original Form1. This can be accomplished in several ways, probably the easiest is passing a reference into the Form4 constructor.

// 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();
}


You only need a couple of changes. No need to add your own way of storing the owner form as this functionality already exists.

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
}  

Then reference it on the other form.

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


Try thisway

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();
        }


    }
}

FORM 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;
            }
        }    
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜