• C# Listview problem?

    Adelaid Member

    I got a code for listview that can read a text file. but there is a problem with this code its in Add items to the list.

    private void Form1_Load(System.Object sender, System.EventArgs e)
    {
       ListView1.View = View.Details;
       ListView1.GridLines = true;
       ListView1.FullRowSelect = true;
       ListView1.HideSelection = false;
       ListView1.MultiSelect = false;
       ListView1.Columns.Add("IP", 90, HorizontalAlignment.Left);
       ListView1.Columns.Add("Port", 40, HorizontalAlignment.Left);
       ListView1.Columns.Add("Protocol", 80, HorizontalAlignment.Left);
       ListView1.Columns.Add("Type", 60, HorizontalAlignment.Left);
       ListView1.Columns.Add("Speed", 50, HorizontalAlignment.Left);
    }
    
    private void Button4_Click(System.Object sender, System.EventArgs e)
    {
       OpenFileDialog1.ShowDialog();
       string filepath = OpenFileDialog1.FileName;
       System.IO.StreamReader inputstream = new System.IO.StreamReader(filepath);
       string[] newstr = new string[3];
       string IP = null;
       string Port = null;
       string Protocol = null;
       string Type = null;
       string Speed = null;
    
       //Read while there is mor characters to read
       while (inputstream.Peek() != -1) {
          //Split each line containing Account|Password into the array
          newstr = inputstream.ReadLine().Split("|");
          //Assigm the values to the variables
          IP = newstr[0];
          Port = newstr[1];
          Protocol = newstr[2];
          Type = newstr[3];
          Speed = newstr[4];
    
          //Add them to the list
          this.ListView1.Items.Add(IP);
          this.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(Port);
          this.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(Protocol);
          this.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(Type);
          this.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(Speed);
       }
    
       inputstream.Close();
    }
    
    private void Button3_Click(object sender, EventArgs e)
    {
       foreach (ListViewItem Item in ListView1.Items) {
          ListView1.Items.Remove(Item);
       }
    } 
Viewing 0 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish