none
how to add a button as listitem into listview? RRS feed

  • Question

  • Hello All,
    is there a way to add a button as listitem into listview?
    and assign an action on button click (let's say MessageBox.Show("clicked button on row X") ?
    Any info would be great.
    Tuesday, July 3, 2007 6:48 PM

Answers

  • You might want to look at using a datagridview instead, as that sort of thing is actually pretty easy with a DataGridViewButtonColumn and a simple google should do it.

    If you feel you must use a ListView, check out this article.  A bit long and you'll certainly have to download and review the code, but it should work for you

    http://www.codeproject.com/cs/miscctrl/ListViewEmbeddedControls.asp


    Good luck,

    -mwalts
    Thursday, July 5, 2007 6:18 PM
  • If your ListView is only ever going to be in Details mode then I agree with the suggestion of using a DataGridView. It is much easier to make a DataGridView look like a ListView and embed controls in the cells. Besides, the DGV already has a ButtonCell defined and ready to use.

     

    If you still really want to use a ListView then I think that codeproject.com article seems quite good. But I don't think you really need to actually use a button control.

    Think about it. You already have the click event that you would get with a button. All you really need to do is to make the ListViewItem *look* like a button.

    You could set your ListView to be an owner drawn listview (OwnerDraw=true) and then wireup paint events to the DrawItem and DrawSubItem events. Inside these paint events you can use the ButtonRenderer.DrawButton method to actually handle drawing the button.

     

    Its just an idea. I haven't actually experiemented with it at all.

     

    Regards

    Thursday, July 5, 2007 9:22 PM

All replies

  • Try something like this:

           

    Code Snippet

    private void Form4_Load(object sender, EventArgs e)

            {

                this.listView1.Items.Add("aa");

                this.listView1.Items.Add("bb");

                this.listView1.Items.Add("cc");

                this.listView1.Items.Add("dd");

                this.listView1.Items.Add("ee");

                Button btn = new Button();

                btn.Text = "Click me";

                btn.BackColor = SystemColors.ButtonFace;

                btn.Click += new EventHandler(btn_Click);

                Point p = this.listView1.Items[2].Position;

                p.X -= 21;

                btn.Location = p;

                btn.Size = this.listView1.Items[2].Bounds.Size;

                this.listView1.Controls.Add(btn);

            }

     

    Wednesday, July 4, 2007 1:14 PM
  • Thank you for the help with this,
    but this recipe adds a button to a listview, but not to a listitem of the listview.
    Is it possible to do something similar to
    Button btn = new Button();
    btn.Click += new EventHandler(btn_Click);
    this.listView1.Items.Add(btn,"dd");
    ?
    Thursday, July 5, 2007 11:22 AM
  • I'm afraid as far as I know it's not possible to do the things as this.listView1.Items.Add(btn,"dd").
    Why not add button to the ListView? What're you adding button as ListItem to the ListView for?
    Thursday, July 5, 2007 12:21 PM
  • The problem is to implement buttons for each ListItem, for be able to assign different actions for ListItems.
    I know, the common way to do so is to implement buttons outside the ListView, and apply button events for a selected item.
    But the customer ask for one-click action, but not to select an item and next click on a button.
    What do you think is it possible?
    Thursday, July 5, 2007 4:51 PM
  • You might want to look at using a datagridview instead, as that sort of thing is actually pretty easy with a DataGridViewButtonColumn and a simple google should do it.

    If you feel you must use a ListView, check out this article.  A bit long and you'll certainly have to download and review the code, but it should work for you

    http://www.codeproject.com/cs/miscctrl/ListViewEmbeddedControls.asp


    Good luck,

    -mwalts
    Thursday, July 5, 2007 6:18 PM
  • If your ListView is only ever going to be in Details mode then I agree with the suggestion of using a DataGridView. It is much easier to make a DataGridView look like a ListView and embed controls in the cells. Besides, the DGV already has a ButtonCell defined and ready to use.

     

    If you still really want to use a ListView then I think that codeproject.com article seems quite good. But I don't think you really need to actually use a button control.

    Think about it. You already have the click event that you would get with a button. All you really need to do is to make the ListViewItem *look* like a button.

    You could set your ListView to be an owner drawn listview (OwnerDraw=true) and then wireup paint events to the DrawItem and DrawSubItem events. Inside these paint events you can use the ButtonRenderer.DrawButton method to actually handle drawing the button.

     

    Its just an idea. I haven't actually experiemented with it at all.

     

    Regards

    Thursday, July 5, 2007 9:22 PM