【问题标题】:friendly port name combobox c# Windows Form友好的端口名称组合框 c# Windows Form
【发布时间】:2017-03-14 07:19:09
【问题描述】:

我有一个关于友好名称的问题我阅读了很多(真的很多)关于如何获得友好名称的问题。我什至使用了 WMICodeCreator 我有一个代码要放。但实际上我不明白如何将它添加到我的组合框菜单中。这是我的代码。实际上,这个显示的是计算机上可用的 COM 端口,但我想显示这个的友好名称,以使我的应用程序更易于使用。如何显示友好名称而不是“COMx”? 我在 C# 上使用 Windows 窗体应用程序。

    public partial class Form1 : Form
{
    private SerialPort myport;
    public Form1()
    {
        InitializeComponent();
        myport = new SerialPort();
    }

    private void portbox_DropDown(object sender, EventArgs e)
    {
        string[] ports = SerialPort.GetPortNames();

        portbox.Items.Clear();

        foreach (string port in ports)
        {
           portbox.Items.Add(port);
        }
    }

    private void butvalidate_Click(object sender, EventArgs e)
    {
        myport.BaudRate = 9600;
        myport.PortName = portbox.SelectedItem.ToString();
        myport.Open();
    }

提前感谢您的帮助

【问题讨论】:

  • 试试this
  • 我已经使用了所有页面,所有代码,尝试添加很多行,移动,更改所有压缩所有我能阅读的内容,但没有做任何我想做的事情,它充满了错误消息。尤其是ManagementObjectSearcher

标签: c# combobox windows-forms-designer


【解决方案1】:

这是一个老问题,但在我搜索相关内容时出现了,因此我将发布用于此目的的代码。

我从 SerialPort 获取端口名称列表,然后将它们与从 WMI 查询中检索到的友好名称列表进行匹配,并将它们放入字典中,端口名称作为键,友好名称作为值。然后我使用这个 Dictionary 作为 ComboBox 的 BindingSource。还有其他方法可以做到这一点,但我发现这种方法简单明了。

这个术语有点混乱,因为 ComboBox 有一个用于显示文本的 DisplayMember 和一个作为 SelectedValue 提供的 ValueMember。 DisplayMember 是字典“Value”,ValueMember 是字典“Key”。

    using System.Management;
    private void getAvailablePorts()
    {
        // Get friendly names to go along with the actual portNames
        using (var searcher = new ManagementObjectSearcher("SELECT * FROM WIN32_SERIALPORT"))
        {
           var portDictionary = new Dictionary<string, string>();
           var portNames = SerialPort.GetPortNames().OrderByDescending(s => s.GetAlphaNumericOrderToken()).ToArray<object>();
           var portList = searcher.Get().Cast<ManagementBaseObject>().ToList();

            foreach( var portName in portNames)
            {
                // WMI does not always find all com ports so provide a null alternative
                var portDesc = portList.Where(p => p["DeviceID"].ToString() == portName.ToString()).Select(q => q["Caption"].ToString()).FirstOrDefault() ?? portName.ToString();
                portDictionary.Add(portName.ToString(), portDesc);
            }
            portComboBox.DataSource = new BindingSource(portDictionary, null);
            portComboBox.DisplayMember = "Value";
            portComboBox.ValueMember = "Key";

            // I set my comboBox Selected entry to be the one with a friendly name 
            // beginning with "Teensy" (of course yours will be different, 
            // or you can leave this out)
            portComboBox.SelectedIndex = portComboBox.FindString("Teensy");  // -1 if not found
            if (portComboBox.SelectedIndex < 0)
                portComboBox.Selected = 0;
        }
    }

用法:

            // Since the ComboBox text is now different from the value, 
            // you have to specifically get the SelectedValue of the ComboBox 
            // rather than just its text. 
            var _port = new SerialPort(portComboBox.SelectedValue.ToString());

我希望这对某人有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-18
    • 2012-03-11
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    相关资源
    最近更新 更多