Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表框不';从列表中添加后,无法更新 编辑_C#_List_Arduino_Listbox - Fatal编程技术网

C# 列表框不';从列表中添加后,无法更新 编辑

C# 列表框不';从列表中添加后,无法更新 编辑,c#,list,arduino,listbox,C#,List,Arduino,Listbox,多亏@omaxel的 BindingList<string> UIDList = new BindingList<string>(); 和@Ming Zhou的 //reset the DataSource lbUID.DataSource = null; lbUID.DataSource = UIDList; 原职 将列表中的项目添加到表单中后,表单中的列表框不会更新。它确实向列表中添加了一个值,整数(beschikbarePlekken)按预期工作。你能帮我吗?

多亏@omaxel的

BindingList<string> UIDList = new BindingList<string>();
和@Ming Zhou的

//reset the DataSource
lbUID.DataSource = null;
lbUID.DataSource = UIDList;

原职 将列表中的项目添加到表单中后,表单中的列表框不会更新。它确实向列表中添加了一个值,整数(beschikbarePlekken)按预期工作。你能帮我吗?这是我的密码

public partial class Form1 : Form
{
    // Variabelen
    SerialPort port;
    int beschikbarePlekken = 255; // Beschikbare parkeerplekken
    string UID = " ";
    List<string> UIDList = new List<string>();

    public Form1()
    {
        InitializeComponent();
        port = new SerialPort("COM12", 9600);
        port.DataReceived += incoming;
        port.Open();
        lbUID.DataSource = UIDList;
    }

    private void incoming(object sender, SerialDataReceivedEventArgs e)
    {
        UID = port.ReadLine().Trim();

        if (UID.Length == 0)
        {
            return;
        }

        UpdateList(UID);
    }

    delegate void SetLabel();

    public void UpdateList(string UID)
    {
        if (!UIDList.Contains(UID) && UIDList.Count < beschikbarePlekken)
        {
            UIDList.Add(UID);
            Console.WriteLine(UID);
            lblPlek.Invoke(new SetLabel(SetLabelMethod));
        }
    }

    void SetLabelMethod()
    {
        lblPlek.Text = "Beschikbare plekken: " + (beschikbarePlekken - UIDList.Count);
    }

}
公共部分类表单1:表单
{
//瓦里亚贝伦
串行端口;
int beschikbarePlekken=255;//Beschikbare parkeerplekken
字符串UID=“”;
List UIDList=新列表();
公共表格1()
{
初始化组件();
端口=新的串行端口(“COM12”,9600);
port.DataReceived+=传入;
port.Open();
lbUID.DataSource=UIDList;
}
私有无效传入(对象发送方,SerialDataReceivedEventArgs e)
{
UID=port.ReadLine().Trim();
如果(UID.Length==0)
{
返回;
}
更新列表(UID);
}
委托void SetLabel();
公共无效更新列表(字符串UID)
{
如果(!UIDList.Contains(UID)&&UIDList.Count
您应该使用而不是
列表
。因此,每当您将项目添加到
UIDList
,列表框都会更新

从Microsoft文档:

BindingList
:提供支持数据绑定的通用集合

UIDList
变量声明/初始化更改为:

BindingList<string> UIDList = new BindingList<string>();

再次尝试设置
列表框的数据源。在
UIDList.Add之后添加(UID)添加
lbUID.DataSource=UIDList@Nino仍然不起作用,很遗憾。很高兴帮助你;)
BindingList<string> UIDList = new BindingList<string>();
if (!UIDList.Contains(UID) && UIDList.Count < beschikbarePlekken)
{
    lbUID.Invoke((MethodInvoker)delegate
    {
        UIDList.Add(UID);
    });

    lblPlek.Invoke(new SetLabel(SetLabelMethod));
}