C#:如何在组合框中显示来自自定义类对象的列表
这是我的班级:C#:如何在组合框中显示来自自定义类对象的列表,c#,list,class,combobox,C#,List,Class,Combobox,这是我的班级: 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,
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;
m_listLanObjects=new List();
comboBox1.DataSource=m_listLanObjects;
comboBox1.DisplayMember=“Name”;
comboBox1.ValueMember=null;
每次我向列表中添加一个新的LAN_对象时,我都会将数据源重置为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;
}
}
}
namespace LAN\u index\u程序
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
m_listLanObjects=新列表();
comboBox1.DataSource=m_listLanObjects;
comboBox1.DisplayMember=“Name”;
comboBox1.ValueMember=null;
}
public void AddLanObject(LAN\u object a\u newLanObject,LAN\u object a\u oldlanobject,bool New=true)
{
如果(新的)
{
m_listLanObjects.Add(a_newLanObject);
}
其他的
{
int index=m_listLanObjects.IndexOf(a_oldlanobject);
m_listLanObjects.RemoveAt(索引);
m_listLanObjects.Insert(索引,a_newLanObject);
}
richTextBox1.Text=“”;
richTextBox1.Text=listToString();
comboBox1.DataSource=m_listLanObjects;
}
}
}
我想从名称(m_deviceName)中选择一个lan_对象。在按下按钮时,您希望在进一步的功能中使用特定的Lan_对象
希望有人能帮上忙,谷歌这一次没有帮上忙。为了尽可能保持当前代码不变,您必须先将
数据源设置为null
,然后再次设置DisplayMember
(因为它被删除),然后再次设置数据源
comboBox1.DataSource = null;
comboBox1.DisplayMember = "Name";
comboBox1.DataSource = m_listLanObjects;
一种更简单的方法是使用绑定列表
而不是列表
,因为这将自动更新数据源
,而无需重置数据源
您将声明绑定列表
,如下所示:
private BindingList<LAN_object> m_listLanObjects = new BindingList<LAN_object>();
public Form1()
{
InitializeComponent();
comboBox1.DataSource = new BindingSource {DataSource = m_listLanObjects};
comboBox1.DisplayMember = "Name";
}
private BindingList m_listLanObjects=new BindingList();
公共表格1()
{
初始化组件();
comboBox1.DataSource=新BindingSource{DataSource=m_listLanObjects};
comboBox1.DisplayMember=“Name”;
}
然后只需将项目添加到集合中,它们就会显示在组合框中
我已经添加了代码。这只会在对话框结束时发生。只需填写一张全班同学的表格。m_listLANobjects是在“public form1{…}”中声明的,包含代码片段的整个代码都被上传。希望你能帮上忙,因为没有人能帮上忙