尝试以另一种形式访问listview项

时间:2016-08-09 18:37:44

标签: c# .net winforms listview

这是我的Form1。这里我有一个名为ListViewSelectedItems的listview集合,以访问另一种形式的listview1项目。

using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace sundayschoolproject
   {
      public partial class Form1 : Form
{
    string con = ("Data Source =ZTABASSUM\\SQLEXPRESS01;Initial Catalog= Sunday School;Integrated Security=True");


    public System.Windows.Forms.ListView.SelectedListViewItemCollection     ListViewSelectedItems
    {
        get { return listView1.SelectedItems; }
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        listView1.GridLines = true;
        listView1.View = View.Details;

        //Add Columns 
        listView1.Columns.Add("ParentName", 100);
        listView1.Columns.Add("Address", 150);
        listView1.Columns.Add("City", 100);
        listView1.Columns.Add("State", 50);
        listView1.Columns.Add("Zipcode", 100);
        listView1.Columns.Add("Phone", 150);


    }

    private void button1_Click(object sender, EventArgs e)
    {

        string sql = ("Select * from Family");
        SqlConnection connection = new SqlConnection(con);

        SqlCommand cmd = new SqlCommand(sql, connection);

        connection.Open();

        SqlDataReader dr = cmd.ExecuteReader();

        listView1.Items.Clear();

        while (dr.Read())
        {
            ListViewItem lv = new ListViewItem(dr.GetString(0));
            lv.SubItems.Add(dr.GetString(1));
            lv.SubItems.Add(dr.GetString(2));
            lv.SubItems.Add(dr.GetString(3));
            lv.SubItems.Add(dr.GetString(4));
            lv.SubItems.Add(dr.GetString(5));
            listView1.Items.Add(lv);

        }




        dr.Close();
        connection.Close();



    }


    //registered a listview1_click handler and then if name is clicked, a new form pops up 
 private void listView1_Click(object sender, EventArgs e)
    {
        if (listView1.SelectedItems.Count > 0)
        {
            Form2 frm = new Form2();
            frm.Show();
            MessageBox.Show("You clicked " + listView1.SelectedItems[0].Text);
           ListViewItem item1 = listView1.SelectedItems[0];





            MessageBox.Show("Person's phone number is " + item1.SubItems[5].Text);
        }
        else
        {
            MessageBox.Show("Please select an item");
        }






    }






}
}

这是我的Form2,名为frm。在这里,我创建了一个名为otherForm的form1实例。

    using System;
    using System.Data;
    using System.Windows.Forms;
    using System.Data.SqlClient;

    namespace sundayschoolproject
   {
     public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            Text = "Form2";
        }
            SqlConnection connection = new SqlConnection("Data      Source=ZTABASSUM\\SQLEXPRESS01;Initial Catalog= Sunday School;Integrated Security=True");
        SqlCommand comm;
        SqlDataReader dreader;


        private void frm_Load(object sender, EventArgs e)
        {
            connection.Open();

            MessageBox.Show(otherForm.hello);
            MessageBox.Show(otherForm.ListViewSelectedItems[0].Text);

    }
}

但是第二个消息框不会在索引处显示所选项目,说索引超出范围?即使那里有一个项目。非常感谢帮忙。

1 个答案:

答案 0 :(得分:3)

你应该用自己的语言意识到这个问题。你说:

这里我创建了一个form1的实例

您不需要创建该表单的新实例,因为它是新实例,是另一个实例。

您需要传递给Form2 实例Form1的构造函数,其中包含您的数据(状态)&#39 ;重新合作。

public Form2(Form1 form1)
{
     InitializeComponent();
     Text = "Form2";
     otherForm = form1;
}

呼叫者:

Form2 form2 = new Form2(this);