从列表框winform中获取所有项目 [英] Getting all the items from the listbox winform

查看:96
本文介绍了从列表框winform中获取所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

如果要访问listbox中的所有项目,则必须迭代所有项目并访问该item的值.这是一个如何实现此目的的示例:

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

我们有一个具有不同属性的示例类Process.每个Process都添加到ListBox上,以后可以在循环内部进行访问.

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

这篇关于从列表框winform中获取所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆