Cod*_*ent 1 c# listbox winforms
嘿伙计们,我想出了如何一次一行地将项目添加到列表框中:
try
{
if (nameTxtbox.Text == "")
throw new Exception();
listBox1.Items.Add(nameTxtbox.Text);
nameTxtbox.Text = "";
textBox1.Text = "";
nameTxtbox.Focus();
}
catch(Exception err)
{
MessageBox.Show(err.Message, "Enter something into the txtbox", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Run Code Online (Sandbox Code Playgroud)
但我不能在同一行添加多个项目.喜欢有first_name | last_name | DoB都在同一条线上.当我做
listBox1.Items.Add(last_name.Text);
Run Code Online (Sandbox Code Playgroud)
它将姓氏添加到列表框中的新行,我需要将其添加到与第一个名称相同的行.
听起来你仍然想要添加一个"项目",但是你希望它包含多个文本.只需做一些字符串连接(或使用string.Format),例如.
listBox1.Items.Add(string.Format("{0} | {1}", first_name.Text, last_name.Text));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36791 次 |
| 最近记录: |