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