hi guys
i am a new of programming and new of c# i try to learn it , i try to understund how work a listview , i have a file.txt inside of this , i have 4 line .
i have creted a form with a listview with 2 colums i want the even line in the txt appear in first columns and the odd in second column i try is 2 day i did do a test but not work , this is my code , any one can help me? thankz at all

 listView1.View = View.Details;
            listView1.Columns.Add("IP", 100);
            listView1.Columns.Add("MAC", 200);

            string Result = ReadZombieTxt("Zombie.txt");

            if (Result == "")
            {
                using (StreamWriter sw = File.CreateText("Zombie.txt")) ;
            }
            else
            {

                // IList<string> list = new List<string>(Result.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
                //string[] strarr = Result.Split(new char[] { ' ' ,'\n'}, StringSplitOptions.RemoveEmptyEntries);

                ///*
                string[]  strarr = System.Text.RegularExpressions.Regex.Split(Result, "\\r\\n");
                int a = 1;
                for (int i = 0; i < strarr.Length; i++)
                {

                    // MessageBox.Show(strarr[i]);
                    //listView1.Items.Add(strarr[i]);//.SubItems.Add(strarr[i]);
                    ListViewItem item = new ListViewItem();
                    if (IsEven(i))
                    {
                        item.Text = strarr[i];
                    }
                    else
                    {
                        item.SubItems.Add(strarr[i]);
                    }

                    listView1.Items.Add(item);

                }
               // */

Recommended Answers

All 3 Replies

Does strarr have the proper elements?
Does ReadZombieTxt return the proper string?
What does Zombie.txt look like?

Answer these questions and it'll be easier to get help.

Does strarr have the proper elements? .... i think yes becaus if in for i put a console write or messagebox return correctly all
Does ReadZombieTxt return the proper string? ... i think yes
What does Zombie.txt look like? inside a txt i have 192.168.0.50 first line , second line mac address aa:VV:VV:FF:FF
thankz again for support

It looks to me that you're adding the SubItem to a new LitViewItem instead of an existing one. By stepping through the array by 2, you can add the SubItem to the existing ListViewItem. I haven't been abble to test this, but, something like this should work:

for (int i = 0; i < strarr.Length; i += 2)
{
        ListViewItem item = new ListViewItem();
        item.Text = strarr[i];
        item.SubItems.Add(strarr[i+1]);
        listView1.Items.Add(item);            
}

On a side note, if I'm not mistaken, the ListView.Add method returns a ListViewItem, so you can chain the SubItem.Add on to the ListView.Add.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.