如何在组合框控件中做到这一点,它将仅显示每个项目名称的一部分



这就是我列出所有 win32 项目并将其添加到组合框的方式。

using System;
using System.Collections;
using System.Management;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace GetHardwareInfo
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            cmbxOption.SelectedItem = "Win32_Processor";
            cmbxStorage.SelectedItem = "Win32_DiskDrive";
            cmbxMemory.SelectedItem = "Win32_CacheMemory";
            cmbxSystemInfo.SelectedItem = "";
            cmbxNetwork.SelectedItem = "Win32_NetworkAdapter";
            cmbxUserAccount.SelectedItem = "Win32_SystemUsers";
            cmbxDeveloper.SelectedItem = "Win32_COMApplication";
            cmbxUtility.SelectedItem = "Win32_1394Controller";

        }
        private void InsertInfo(string Key, ref ListView lst, bool DontInsertNull)
        {
            lst.Items.Clear();
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + Key);
            try
            {
                foreach (ManagementObject share in searcher.Get())
                {
                    ListViewGroup grp;
                    try
                    {
                        grp = lst.Groups.Add(share["Name"].ToString(), share["Name"].ToString());
                    }
                    catch
                    {
                        grp = lst.Groups.Add(share.ToString(), share.ToString());
                    }
                    if (share.Properties.Count <= 0)
                    {
                        MessageBox.Show("No Information Available", "No Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    foreach (PropertyData PC in share.Properties)
                    {
                        ListViewItem item = new ListViewItem(grp);
                        if (lst.Items.Count % 2 != 0)
                            item.BackColor = Color.White;
                        else
                            item.BackColor = Color.WhiteSmoke;
                        item.Text = PC.Name;
                        if (PC.Value != null && PC.Value.ToString() != "")
                        {
                            switch (PC.Value.GetType().ToString())
                            {
                                case "System.String[]":
                                    string[] str = (string[])PC.Value;
                                    string str2 = "";
                                    foreach (string st in str)
                                        str2 += st + " ";
                                    item.SubItems.Add(str2);
                                    break;
                                case "System.UInt16[]":
                                    ushort[] shortData = (ushort[])PC.Value;

                                    string tstr2 = "";
                                    foreach (ushort st in shortData)
                                        tstr2 += st.ToString() + " ";
                                    item.SubItems.Add(tstr2);
                                    break;
                                default:
                                    item.SubItems.Add(PC.Value.ToString());
                                    break;
                            }
                        }
                        else
                        {
                            if (!DontInsertNull)
                                item.SubItems.Add("No Information available");
                            else
                                continue;
                        }
                        lst.Items.Add(item);
                    }
                }
            }

            catch (Exception exp)
            {
                MessageBox.Show("can't get data because of the followeing error n" + exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
        private void RemoveNullValue(ref ListView lst)
        {
            foreach (ListViewItem item in lst.Items)
                if (item.SubItems[1].Text == "No Information available")
                    item.Remove();
        }

        #region Control events ...
        private void cmbxNetwork_SelectedIndexChanged(object sender, EventArgs e)
        {
            InsertInfo(cmbxNetwork.SelectedItem.ToString(), ref lstNetwork, chkNetwork.Checked);
        }
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            linkLabel1.LinkVisited = true;
        }
        #endregion

    }
}
最后,我得到的是,在这种情况下,

在ComboBox中,我称之为cmbxNetwork,我在其中看到许多项目都以Win32_开头例如,当我单击ComboBox时,我看到第一项是:"Win32_NetworkAdapter"

而不是Win32_NetworkAdapter我只想在组合框中看到:网络适配器但是当我选择该项目时,它应该被选为Win32_NetworkAdapter但用户应该只看到并选择网络适配器。

我想从组合框项目中删除Win32_作为测试但只有用户会看到它而不会Win32_

同样在构造函数中,当我现在做的时候:cmbxNetwork.SelectedItem = "Win32_NetworkAdapter";所以如果我正在做:cmbxNetwork.SelectedItem = "NetworkAdapter";这就足够了。程序将使用"Win32_NetworkAdapter";但我在组合框中看到和用户作为项目只是网络适配器

您可以使用来自自写结构的对象,该结构封装了一个值和一个显示字符串:

class Box {
    public object Value { get; set; }
    public string Display { get; set; }
    public override string ToString() { return Display; }
}

将值放入 Box 类中并定义显示字符串。ComboBox将显示ToString的结果。

最新更新