c# - 获取winform列表框的所有项目

标签 c# wpf winforms listbox

美好的一天! 我无法从列表框中获取所有项目(无论是否选中)。每当我单击发送按钮时,我可以获得的唯一项目就是我选择的项目(这是下面代码的当前结果: /image/kvbbb.jpg )。我想要的是从文本框中获取所有项目,而不仅仅是从选定的项目中获取并且不重复。

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

最佳答案

如果您想访问列表框中的所有项目,则必须迭代所有项目并访问该项目的值。以下是如何实现此目的的示例:

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上,稍后在循环内访问。

关于c# - 获取winform列表框的所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31779682/

相关文章:

c# - 如何使用自定义 IPasswordHasher?

c# - 有没有办法通过 C# 中的结尾子字符串对字符串列表进行排序?

wpf - 使用 WPF 在 F# 中调用窗口加载事件的方法

c# - 您所见过的最好的WPF开源项目

c# - 更新 ObservableCollection 对象更改的值

c# - .NET 应用程序使用 PHP/MYSQL 进行身份验证?

c# - 使用 key 大小小于 2048 的 RSA 安全 key 创建 JWT token 时出错

c# - 无法转换 Combobox.SelectedValue

c# - DataGridView.Rows.RemoveAt(int index) 不会删除 Windows 窗体中的行?

c# - CopyFromScreen 是获取屏幕截图的正确方法吗?