更新 List<t> 对象及其属性

Update a List<t> object and his properties

我有一个列表 MemoryClienti,其中的项目基于 ClienteModel class。

我用来添加新的项目MemoryClienti方法是:

        public bool CreateCliente(ClienteModel model)
    {
        bool empty = !MemoryClienti.Any();
        if (empty)
        {
            ClienteModel clienteModel = new ClienteModel();
            clienteModel.Cognome = model.Cognome;
            clienteModel.Nome = model.Nome;
            clienteModel.Indirizzo = model.Indirizzo;
            clienteModel.IDCliente = StartID;
            MemoryClienti.Add(clienteModel);
            MessageBox.Show("Cliente aggiunto correttamente.");
        }
        else
        {
            int maxID = MemoryClienti.Count;
            ClienteModel clienteModel = new ClienteModel();
            clienteModel.Cognome = model.Cognome;
            clienteModel.Nome = model.Nome;
            clienteModel.Indirizzo = model.Indirizzo;
            clienteModel.IDCliente = maxID;
            MemoryClienti.Add(clienteModel);
            MessageBox.Show("Cliente aggiunto correttamente.");
        }
        return true;

此方法使我能够添加新项目,计算列表中项目的数量,并将新项目的 ID 设置为计数结果,因此我添加的每个项目都会发生这种情况,而且它是正在工作。

项目“模型”的数据 来自表单的文本框:

        private void aggiungiClienteButton_Click(object sender, EventArgs e)
    {
        if (cognomeTextBox.Text == "")
        {
            MessageBox.Show("Uno o più campi sono vuoti");
        }
        else if (nomeTextBox.Text=="")
        {
            MessageBox.Show("Uno o più campi sono vuoti");
        }
        else if (indirizzoTextbox.Text == "")
        {
            MessageBox.Show("Uno o più campi sono vuoti");
        }
        else
        {
            clienteModel.Cognome = cognomeTextBox.Text;
            clienteModel.Nome = nomeTextBox.Text;
            clienteModel.Indirizzo = indirizzoTextbox.Text;
            dbMemoryManager.CreateCliente(clienteModel);
            cognomeTextBox.Text = String.Empty;
            nomeTextBox.Text = String.Empty;
            indirizzoTextbox.Text = String.Empty;
        }
    }

我的class是:

    public class ClienteModel
{
    public int IDCliente { get; set; }
    public string Cognome { get; set; }
    public string Nome { get; set; }
    public string Indirizzo { get; set; }

}

问题是:如何在不更改 ID 的情况下使用文本框更新 一个 项目?

这是一个快速但肮脏的解决方案。您没有指定您使用的是哪种文本框。我假设它是 Windows 表格。

我修改了您的 ClienteModel,使其看起来像这样:

public class ClienteModel
{
    private static int _currentId = 0;
    public int IDCliente { get; set; } = _currentId++;
    public string Cognome { get; set; }
    public string Nome { get; set; }
    public string Indirizzo { get; set; }

    public override string ToString()
    {
        return Nome;
    }
}

请注意,它现在管理 IDCliente 字段并且它有一个 ToString 成员(您可以将其设置为您想要的任何字符串)。您可能希望在表单的 read-only 文本框中显示 IDCliente 字段。

然后我创建了一个简单的 Windows Forms 表单,其中包含三个文本框、一个名为 ModelsListBox 的列表框和两个按钮 AddButton(标题:“添加”)和 UpdateButton(“更新”)。

在表单 class 中,我创建了一个小的验证方法(因为我在两个地方使用了它)。请注意,即使您有多个错误,您也只会得到一个 MessageBox:

private bool ValidateFields()
{
    var errors = new List<string>();
    foreach (var tb in new[] {cognomeTextBox, nomeTextBox, indirizzoTextbox})
    {
        if (string.IsNullOrWhiteSpace(tb.Text))
        {
            errors.Add($"{tb.Name} must not be empty");
        }
    }

    if (errors.Count > 0)
    {
        MessageBox.Show(string.Join(Environment.NewLine, errors), "Errors", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return false;
    }
    //otherwise
    return true;
}

然后我添加了三个事件处理程序(在设计器中以正常方式连接它们)。第一个是按下添加按钮时:

private void AddButton_Click(object sender, EventArgs e)
{
    if (!ValidateFields())
    {
        return;
    }

    var model = new ClienteModel
    {
        Cognome = cognomeTextBox.Text,
        Nome = nomeTextBox.Text,
        Indirizzo = indirizzoTextbox.Text,
    };
    ModelsListBox.Items.Add(model);
}

它创建一个新的 ClienteModel 并将其添加到列表框(假设验证通过)。

然后,我创建了一个处理程序,只要列表框中的选择发生变化,它就会更新文本框:

private void ModelsListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ModelsListBox.SelectedItem is ClienteModel model)
    {
        cognomeTextBox.Text = model.Cognome;
        nomeTextBox.Text = model.Nome;
        indirizzoTextbox.Text = model.Indirizzo;
    }
}

最后,更新按钮处理程序:

private void UpdateButton_Click(object sender, EventArgs e)
{
    if (!ValidateFields())
    {
        return;
    }
    if (ModelsListBox.SelectedItem is ClienteModel model)
    {
        model.Cognome = cognomeTextBox.Text;
        model.Nome = nomeTextBox.Text;
        model.Indirizzo = indirizzoTextbox.Text;
    }
}

这并不完美。在做出选择之前,您应该禁用“更新”按钮(并且可能仅在文本框中进行更改后才启用)。

更重要的是,项目列表框中显示的字符串基于项目首次添加到列表时调用 ClienteModel.ToString 的结果。如果更改用于计算 .ToString 的字段的值,则列表框不会更新。有几种解决方法(可在 Stack Overflow 上找到),但我认为这足以让您入门。