Implementation of C # Listview control plus button

Source: Internet
Author: User

Did not write a few blog, when practicing practicing, record notes, do not like to spray.

Start body

The effect of this program is when the mouse clicks on a line of the control, a button appears at the end of the selected line, and a response event is added for the button button.


1, the project preparation. First we create a C # form application that is named WFALISTVIEWBTN.

2, control preparation. Add a ListView control to the form, where the ID is the default. The properties of the control require 3 modifications:

Change the View property to the Detail,fullrowselect property to True and click Columns to add three columns.

3, define variables. This variable is about the button control, and the definition code is as follows:

Private button btn = New button ();

4, add the form to respond to the event. Double-click the form to add the initial load function of the form, as follows:

private void Form1_Load (object sender, EventArgs e)
        {
            listviewitem[] LVs = new Listviewitem[3];
            Lvs[0] = new ListViewItem (new string[] {row 1 column 1, row 1 column 2, ""});
            LVS[1] = new ListViewItem (new string[] {row 2 column 1, row 2 column 2, ""});
            LVS[2] = new ListViewItem (new string[] {row 3 column 1, row 3 column 2, ""});
            This.listView1.Items.AddRange (LVS);

            Btn. Visible = false;
            Btn. Text = "...";
            Btn. Click + + This.button_click;
            THIS.LISTVIEW1.CONTROLS.ADD (BTN);
            This.btn.Size = new Size (This.listview1.items[0]. SUBITEMS[2]. Bounds.width,
            this.listview1.items[0]. SUBITEMS[2]. bounds.height);
        }
5, ListView click event. Double-click the ListView control to add a SelectedIndexChanged response event with the function code as follows:

private void Listview1_selectedindexchanged (object sender, EventArgs e)
        {
            if ( This.listView1.SelectedItems.Count > 0)
            {
                this.btn.Location = new Point (This.listview1.selecteditems[0]. SUBITEMS[2]. Bounds.left,
                    this.listview1.selecteditems[0]. SUBITEMS[2]. Bounds.top);
                This.btn.Visible = true;
            }
        }


6, button buttons to respond to events. The code is as follows:

private void Button_Click (object sender, EventArgs e)
        {
            MessageBox.Show (this.listview1.selecteditems[0). Subitems[0]. Text);
        }

So far, the code is ready to complete, click to run, to see the implementation effect. The following figure:


Click the button at the back and show the image below


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.