[英]Edit Items in a ListBox
I am creating a program using WinForms so users can input info into textboxes on one form which then are saved into a Listbox on another form. 我正在使用WinForms创建一个程序,因此用户可以将信息输入一种形式的文本框中,然后将其保存到另一种形式的列表框中。 I would like to be able to edit the items saved in the listbox by opening the original form on a button click.
我希望能够通过单击按钮打开原始表单来编辑保存在列表框中的项目。 Really struggling with it as I can't think of the code and I can't seem to find a solution.
真的很难解决它,因为我无法想到代码,而且似乎也找不到解决方案。 My Code:
我的代码:
private void btnAdd_Click(object sender, EventArgs e)
{
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.ShowDialog();
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
listBoxRooms.Items.Add(newRoomDisplayForm.value);
}
newRoomDisplayForm.Close();
}
private void btnRemove_Click(object sender, EventArgs e)
{
this.listBoxRooms.Items.RemoveAt(this.listBoxRooms.SelectedIndex);
}
private void btnEdit_Click(object sender, EventArgs e)
{
}
So i've got a Add and Remove button which work perfectly just need a solution to the edit button. 因此,我有一个添加和删除按钮,它们可以完美地工作,只需要解决编辑按钮的问题即可。
Thanks in advance 提前致谢
I'm guessing newRoomDisplayForm.value is a property or a public member inside the form. 我猜newRoomDisplayForm.value是属性或窗体内的公共成员。 You just need to do something like this:
您只需要执行以下操作:
private void btnEdit_Click(object sender, EventArgs e)
{
if(listBoxRooms.SelectedIndex < 0) return;
var tmpValue = listBoxRooms.Items[listBoxRooms.SelectedIndex].ToString();
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.value = tmpValue;
newRoomDisplayForm.ShowDialog();
//TODO: inside "newRoomDisplayForm" set the value to the textbox
// ie.: myValueTextBox.Text = this.value;
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
// replace the selected item with the new value
listBoxRooms.Items[listBoxRooms.SelectedIndex] = newRoomDisplayForm.value;
}
}
Hope it helps! 希望能帮助到你!
You can simply remove the listitem in that specific position, create a new item and add it again. 您可以简单地删除该特定位置的列表项,创建一个新项,然后再次添加它。 it's kind of replacement.
这是一种替代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.