简体   繁体   English

Winform 上的文本框不显示我的数据 C#

[英]TextBox on Winform not showing my Data C#

I'm creating a Windows form where it should list the COM Ports on my PC.我正在创建一个 Windows 表单,它应该在我的 PC 上列出 COM 端口。 I'm trying to add this data to a ComBox, where it will list/display all my COM Ports and the details of each one.我正在尝试将此数据添加到 ComBox,它将列出/显示我所有的 COM 端口和每个端口的详细信息。 This is the code.这是代码。

    private void cb1_SelectedIndexChanged(object sender, EventArgs e)
    {
        using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'"))
        {
            var portnames = SerialPort.GetPortNames();
            var ports = searcher.Get().Cast<ManagementBaseObject>().ToList().Select(p => p["Caption"].ToString());

            var portList = portnames.Select(n => n + " - " + ports.FirstOrDefault(s => s.Contains(n))).ToList();

            foreach (string s in portList)
            {
                cb1.Text = s;
            }
        }
    }

This code works perfectly in a Console App, it shows exactly what I need.此代码在控制台应用程序中完美运行,它准确显示了我需要的内容。 But in the Windows Form, it is not working.但是在 Windows 表格中,它不起作用。 I have googled some possible solutions for this, but none of them worked.我已经为此搜索了一些可能的解决方案,但没有一个有效。 I am not an expert on that, but I believe this error could be something related with the Data Bindings, I tried to remove the Data Bindings but I couldn't.我不是这方面的专家,但我相信这个错误可能与数据绑定有关,我试图删除数据绑定但我不能。

cb1.DataSource = null; cb1.DataSource = null; or cb1.Items.Clear();或 cb1.Items.Clear();

These were just two of many things that I tried but none worked.这些只是我尝试过的许多事情中的两件事,但都没有奏效。

You are currently not adding any items to the ComboBox.您目前没有向 ComboBox 添加任何项目。 Check out the modified code.查看修改后的代码。

private void cb1_SelectedIndexChanged(object sender, EventArgs e)
{
    using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'"))
    {
        var portnames = SerialPort.GetPortNames();
        var ports = searcher.Get().Cast<ManagementBaseObject>().ToList().Select(p => p["Caption"].ToString());

        var portList = portnames.Select(n => n + " - " + ports.FirstOrDefault(s => s.Contains(n))).ToList();

        foreach (string s in portList)
        {
            cb1.Items.Add(s); //THIS LINE CHANGED
        }
    }
}

You want to add the available COM Ports to the ComboBox so you have to add a new Item for every COM Port.您想将可用的 COM 端口添加到 ComboBox,因此您必须为每个 COM 端口添加一个新项目。 This items can be selected by the user after they are inserted.这些项目可以在插入后由用户选择。

EDIT: To run the code when opening your form you should do something like this:编辑:要在打开表单时运行代码,您应该执行以下操作:

public Form1() {
  InitializeComponent();

  using(var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'")) {
    var portnames = SerialPort.GetPortNames();
    var ports = searcher.Get().Cast<ManagementBaseObject>().ToList().Select(p => p ["Caption"]
                                                                                     .ToString());

    var portList = portnames.Select(n => n + " - " + ports.FirstOrDefault(s => s.Contains(n))).ToList();

    foreach (string s in portList) {
      cb1.Items.Add(s);  // THIS LINE CHANGED
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM