C#:如何在组合框中显示来自自定义 class 对象的列表
C#: how to show list from custom class objects in a combobox
这是我的 class:
namespace LAN_index_program
{
public class LAN_object
{
public string Name { get; set; }
public LAN_object(
DEVICETYPE a_type,
string a_deviceName,
string a_deviceManufacure,
string a_deviceOwner,
string a_deviceIp,
string a_deviceMac,
DateTime a_deviceInstalationDate
)
{
m_deviceType = a_type;
m_deviceName = a_deviceName;
m_deviceManufacure = a_deviceManufacure;
m_deviceOwner = a_deviceOwner;
m_deviceIp = a_deviceIp;
m_deviceMac = a_deviceMac;
m_deviceInstalationDate = a_deviceInstalationDate;
Name = a_deviceName;
}
// public string a_deviceName
// {
// get
// {
// return m_deviceName;
// }
// }
public enum DEVICETYPE
{
Router,
Switch,
Access_point,
Laptop,
Phone,
Computer
};
public string toString()
{
string lan_onbjectstring = "";
lan_onbjectstring = m_deviceType.ToString() + "\r\n";
lan_onbjectstring += m_deviceName + "\r\n";
lan_onbjectstring += m_deviceManufacure + "\r\n";
lan_onbjectstring += m_deviceOwner + "\r\n";
lan_onbjectstring += m_deviceIp + "\r\n";
lan_onbjectstring += m_deviceMac + "\r\n";
lan_onbjectstring += m_deviceInstalationDate.ToShortDateString() + "\r\n";
return lan_onbjectstring;
}
public override string ToString()
{
return m_deviceName;
}
DEVICETYPE m_deviceType;
string m_deviceName;
string m_deviceManufacure;
string m_deviceOwner;
string m_deviceIp;
string m_deviceMac;
DateTime m_deviceInstalationDate;
}
}
这是我认为可以做到的代码:
m_listLanObjects = new List<LAN_object>();
comboBox1.DataSource = m_listLanObjects;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = null;
每次我向列表添加新的 LAN_object 时,我都会将数据源重置为 m_listlanobjects。但是组合框没有填满。
namespace LAN_index_program
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
m_listLanObjects = new List<LAN_object>();
comboBox1.DataSource = m_listLanObjects;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = null;
}
public void AddLanObject(LAN_object a_newLanObject, LAN_object a_oldlanobject, bool New = true)
{
if (New)
{
m_listLanObjects.Add(a_newLanObject);
}
else
{
int index = m_listLanObjects.IndexOf(a_oldlanobject);
m_listLanObjects.RemoveAt(index);
m_listLanObjects.Insert(index, a_newLanObject);
}
richTextBox1.Text = "";
richTextBox1.Text = listToString();
comboBox1.DataSource = m_listLanObjects;
}
}
}
我想从它的名字中选择一个 lan_object (m_deviceName)。并在按下按钮时想要在进一步的功能中使用该特定 Lan_object。
希望有人能帮忙,google一次没帮上忙。
为了尽可能多地保持当前代码相同,您必须先将 DataSource
设置为 null
,然后再次设置 DisplayMember
(因为它被删除),然后再次设置你的DataSource
。
comboBox1.DataSource = null;
comboBox1.DisplayMember = "Name";
comboBox1.DataSource = m_listLanObjects;
一种更简单的方法是使用 BindingList<T>
而不是 List<T>
,因为这将自动更新您的 DataSource
,而无需重置 DataSource
.
您将 BindingList
声明如下:
private BindingList<LAN_object> m_listLanObjects = new BindingList<LAN_object>();
public Form1()
{
InitializeComponent();
comboBox1.DataSource = new BindingSource {DataSource = m_listLanObjects};
comboBox1.DisplayMember = "Name";
}
然后只需将项目添加到集合中,它们就会显示在 ComboBox
。
这是我的 class:
namespace LAN_index_program
{
public class LAN_object
{
public string Name { get; set; }
public LAN_object(
DEVICETYPE a_type,
string a_deviceName,
string a_deviceManufacure,
string a_deviceOwner,
string a_deviceIp,
string a_deviceMac,
DateTime a_deviceInstalationDate
)
{
m_deviceType = a_type;
m_deviceName = a_deviceName;
m_deviceManufacure = a_deviceManufacure;
m_deviceOwner = a_deviceOwner;
m_deviceIp = a_deviceIp;
m_deviceMac = a_deviceMac;
m_deviceInstalationDate = a_deviceInstalationDate;
Name = a_deviceName;
}
// public string a_deviceName
// {
// get
// {
// return m_deviceName;
// }
// }
public enum DEVICETYPE
{
Router,
Switch,
Access_point,
Laptop,
Phone,
Computer
};
public string toString()
{
string lan_onbjectstring = "";
lan_onbjectstring = m_deviceType.ToString() + "\r\n";
lan_onbjectstring += m_deviceName + "\r\n";
lan_onbjectstring += m_deviceManufacure + "\r\n";
lan_onbjectstring += m_deviceOwner + "\r\n";
lan_onbjectstring += m_deviceIp + "\r\n";
lan_onbjectstring += m_deviceMac + "\r\n";
lan_onbjectstring += m_deviceInstalationDate.ToShortDateString() + "\r\n";
return lan_onbjectstring;
}
public override string ToString()
{
return m_deviceName;
}
DEVICETYPE m_deviceType;
string m_deviceName;
string m_deviceManufacure;
string m_deviceOwner;
string m_deviceIp;
string m_deviceMac;
DateTime m_deviceInstalationDate;
}
}
这是我认为可以做到的代码:
m_listLanObjects = new List<LAN_object>();
comboBox1.DataSource = m_listLanObjects;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = null;
每次我向列表添加新的 LAN_object 时,我都会将数据源重置为 m_listlanobjects。但是组合框没有填满。
namespace LAN_index_program
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
m_listLanObjects = new List<LAN_object>();
comboBox1.DataSource = m_listLanObjects;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = null;
}
public void AddLanObject(LAN_object a_newLanObject, LAN_object a_oldlanobject, bool New = true)
{
if (New)
{
m_listLanObjects.Add(a_newLanObject);
}
else
{
int index = m_listLanObjects.IndexOf(a_oldlanobject);
m_listLanObjects.RemoveAt(index);
m_listLanObjects.Insert(index, a_newLanObject);
}
richTextBox1.Text = "";
richTextBox1.Text = listToString();
comboBox1.DataSource = m_listLanObjects;
}
}
}
我想从它的名字中选择一个 lan_object (m_deviceName)。并在按下按钮时想要在进一步的功能中使用该特定 Lan_object。
希望有人能帮忙,google一次没帮上忙。
为了尽可能多地保持当前代码相同,您必须先将 DataSource
设置为 null
,然后再次设置 DisplayMember
(因为它被删除),然后再次设置你的DataSource
。
comboBox1.DataSource = null;
comboBox1.DisplayMember = "Name";
comboBox1.DataSource = m_listLanObjects;
一种更简单的方法是使用 BindingList<T>
而不是 List<T>
,因为这将自动更新您的 DataSource
,而无需重置 DataSource
.
您将 BindingList
声明如下:
private BindingList<LAN_object> m_listLanObjects = new BindingList<LAN_object>();
public Form1()
{
InitializeComponent();
comboBox1.DataSource = new BindingSource {DataSource = m_listLanObjects};
comboBox1.DisplayMember = "Name";
}
然后只需将项目添加到集合中,它们就会显示在 ComboBox
。