title: Trying to access listview item in another form
body: <p>This is my Form1. Here I have a listview collection called ListViewSelectedItems to access my listview1 items in another form.</p>
  
  <pre><code>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 &gt; 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");
          }
  
  
  
  
  
  
      }
  
  
  
  
  
  
  }
  }
  </code></pre>
  
  <p>Here is my Form2 called frm. Here I have created an instance of my form1 called otherForm. </p>
  
  <pre><code>    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);
  
      }
  }
  </code></pre>
  
  <p>However the second messagebox will not show the selected item at the index, saying the index is out of range? Even though there is an item there.  Thanks so much for the help. </p>