C# dynamically add button and send event args with it

10,240

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);
Share:
10,240
Mathlight
Author by

Mathlight

Updated on June 04, 2022

Comments

  • Mathlight
    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
    Mathlight almost 12 years
    Looks 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
    JohnnBlade almost 12 years
    dynamicbutton.Click += new EventHandler(dynamicbutton_Click);
  • JohnnBlade
    JohnnBlade almost 12 years
    void dynamicbutton_Click(object sender, EventArgs e)
  • Mathlight
    Mathlight almost 12 years
    alright, 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
    Turbot almost 12 years
    I think CommandName/Arugment only appear in WPF, couldn't find them in the winform world...
  • Turbot
    Turbot almost 12 years
    add 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.