C# dynamically add button and send event args with it
Solution 1
The First Question
How would you add a button onto standard winform listview control
There is no way to add the control directly since listview(or listviewitem) doesn't function as container. you might need to look for custom method/extended functionality. look at the ListViewExtender
at the article
How to add button into a listview in winforms
The Second Question
How to make event handler to the button and add event argument to it
Once you use the ListViewExtender
, you can use the custom ListViewColumns to get the parameter of the listviewitem sub text. You don't need to pass the argument from the UI
dynamicbutton.Click += OnButtonActionClick();
private void OnButtonActionClick(object sender, ListViewColumnMouseEventArgs e)
{
// you clicked the button.
MessageBox.Show("your current listviewItem - " - e.SubItem.Text)
}
Solution 2
Try this
Button dynamicbutton = new Button();
dynamicbutton.Command += new CommandEventHandler(dynamicbutton_Command);
dynamicbutton.CommandName = "myCommandName";
dynamicbutton.CommandArgument = anyID;
void dynamicbutton_Command(object sender, CommandEventArgs e)
{
// do stuff
}
and also try this to add yr button
item.Controls.Add(dynamicbutton);
Mathlight
Updated on June 04, 2022Comments
-
Mathlight almost 2 years
The answer would be very easy I think, but I can't find it...
I've got a listview programmed this way:
ListView board = new ListView(); board.Bounds = new Rectangle(new Point(0, 0), new Size(this.Width, this.Height)); board.View = View.Details; board.LabelEdit = false; board.AllowColumnReorder = false; board.GridLines = true; //textPieces 0 = Van 1 = Titel 2 = Ontvangen 3 = groote board.Columns.Add(" Afzender ", -10, HorizontalAlignment.Center); board.Columns.Add(" Titel ", -10, HorizontalAlignment.Center); board.Columns.Add(" Ontvangen ", -10, HorizontalAlignment.Center); board.Columns.Add(" Groote ", -10, HorizontalAlignment.Center); board.Columns.Add(" Bekijken ", -10, HorizontalAlignment.Center); for (int c = 0; c < new_message_counter; c++) { ListViewItem item = new ListViewItem(berichten_verzameling[c, 0], 0); item.SubItems.Add(berichten_verzameling[c, 1]); item.SubItems.Add(berichten_verzameling[c, 2]); item.SubItems.Add(berichten_verzameling[c, 3]); Button dynamicbutton = new Button(); dynamicbutton.Text = "Bekijk dit bericht"; dynamicbutton.Name = "show_all_number_" + c; item.SubItems.Add(dynamicbutton); board.Items.Add(item); } groupBox1_all_message.Controls.Add(board);
As some of you probably already saw, there is an error... because this doesn't work: ( item.SubItems.Add(dynamicbutton);)
So my first question is how can I show the button on the same line of the other info.
Also, how can I make an eventhandler for all these buttons dynamically, and send some arguments with it...
Like in html / javascript it was something like :
<button OnClick="DoStuff(5,true);">do it!</button>
But how can I do this in in C#?
Thanks in advance TWCrap
-
Mathlight almost 12 yearsLooks nice, but when i use it, i get this error: Error 1 The type or namespace name 'CommandEventArgs' could not be found (are you missing a using directive or an assembly reference?)
-
JohnnBlade almost 12 yearsdynamicbutton.Click += new EventHandler(dynamicbutton_Click);
-
JohnnBlade almost 12 yearsvoid dynamicbutton_Click(object sender, EventArgs e)
-
Mathlight almost 12 yearsalright, i've got now this: dynamicbutton.Name = "myCommandName"; dynamicbutton.CommandArgument = c; item.SubItems.Add(dynamicbutton); But this(CommandArgument ) isn't valid either. And i've looked for an alternative, but can't come up with one. So what one does i have to use? And this line: item.Controls.Add(dynamicbutton); give's the same problem with the Controls part....
-
Turbot almost 12 yearsI think CommandName/Arugment only appear in WPF, couldn't find them in the winform world...
-
Turbot almost 12 yearsadd a button to winform is easier, add a button on top of listview control you need to do some work. if that's is the case, can't you add one button at the bottom of the list view, and you can get the
selectedlistviewitem
when you clicked the button.