Getting all the items from the listbox winform

2020-07-23 09:11发布

问题:

Good day! I am having troubles on getting all the items, selected or not, from the listbox. Whenever I click the send button, the only items that I could get is the ones I selected (This is the current results of my code below: http://imgur.com/jA94Bjm). What I want is to get all the items from the textbox not just from the selected ones and which is not repeating.

private void cmd_send_Click_1(object sender, EventArgs e)
{
     for (int i = 0; i < listBox1.Items.Count; i++)
     {
         try
         {
             String pno = textBox4.Text.ToString();
             String path = textBox5.Text.ToString();
             String name = textBox6.Text.ToString();
             String user = textBox7.Text.ToString();
             output.Text += "\n Sent data : " + pno + " " + user + " " + name + " " + path;
          }
          catch (Exception ex)
          {
              wait.Abort();
              output.Text += "Error..... " + ex.StackTrace;
          }

          NetworkStream ns = tcpclnt.GetStream();
          String data = "";
          data = "--++" + "  " + textBox4.Text + " " + textBox5.Text + " " + textBox6.Text + " " + textBox7.Text;

          if (ns.CanWrite)
          {
              byte[] bf = new ASCIIEncoding().GetBytes(data);
              ns.Write(bf, 0, bf.Length);
              ns.Flush();
          }
    }
}

回答1:

If you want to access all your items from your listbox, you have to iterate all of the items and access the value of that item. Here's a sample how you can achieve this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    class Program
    {
        class Process
        {
            public int ProcessId { get; set; }
            public string FilePath { get; set; }
            public string FileName { get; set; }
            public string User { get; set; }
        }
        static void Main(string[] args)
        {

            Process p1 = new Process();
            p1.ProcessId = 1;
            p1.FileName = "Tool.exe";
            p1.FilePath = @"C:\Tool.exe";
            p1.User = "User1";

            Process p2 = new Process();
            p2.ProcessId = 2;
            p2.FileName = "Tool2.exe";
            p2.FilePath = @"C:\Tool2.exe";
            p2.User = "User2";

            Process p3 = new Process();
            p3.ProcessId = 3;
            p3.FileName = "Tool3.exe";
            p3.FilePath = @"C:\Tool3.exe";
            p3.User = "User3";


            ListBox listBox = new ListBox();
            listBox.Items.Add(p1);
            listBox.Items.Add(p2);
            listBox.Items.Add(p3);

            for (int i = 0; i < listBox.Items.Count; i++)
            {
                Process p = (Process)listBox.Items[i]; //Access the value of the item
                Console.WriteLine("Process id: {0}", p.ProcessId);
                Console.WriteLine("Process filename: {0}", p.FileName);
                Console.WriteLine("Process file path: {0}", p.FilePath);
                Console.WriteLine("Process user: {0}", p.User);
            }

            Console.ReadLine();
        }
    }
}

We have a sample class Process with different properties. Each Process is added on ListBox which is later accessed inside the loop.