C# - 在 ListBox 中添加按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/814682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# - Adding Button inside ListBox
提问by Vectovox
I'm writing an C# App (WinForm) with a ListBox having content added by the user. Now, I could have a ordinary button under the ListBox to remove items, but I would like to have the button right next to the content, thus being inside of the ListBox.
我正在编写一个带有用户添加内容的 ListBox 的 C# 应用程序(WinForm)。现在,我可以在 ListBox 下有一个普通按钮来删除项目,但我希望在内容旁边有一个按钮,因此在 ListBox 内。
Like this:
像这样:
- Content 1 | X
- Content 2 | X
- ...
- Content 5 | X
- 内容 1 | X
- 内容 2 | X
- ...
- 内容 5 | X
The problem is that I lack experience in .NET so I have no clue on how this would be possible with all the automated controls going on. I've googled it, but came up with no meaningful results.
问题是我缺乏 .NET 方面的经验,所以我不知道如何在所有自动化控件中实现这一点。我已经用谷歌搜索了它,但没有提出任何有意义的结果。
Any hints, clues or snippets for achieving this are welcome! :)
欢迎提供任何提示、线索或片段来实现这一目标!:)
采纳答案by Vectovox
So one could make a custom control but for my app it isn't really worth the trouble.
所以可以制作自定义控件,但对于我的应用程序来说,这真的不值得麻烦。
What I did was to create a DataGrid, made it resemble a ListView but with its own flare. I did this because the DataGrid already has a buttoncontrol built in to its cells.
我所做的是创建一个 DataGrid,使其类似于 ListView,但具有自己的耀斑。我这样做是因为 DataGrid 已经在其单元格中内置了一个按钮控件。
Yes I know, kind of fugly "hack", but it works like a charm! :)
是的,我知道,有点像“黑客”,但它的作用就像一个魅力!:)
Props to Shay Erlichmen who led me into thinking outsite my ListBox. See what I did there? ;)
给 Shay Erlichmen 的道具,他引导我思考我的 ListBox 之外的问题。看看我在那里做了什么?;)
回答by Ian Roke
I don't know why you would want to do specifically that? I would put a button at the bottom that deletes any selected items in the listbox. That is deemed the usual way of doing such a thing unless you want to use jquery and put an onClick event on the listbox that sends a call to delete the item if it is stored in a database or remove the item from the list on the page.
我不知道你为什么要特别这样做?我会在底部放一个按钮来删除列表框中的任何选定项目。这被认为是做这种事情的常用方法,除非您想使用 jquery 并在列表框上放置一个 onClick 事件,如果该项目存储在数据库中或从页面上的列表中删除该项目,则该事件会发送调用以删除该项目.
回答by Chad Grant
Assuming it's a WinForms app
假设它是一个 WinForms 应用程序
You'd need a custom control for that. I would check around vendors like http://www.devexpress.com/Products/NET/Controls/WinForms/Editors/editors/ListBoxes.xmlmaybe someone knows of a control that specifically does that.
你需要一个自定义控件。我会检查像http://www.devexpress.com/Products/NET/Controls/WinForms/Editors/editors/ListBoxes.xml这样的供应商,也许有人知道专门执行此操作的控件。
回答by Chad Grant
using System; using System.Collections.Generic; using System.Windows.Forms;
使用系统;使用 System.Collections.Generic; 使用 System.Windows.Forms;
namespace WindowsFormsApplication11 { public partial class Form1 : Form { List _items = new List();
namespace WindowsFormsApplication11 { public partial class Form1 : Form { List _items = new List();
public Form1()
{
InitializeComponent();
_items.Add("One");
_items.Add("Two");
_items.Add("Three");
listBox1.DataSource = _items;
}
private void button1_Click(object sender, EventArgs e)
{
// The Add button was clicked.
_items.Add("New item " + DateTime.Now.Second);
// Change the DataSource.
listBox1.DataSource = null;
listBox1.DataSource = _items;
}
private void button2_Click(object sender, EventArgs e)
{
// The Remove button was clicked.
int selectedIndex = listBox1.SelectedIndex;
try
{
// Remove the item in the List.
_items.RemoveAt(selectedIndex);
}
catch
{
}
listBox1.DataSource = null;
listBox1.DataSource = _items;
}
}
}
}
private void button1_Click(object sender, EventArgs e) { // The Add button was clicked. // ...
private void button1_Click(object sender, EventArgs e) { // 添加按钮被点击。// ...
button2.Enabled = true;
}
}
private void button2_Click(object sender, EventArgs e) { // The Remove button was clicked. // ....
private void button2_Click(object sender, EventArgs e) { // 删除按钮被点击。// ....
if (listBox1.Items.Count == 0)
{
button2.Enabled = false;
}
}
}
回答by Shay Erlichmen
Instead of ListBox you can use ListView, ListView has the ability to add custom column types.
您可以使用 ListView 代替 ListBox,ListView能够添加自定义列类型。