listView选择项目到文本框

时间:2017-05-12 15:36:37

标签: c# winforms visual-studio listview

所以我的列表视图中填充了项目。当我选择项目时,它适用于第一行,但接下来的行将显示下一行。

所以它就像列1-4 c1,c2,c3,c4

textbox1显示在第1行= c1的第一行值 textbox1在第2行显示第2行= c2

当我需要它留在c1

编辑: 所以我想通过阅读每行See Photo中的属性来转到作业文件。但问题是作业文件有一个属性文件夹,所以我将从每行获取的值导航到我需要的正确文件夹,如果这是有道理的。但是在照片中,它应该如何工作,但如果我选择那个下面的行,它会给我一个错误,因为int不存在。因为下一行之后的每一行都希望将值放在错误文本框中的旁边的列中。

错误:System.ArgumentOutOfRangeException:'InvalidArgument ='1'的值对'index'无效。'

private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
    {

        int Itemindex = e.ItemIndex;
        ListViewItem item = listView1.SelectedItems[Itemindex];
        //fill the text boxes
        textBox2.Text = item.Text;
       textBox3.Text = item.SubItems[1].Text;
        textBox4.Text = item.SubItems[2].Text;
       textBox5.Text = item.SubItems[3].Text;

    }

private void Chad2017()
    {
        try
        {
            if (textBox1.Text != "arc1")
            {
                textBox1.Text = "arc1";
                listView1.Visible = true;


                listView1.Sort();
                string[] dirs = Directory.GetDirectories(JobArc1 + @"\2017\1-January");
                string[] dirs1 = Directory.GetDirectories(JobArc1 + @"\2017\2-February");
                string[] dirs2 = Directory.GetDirectories(JobArc1 + @"\2017\3-March");
                string[] dirs3 = Directory.GetDirectories(JobArc1 + @"\2017\4-April");
                string[] dirs4 = Directory.GetDirectories(JobArc1 + @"\2017\5-May");
                string[] dirs5 = Directory.GetDirectories(JobArc1 + @"\2017\6-June");
                string[] dirs6 = Directory.GetDirectories(JobArc1 + @"\2017\7-July");
                string[] dirs7 = Directory.GetDirectories(JobArc1 + @"\2017\8-August");
                string[] dirs8 = Directory.GetDirectories(JobArc1 + @"\2017\9-September");
                string[] dirs9 = Directory.GetDirectories(JobArc1 + @"\2017\10-October");
                string[] dirs10 = Directory.GetDirectories(JobArc1 + @"\2017\11-November");
                string[] dirs11 = Directory.GetDirectories(JobArc1 + @"\2017\12-December");
                string[] files = Directory.GetFiles(JobLoc1);
                foreach (string dir in dirs)
                {
                    try
                    {
                        string JobNumber = File.ReadAllText(@dir + "\\Properties\\thisJobNumber.txt");
                        string JobName = File.ReadAllText(@dir + "\\Properties\\JobName.txt");
                        string BidDate = File.ReadAllText(@dir + "\\Properties\\BidDate.txt");
                        string Location = File.ReadAllText(@dir + "\\Properties\\Location.txt");
                        string Contact = File.ReadAllText(@dir + "\\Properties\\Contact.txt");
                        string[] JobInfo = { JobName, BidDate, Location, Contact };

                        ListViewItem item1 = new ListViewItem();

                        if (Contact != "")
                        {
                            listView1.Items.Add(JobNumber).SubItems.AddRange(JobInfo);
                            //  listView1.Items[].SubItems.Add(File.ReadAllText(@dir + "\\Properties\\JobName.txt"));
                        }
                    }
                    catch { }
                }
                foreach (string dir1 in dirs1)
                {
                    try
                    {
                        string JobNumber = File.ReadAllText(@dir1 + "\\Properties\\thisJobNumber.txt");
                        string JobName = File.ReadAllText(@dir1 + "\\Properties\\JobName.txt");
                        string BidDate = File.ReadAllText(@dir1 + "\\Properties\\BidDate.txt");
                        string Location = File.ReadAllText(@dir1 + "\\Properties\\Location.txt");
                        string Contact = File.ReadAllText(@dir1 + "\\Properties\\Contact.txt");
                        string[] JobInfo = { JobName, BidDate, Location, Contact };

                        ListViewItem item1 = new ListViewItem();

                        if (Contact != "")
                        {
                            listView1.Items.Add(JobNumber).SubItems.AddRange(JobInfo);
                            //  listView1.Items[].SubItems.Add(File.ReadAllText(@dir + "\\Properties\\JobName.txt"));
                        }
                    }
                    catch { }
                }

                for each (string dir2 in dirs2)
                {
                    try
                    {
                        string JobNumber = File.ReadAllText(@dir2 + "\\Properties\\thisJobNumber.txt");
                        string JobName = File.ReadAllText(@dir2 + "\\Properties\\JobName.txt");
                        string BidDate = File.ReadAllText(@dir2 + "\\Properties\\BidDate.txt");
                        string Location = File.ReadAllText(@dir2 + "\\Properties\\Location.txt");
                        string Contact = File.ReadAllText(@dir2 + "\\Properties\\Contact.txt");
                        string[] JobInfo = { JobName, BidDate, Location, Contact };

                        ListViewItem item1 = new ListViewItem();

                        if (Contact != "")
                        {
                            listView1.Items.Add(JobNumber).SubItems.AddRange(JobInfo);
                            //  listView1.Items[].SubItems.Add(File.ReadAllText(@dir + "\\Properties\\JobName.txt"));
                        }
                    }
                    catch { }
                }
                foreach (string dir3 in dirs3)
                {
                    try
                    {
                        string JobNumber = File.ReadAllText(@dir3 + "\\Properties\\thisJobNumber.txt");
                        string JobName = File.ReadAllText(@dir3 + "\\Properties\\JobName.txt");
                        string BidDate = File.ReadAllText(@dir3 + "\\Properties\\BidDate.txt");
                        string Location = File.ReadAllText(@dir3 + "\\Properties\\Location.txt");
                        string Contact = File.ReadAllText(@dir3 + "\\Properties\\Contact.txt");
                        string[] JobInfo = { JobName, BidDate, Location, Contact };

                        ListViewItem item1 = new ListViewItem();

                        if (Contact != "")
                        {
                            listView1.Items.Add(JobNumber).SubItems.AddRange(JobInfo);
                            //  listView1.Items[].SubItems.Add(File.ReadAllText(@dir + "\\Properties\\JobName.txt"));
                        }
                    }
                    catch { }
                }
                foreach (string dir4 in dirs4)
                {
                    try
                    {
                        string JobNumber = File.ReadAllText(@dir4 + "\\Properties\\thisJobNumber.txt");
                        string JobName = File.ReadAllText(@dir4 + "\\Properties\\JobName.txt");
                        string BidDate = File.ReadAllText(@dir4 + "\\Properties\\BidDate.txt");
                        string Location = File.ReadAllText(@dir4 + "\\Properties\\Location.txt");
                        string Contact = File.ReadAllText(@dir4 + "\\Properties\\Contact.txt");
                        string[] JobInfo = { JobName, BidDate, Location, Contact };

                        ListViewItem item1 = new ListViewItem();

                        if (Contact != "")
                        {
                            listView1.Items.Add(JobNumber).SubItems.AddRange(JobInfo);
                            //  listView1.Items[].SubItems.Add(File.ReadAllText(@dir + "\\Properties\\JobName.txt"));
                        }
                    }
                    catch { }
                }
                foreach (string dir5 in dirs5)
                {
                    try
                    {
                        string JobNumber = File.ReadAllText(@dir5 + "\\Properties\\thisJobNumber.txt");
                        string JobName = File.ReadAllText(@dir5 + "\\Properties\\JobName.txt");
                        string BidDate = File.ReadAllText(@dir5 + "\\Properties\\BidDate.txt");
                        string Location = File.ReadAllText(@dir5 + "\\Properties\\Location.txt");
                        string Contact = File.ReadAllText(@dir5 + "\\Properties\\Contact.txt");
                        string[] JobInfo = { JobName, BidDate, Location, Contact };

                        ListViewItem item1 = new ListViewItem();

                        if (Contact != "")
                        {
                            listView1.Items.Add(JobNumber).SubItems.AddRange(JobInfo);
                            //  listView1.Items[].SubItems.Add(File.ReadAllText(@dir + "\\Properties\\JobName.txt"));
                        }
                    }
                    catch { }
                }
                foreach (string dir6 in dirs6)
                {
                    try
                    {
                        string JobNumber = File.ReadAllText(@dir6 + "\\Properties\\thisJobNumber.txt");
                        string JobName = File.ReadAllText(@dir6 + "\\Properties\\JobName.txt");
                        string BidDate = File.ReadAllText(@dir6 + "\\Properties\\BidDate.txt");
                        string Location = File.ReadAllText(@dir6 + "\\Properties\\Location.txt");
                        string Contact = File.ReadAllText(@dir6 + "\\Properties\\Contact.txt");
                        string[] JobInfo = { JobName, BidDate, Location, Contact };

                        ListViewItem item1 = new ListViewItem();

                        if (Contact != "")
                        {
                            listView1.Items.Add(JobNumber).SubItems.AddRange(JobInfo);
                            //  listView1.Items[].SubItems.Add(File.ReadAllText(@dir + "\\Properties\\JobName.txt"));
                        }
                    }
                    catch { }
                }
                foreach (string dir7 in dirs7)
                {
                    try
                    {
                        string JobNumber = File.ReadAllText(@dir7 + "\\Properties\\thisJobNumber.txt");
                        string JobName = File.ReadAllText(@dir7 + "\\Properties\\JobName.txt");
                        string BidDate = File.ReadAllText(@dir7 + "\\Properties\\BidDate.txt");
                        string Location = File.ReadAllText(@dir7 + "\\Properties\\Location.txt");
                        string Contact = File.ReadAllText(@dir7 + "\\Properties\\Contact.txt");
                        string[] JobInfo = { JobName, BidDate, Location, Contact };

                        ListViewItem item1 = new ListViewItem();

                        if (Contact != "")
                        {
                            listView1.Items.Add(JobNumber).SubItems.AddRange(JobInfo);
                            //  listView1.Items[].SubItems.Add(File.ReadAllText(@dir + "\\Properties\\JobName.txt"));
                        }
                    }
                    catch { }


                }
                foreach (string dir8 in dirs8)
                {
                    try
                    {
                        string JobNumber = File.ReadAllText(@dir8 + "\\Properties\\thisJobNumber.txt");
                        string JobName = File.ReadAllText(@dir8 + "\\Properties\\JobName.txt");
                        string BidDate = File.ReadAllText(@dir8 + "\\Properties\\BidDate.txt");
                        string Location = File.ReadAllText(@dir8 + "\\Properties\\Location.txt");
                        string Contact = File.ReadAllText(@dir8 + "\\Properties\\Contact.txt");
                        string[] JobInfo = { JobName, BidDate, Location, Contact };

                        ListViewItem item1 = new ListViewItem();

                        if (Contact != "")
                        {
                            listView1.Items.Add(JobNumber).SubItems.AddRange(JobInfo);
                            //  listView1.Items[].SubItems.Add(File.ReadAllText(@dir + "\\Properties\\JobName.txt"));
                        }
                    }
                    catch { }
                }
                foreach (string dir9 in dirs9)
                {
                    try
                    {
                        string JobNumber = File.ReadAllText(@dir9 + "\\Properties\\thisJobNumber.txt");
                        string JobName = File.ReadAllText(@dir9 + "\\Properties\\JobName.txt");
                        string BidDate = File.ReadAllText(@dir9 + "\\Properties\\BidDate.txt");
                        string Location = File.ReadAllText(@dir9 + "\\Properties\\Location.txt");
                        string Contact = File.ReadAllText(@dir9 + "\\Properties\\Contact.txt");
                        string[] JobInfo = { JobName, BidDate, Location, Contact };

                        ListViewItem item1 = new ListViewItem();

                        if (Contact != "")
                        {
                            listView1.Items.Add(JobNumber).SubItems.AddRange(JobInfo);
                            //  listView1.Items[].SubItems.Add(File.ReadAllText(@dir + "\\Properties\\JobName.txt"));
                        }
                    }
                    catch { }
                }
                foreach (string dir10 in dirs10)
                {
                    try
                    {
                        string JobNumber = File.ReadAllText(@dir10 + "\\Properties\\thisJobNumber.txt");
                        string JobName = File.ReadAllText(@dir10 + "\\Properties\\JobName.txt");
                        string BidDate = File.ReadAllText(@dir10 + "\\Properties\\BidDate.txt");
                        string Location = File.ReadAllText(@dir10 + "\\Properties\\Location.txt");
                        string Contact = File.ReadAllText(@dir10 + "\\Properties\\Contact.txt");
                        string[] JobInfo = { JobName, BidDate, Location, Contact };

                        ListViewItem item1 = new ListViewItem();

                        if (Contact != "")
                        {
                            listView1.Items.Add(JobNumber).SubItems.AddRange(JobInfo);
                            //  listView1.Items[].SubItems.Add(File.ReadAllText(@dir + "\\Properties\\JobName.txt"));
                        }
                    }
                    catch { }
                }
                foreach (string dir11 in dirs11)
                {
                    try
                    {
                        string JobNumber = File.ReadAllText(@dir11 + "\\Properties\\thisJobNumber.txt");
                        string JobName = File.ReadAllText(@dir11 + "\\Properties\\JobName.txt");
                        string BidDate = File.ReadAllText(@dir11 + "\\Properties\\BidDate.txt");
                        string Location = File.ReadAllText(@dir11 + "\\Properties\\Location.txt");
                        string Contact = File.ReadAllText(@dir11 + "\\Properties\\Contact.txt");
                        string[] JobInfo = { JobName, BidDate, Location, Contact };

                        ListViewItem item1 = new ListViewItem();

                        if (Contact != "")
                        {
                            listView1.Items.Add(JobNumber).SubItems.AddRange(JobInfo);
                            //  listView1.Items[].SubItems.Add(File.ReadAllText(@dir + "\\Properties\\JobName.txt"));
                        }
                    }
                    catch { }
                }
            }
            else
            {
                textBox1.Text = "arc0";
                listView1.Visible = false;
            }

        }
        catch { }
    }
private void button1_Click(object sender, EventArgs e)
    {

        try
        {

            if (textBox1.Text != "1")
            {
                textBox1.Text = "1";
                listView1.Visible = true;
                listView1.Items.Clear();

                listView1.Sort();
                string[] dirs = Directory.GetDirectories(JobLoc1);
                string[] files = Directory.GetFiles(JobLoc1);
                foreach (string dir in dirs)
                {
                    try
                    {
                        string JobNumber = File.ReadAllText(@dir + "\\Properties\\thisJobNumber.txt");
                        string JobName = File.ReadAllText(@dir + "\\Properties\\JobName.txt");
                        string BidDate = File.ReadAllText(@dir + "\\Properties\\BidDate.txt");
                        string Location = File.ReadAllText(@dir + "\\Properties\\Location.txt");
                        string Contact = File.ReadAllText(@dir + "\\Properties\\Contact.txt");
                        string[] JobInfo = { JobName, BidDate, Location, Contact };

                        ListViewItem item1 = new ListViewItem();

                        if (JobNumber != "")
                        {
                            listView1.Items.Add(JobNumber).SubItems.AddRange(JobInfo);
                            //  listView1.Items[].SubItems.Add(File.ReadAllText(@dir + "\\Properties\\JobName.txt"));
                        }
                    }
                    catch { }
                }
            }

            else
            {
                textBox1.Text = "0";
                listView1.Visible = false;
            }

1 个答案:

答案 0 :(得分:0)

SelectedItems是与Items集合不同的集合。

改变这个:

int Itemindex = e.ItemIndex;
ListViewItem item = listView1.SelectedItems[Itemindex];

到此:

int Itemindex = e.ItemIndex;
ListViewItem item = listView1.Items[Itemindex];