IndexOf in listBox.

MiPakTeh 1,476 Reputation points
2022-08-22T02:57:10.693+00:00

Hi All ,
In my listBox have line count and data.I want show in listBox2 items is line only without the data.
Line 0 to 7.In listbox show" 2 and 3" or " 1,2 and 3".
I want use IndexOf code to remove that Data.

Text File;=7508,7989,5177,5894,8711,5618,7784,0772,1148,2302,3007,0445,7829,5254,0672,6012,2931,8558,9004,8286,4251,3918,4525
3142,8140,0147,1022,9497,6486,2969,3132,1470,5048,0440,0720,3074,2699,5176,7436,0573,6838,3996,2816,5044,0842,2215
6777,0370,0622,5686,7406,5002,8392,1134,8105,3722,8120,7627,9722,9637,2796,6892,2386,2467,8991,3882,8678,3000,2243
9833,3856,8514,5533,4623,4715,6821,2943,4246,4592,8990,2299,3000,1630,1635,1426,7648,8883,9426,1357,0309,7176,2005
1510,4365,6184,3709,2544,8940,3138,6743,0193,6703,2157,3618,4561,7326,6067,1156,6705,8319,4406,3085,8871,0661,9861
8403,1980,5490,7606,6127,6431,4916,0421,0111,0495,9858,0020,8117,2913,6913,8912,1929,9174,1653,0722,2143,2283,6557
2895,3572,9580,2712,3854,7385,2866,1925,8168,3931,8140,1541,3263,2393,5656,9860,4136,8550,7844,2627,1576,5170,2300
1109,7796,6815,2226,1854,1001,7718,0956,3157,4504,2122,2151,0788,9371,1838,1155,9435,0502,1337,7646,0096,0757,6351

Here is code test;

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.IO;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
  
namespace ListB_1  
{  
    public partial class Form1 : Form  
    {  
        List<string> Data1 = new List<string>();  
        List<string> Data2 = new List<string>();  
  
        public Form1()  
        {  
            InitializeComponent();  
        }  
  
        private void Form1_Load(object sender, EventArgs e)  
        {  
            int linecount = 0;  
  
            foreach (var s in File.ReadLines(@"C:\Users\family\Documents\Ahad.txt"))  
            {  
                listBox1.Items.Add(linecount++ + " " + s);  
                Data1.Add(linecount.ToString() + " " + s);  
  
            }  
        }  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            string a = "";  
            string b = "";  
  
            for (int i = 0; i < listBox1.Items.Count; i++)  
            {  
                a = listBox1.Items[5].ToString();  
                b = listBox1.Items[6].ToString();  
            }  
            listBox2.Items.Add(a);  
            listBox2.Items.Add(b);  
  
  
        }  
  
        private void button2_Click(object sender, EventArgs e)  
        {  
  
  
        }  
    }  
}  


  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2022-08-23T03:03:50.56+00:00

    @ MiPakTeh-6272 , If you want to show only the linecount in the listbox2, there is no need to use Indexof method to remove it.

    You could try the following code.

     private void button1_Click(object sender, EventArgs e)  
            {  
      
                for (int i = 0; i < listBox1.Items.Count; i++)  
                {  
                    listBox2.Items.Add(i);  
                }  
               
            }  
    

    If this still exists the problem, please feel free to let me know.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful