lundi 20 avril 2015

How can i make that in the ComboBox control it will show only part of each item name?

This is how i List and Add all the win32 items to the 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


    }
}

What i get is in the end that in the ComboBox in this case i call it cmbxNetwork i see in it many items all start with Win32_ For example when i click on ComboBox i see the first item is: "Win32_NetworkAdapter"

Instead Win32_NetworkAdapter i want to see in the ComboBox only: NetworkAdapter But when i select the item it should be selected as Win32_NetworkAdapter but the user should see and select only NetworkAdapter.

I want to remove as test from the ComboBox items the Win32_ But only that the user will see it without Win32_

Also in the constructor when i'm doing now: cmbxNetwork.SelectedItem = "Win32_NetworkAdapter"; so instead if i'm doing: cmbxNetwork.SelectedItem = "NetworkAdapter"; it will be enough. THe program will use the "Win32_NetworkAdapter"; but again what i see and user in the ComboBox as item is only the NetworkAdapter

Aucun commentaire:

Enregistrer un commentaire