获取列表框winform中的所有项目

时间:2015-08-03 04:58:27

标签: c# wpf winforms listbox

美好的一天! 从列表框中获取所有项目(无论是否选中)我都遇到了麻烦。每当我点击发送按钮时,我可以获得的唯一项目是我选择的项目(这是我的代码的当前结果:http://imgur.com/jA94Bjm)。我想要的是从文本框中获取所有项目,而不仅仅是从所选项目中获取的项目,而不是重复项目。

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 个答案:

答案 0 :(得分:2)

如果您想要访问listbox中的所有商品,则必须迭代所有商品并访问item的值。以下是如何实现这一目标的示例:

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();
        }
    }
}

我们有一个具有不同属性的示例类Process。每个Process都会添加到ListBox上,稍后会在循环内访问。