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



我当前正在处理一个将项目保存为要执行任务的系统,其中一个功能是编辑其中一个任务。列表框中的每个项目都是格式的,并添加了这样的添加:

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类,并且可能是ToString的覆盖,以输出字符串。然后,您可以拥有其中的BindingList,然后将ListBox绑定到它。

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

这是一个应该有所帮助的副本/粘贴样本。Task类位于底部:

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,因此您要做的就是将每个属性设置为每个文本框的正确值。

最新更新