我如何在ComboBox控件中实现它只显示每个项目名称的一部分?

时间:2015-04-16 13:27:54

标签: c# .net winforms

这是我列出并将所有win32项添加到ComboBox的方法。

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我只想在ComboBox中看到:NetworkAdapter 但是当我选择项目时,它应该被选为Win32_NetworkAdapter,但是用户应该只看到并选择NetworkAdapter。

我想从ComboBox项目中删除Win32_作为测试 但只有用户会在没有Win32_

的情况下看到它

当我正在做的时候也在构造函数中:cmbxNetwork.SelectedItem =“Win32_NetworkAdapter”;所以相反,如果我正在做:cmbxNetwork.SelectedItem =“NetworkAdapter”;这就够了。该程序将使用“Win32_NetworkAdapter”;但我再次看到ComboBox中的用户和项目只是NetworkAdapter

1 个答案:

答案 0 :(得分:0)

您可以使用自编写结构中的对象来封装值和显示字符串:

class Box {
    public object Value { get; set; }
    public string Display { get; set; }

    public override string ToString() { return Display; }
}

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