You need to enable JavaScript to run this app. C# 多独立平行列表如何同步删除匹配UID的关联条目
优惠活动
大模型
产品
解决方案
定价
更多

C# 多独立平行列表如何同步删除匹配UID的关联条目

多列表同步删除关联数据的解决方案

方案1:快速适配现有独立列表结构

你当前的核心问题是:删除时仅操作了UID列表,未获取目标元素的索引同步删除另外两个关联列表的同位置条目,且删除后未刷新回显文本框。
修改btnVerwijder_Click的逻辑即可解决,步骤如下:

  • 统一UID的存储类型,避免添加用Int16、删除用Int32的转换不匹配问题
  • 先查找待删除UID在UID列表中的索引,而非直接删值
  • 索引有效时,三个列表同时按索引删除条目
  • 删除完成后重新刷新三个回显文本框的内容

修改后的删除逻辑代码:

private void btnVerwijder_Click(object sender, EventArgs e)
{
    // 处理用户输入非数字的异常,避免程序崩溃
    if (!short.TryParse(txtVerwijderen.Text, out short deleteUID))
    {
        MessageBox.Show("请输入有效的数字UID");
        return;
    }

    // 查找待删除UID的索引
    int deleteIndex = UID.IndexOf(deleteUID);
    
    // 索引为-1代表未找到对应条目
    if (deleteIndex == -1)
    {
        MessageBox.Show("未找到对应UID的条目");
        return;
    }

    // 三个列表同步删除同索引条目
    UID.RemoveAt(deleteIndex);
    DESCRBUTTON.RemoveAt(deleteIndex);
    ARDUINOCONTACT.RemoveAt(deleteIndex);

    // 删除完成后刷新回显文本框(原代码遗漏了这步)
    txtUID.Text = String.Join(Environment.NewLine, UID);
    txtDescriptionButton.Text = String.Join(Environment.NewLine, DESCRBUTTON);
    txtArduinoContact.Text = String.Join(Environment.NewLine, ARDUINOCONTACT);

    // 清空删除输入框
    txtVerwijderen.Text = string.Empty;
}

方案2:更规范的长期优化(避免多列表错位隐患)

用三个独立列表存储关联属性本质上是把同一个条目的三个字段拆成了平行结构,后续很容易出现错位问题。更稳妥的做法是把关联字段封装为自定义类,仅维护一个类对象列表,从根源上避免不同步:

  1. 先定义条目实体类:
// 定义库存条目类,封装三个关联字段
public class InventoryEntry
{
    public short UID { get; set; }
    public string DescriptionButton { get; set; }
    public short ArduinoContact { get; set; }
}
  1. 替换原三个独立列表为单个实体列表:
// 原UID、DESCRBUTTON、ARDUINOCONTACT三个列表替换为这一个
List<InventoryEntry> inventoryList = new List<InventoryEntry>();
  1. 原添加逻辑修改为给列表新增实体实例:
// 原添加逻辑替换
else
{
    inventoryList.Add(new InventoryEntry()
    {
        UID = Convert.ToInt16(txtInvUID.Text),
        DescriptionButton = txtInvDescriptionButton.Text,
        ArduinoContact = Convert.ToInt16(txtInvArduinoContact.Text)
    });

    // 回显时从实体列表提取对应字段拼接
    txtUID.Text = String.Join(Environment.NewLine, inventoryList.Select(item => item.UID));
    txtDescriptionButton.Text = String.Join(Environment.NewLine, inventoryList.Select(item => item.DescriptionButton));
    txtArduinoContact.Text = String.Join(Environment.NewLine, inventoryList.Select(item => item.ArduinoContact));

    // 清空输入框逻辑不变
    txtInvUID.Text = string.Empty;
    txtInvDescriptionButton.Text = string.Empty;
    txtInvArduinoContact.Text = string.Empty;
}
  1. 删除逻辑更简洁,仅需匹配实体的UID属性删除即可,不会出现错位:
private void btnVerwijder_Click(object sender, EventArgs e)
{
    if (!short.TryParse(txtVerwijderen.Text, out short deleteUID))
    {
        MessageBox.Show("请输入有效的数字UID");
        return;
    }

    // 直接匹配删除对应实体,无需关心索引
    inventoryList.RemoveAll(item => item.UID == deleteUID);

    // 刷新回显即可
    txtUID.Text = String.Join(Environment.NewLine, inventoryList.Select(item => item.UID));
    txtDescriptionButton.Text = String.Join(Environment.NewLine, inventoryList.Select(item => item.DescriptionButton));
    txtArduinoContact.Text = String.Join(Environment.NewLine, inventoryList.Select(item => item.ArduinoContact));

    txtVerwijderen.Text = string.Empty;
}

内容的提问来源于stack exchange,提问作者Aeiral

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.05 10:45:06