如何将所选项目(具有字符串格式)保存到多个文本框中

How to save a selected item (that has string formatting) into multiple textboxes

我目前正在开发一个将项目保存为要执行的任务的系统,其中一个功能是编辑这些任务之一。列表框中的每个项目都是这样格式化和添加的:

listFormat = "{0, -10} {1,-35} {2, -20} {3, -20} {4, -20} {5, -15} {6, -10}";
lstMain.Items.Add(string.Format(listFormat, sName, sSpec, sType, sProgress, sContact, sStart, sEnd));

为了让我单独编辑每个变量,我需要将添加的每个变量放入一个单独的文本框中,但整行是一个项目,所以我不知道该怎么做才能编辑它们。

注意:一旦我可以将列表框中项目的每个部分放入多个文本框中,我就可以将它们添加回去,这没问题,我只需要将它们放在那里。 非常感谢。

您可能会考虑创建一个 Task class,它具有每个项目的属性,并且可能会覆盖 ToString 以输出您的字符串。然后你可以得到其中的一个 BindingList,然后将列表框绑定到它。

这样您就可以轻松地编辑和更新您的列表框。

这里有一个 copy/paste-able 示例,应该会有帮助。 Task class 在最下面:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    // This will hold the items displayed in the ListBox
    private BindingList<Task> taskList;

    // Manually creating the controls here so you can copy/paste
    private ListBox taskListBox;
    private Button btnEdit;

    private void Form1_Load(object sender, EventArgs e)
    {
        // Create a few "Tasks" and add them to our BindingList
        taskList = new BindingList<Task>
        {
            new Task("john", "jSpec", "jType", "jProg",
                "jContact", "jStart", "jEnd"),
            new Task("mary", "mSpec", "mType", "mProg",
                "mContact", "mStart", "mEnd"),
            new Task("luther", "lSpec", "lType", "lProg",
                "lContact", "lStart", "lEnd"),
        };

        // Create the ListBox
        taskListBox = new ListBox
        {
            Width = Width - 50,
            Left = 10,
            Top = 30,
            DataSource = taskList
        };

        Controls.Add(taskListBox);

        // Create the Button
        btnEdit = new Button
        {
            Text = "Edit Task",
            Width = 100,
            Left = taskListBox.Left + taskListBox.Width - 100,
            Top = taskListBox.Top + taskListBox.Height + 10
        };
        btnEdit.Click += BtnEdit_Click;

        Controls.Add(btnEdit);
    }

    // When you select an item in the list box and click the button,
    // the selected item will be automatically updated. You can modify
    // this code to get the actual values from the user for whatever 
    // properties you want the user to be able to update
    private void BtnEdit_Click(object sender, EventArgs e)
    {
        // Pretend we get a value from the user
        var newName = "New Name";
        var newEnd = "New End";

        // Get the selected task
        var selectedTask = taskList[taskListBox.SelectedIndex];

        // Change some of it's property values
        selectedTask.Name = newName;
        selectedTask.End = newEnd;

        // Update the data in the listbox and notify the user
        taskList.ResetBindings();
        MessageBox.Show("Updated selected item");
    }
}

// The Task class, with properties to represent the values from your code sample
public class Task
{
    public string Name { get; set; }
    public string Spec { get; set; }
    public string Type { get; set; }
    public string Progress { get; set; }
    public string Contact { get; set; }
    public string Start { get; set; }
    public string End { get; set; }

    public Task(string name, string spec, string type, string progress,
        string contact, string start, string end)
    {
        Name = name; Spec = spec; Type = type; Progress = progress;
        Contact = contact; Start = start; End = end;
    }

    public override string ToString()
    {
        return $"{Name,-10} {Spec,-35} {Type,-20} {Progress,-20} " +
               $"{Contact,-20} {Start,-15} {End,-10}";
    }
}

现在回答有关将项目放入文本框的问题 - 这很简单。在列表框的选择更改事件中,您可以获取所选任务(请参阅按钮单击事件中的示例代码),并将每个文本框设置为所选任务的属性之一。

然后在按钮单击事件中,我们已经获得了选定的 Task,因此您所要做的就是将每个 属性 设置为每个文本框中的正确值。