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

C# 如何将选定项目(具有字符串格式)保存到多个文本框中,c#,listbox,string-formatting,C#,Listbox,String Formatting,我目前正在开发一个将项目保存为任务的系统,其中一个功能是编辑这些任务之一。列表框中的每个项目的格式和添加方式如下: 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)); 为了使我能够单独编辑每

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

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));
为了使我能够单独编辑每个变量,我需要将添加到单独文本框中的每个变量都放在一个单独的文本框中,但整行都是一个项目,因此我不知道如何才能编辑它们

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

你可以考虑创建一个<代码>任务类,每个类都有属性,也可以覆盖输出字符串的<代码> toString 。然后您可以拥有一个
bindingslist
,然后将列表框绑定到其中

通过这种方式,您可以轻松编辑和更新列表框

这里有一个可复制/粘贴的示例,应该会有所帮助。
任务
类位于底部:

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}";
    }
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
//这将保存列表框中显示的项目
私有绑定列表任务列表;
//在此处手动创建控件,以便可以复制/粘贴
私有列表框任务列表框;
私人按钮btnEdit;
私有void Form1\u加载(对象发送方、事件参数e)
{
//创建一些“任务”,并将它们添加到绑定列表中
taskList=新绑定列表
{
新任务(“john”、“jSpec”、“jType”、“jProg”,
“jContact”、“jStart”、“jEnd”),
新任务(“mary”、“mSpec”、“mType”、“mProg”,
“mContact”、“mStart”、“mEnd”),
新任务(“路德”、“lSpec”、“lType”、“lProg”,
“lContact”、“lStart”、“lEnd”),
};
//创建列表框
taskListBox=新列表框
{
宽度=宽度-50,
左=10,
Top=30,
数据源=任务列表
};
控件。添加(任务列表框);
//创建按钮
btnEdit=新按钮
{
Text=“编辑任务”,
宽度=100,
左=任务列表框。左+任务列表框。宽度-100,
顶部=任务列表框。顶部+任务列表框。高度+10
};
点击+=btnEdit\u点击;
控件。添加(btnEdit);
}
//在列表框中选择项目并单击按钮时,
//所选项目将自动更新。您可以修改
//这段代码用于从用户处获取实际值
//您希望用户能够更新的属性
私有void BtnEdit_单击(对象发送方,事件参数e)
{
//假设我们从用户那里得到了一个值
var newName=“新名称”;
var newEnd=“newEnd”;
//获取所选任务
var selectedTask=taskList[taskListBox.SelectedIndex];
//更改它的某些属性值
选择任务。名称=新名称;
选择任务。结束=新结束;
//更新列表框中的数据并通知用户
taskList.ResetBindings();
MessageBox.Show(“更新的选定项”);
}
}
//任务类,其属性表示代码示例中的值
公开课任务
{
公共字符串名称{get;set;}
公共字符串规范{get;set;}
公共字符串类型{get;set;}
公共字符串进度{get;set;}
公共字符串联系人{get;set;}
公共字符串开始{get;set;}
公共字符串结束{get;set;}
公共任务(字符串名称、字符串规范、字符串类型、字符串进度、,
串触点、串开始、串结束)
{
名称=名称;等级库=等级库;类型=类型;进度=进度;
触点=触点;开始=开始;结束=结束;
}
公共重写字符串ToString()
{
返回$“{Name,-10}{Spec,-35}{Type,-20}{Progress,-20}”+
$“{Contact,-20}{Start,-15}{End,-10}”;
}
}
现在回答你关于将项目放入文本框的问题-这很容易。在列表框的selection changed事件中,您可以获取所选任务(请参见按钮单击事件中的示例代码),并将每个文本框设置为所选任务的一个属性


然后在button click事件中,我们已经获得了所选的
任务
,因此您所要做的就是从每个文本框中将每个属性设置为正确的值

visualstudio
标记只能用于有关visualstudio应用程序的问题,而不能用于您用它编写的代码。您使用的是UI框架吗?你能给它加个标签吗?