這是我如何列出並將所有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
}
}
我所得到的是,在這種情況下的組合框我把它稱爲最終cmbxNetwork我在它的許多項目都開始以Win32_ 例如當我點擊組合框,我看到的第一個項目是看: 「Win32_NetworkAdapter」
相反Win32_NetworkAdapter我想只有組合框看到:NetworkAdapter 但是,當我選擇它應該被選爲Win32_NetworkAdapter的項目,但用戶應該看到並僅選擇NetworkAdapter。
我想刪除從Win32_ 但只有用戶將看到它沒有Win32_
而且在構造函數中,當我現在在做組合框項目的測試:cmbxNetwork.SelectedItem =「Win32_NetworkAdapter」;所以相反,如果我在做:cmbxNetwork.SelectedItem =「NetworkAdapter」;這將是足夠的。該程序將使用「Win32_NetworkAdapter」;但同樣我看到的用戶在ComboBox的產品只有NetworkAdapter
'public string Display {get {return Value.Substring(0,5); }}'這會截斷'Value'的值。當然,你需要在得到子字符串if(Value.Length> 5){return Value.Substring(0,5); }'。 –
或者,您可以使用'Value.ToString()。替換(「Win32_」,「」)'來刪除前綴。 –
返回顯示給我錯誤我盒類。在返回返回顯示我得到:錯誤因爲'GetHardwareInfo.frmMain.Box.ToString()'返回void,一個返回關鍵字不能跟一個對象表達式 –