Ya hace unos días hice esta consulta, pero no he recibido respuestas y no he
podido solucionarlo. Pongo un botón en una celda de un ListView, y necesito
que, cuando se haga click en un botón, me informe en que row del ListView se
ha apretado el botón.
Con este código, al presionar click, ingresa siempre en el evento normal (no
con delegado) btn_Click, pero nunca en el que yo indico
Gracias por la ayuda
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsApplication1
{
public delegate void ClickEventHandler(object
source,ButtonInCellClickEventArgs1 e);
public class Button1: Button
{
public event ClickEventHandler Button1Click;
protected virtual void OnClick(object source, ButtonInCellClickEventArgs1
e)
{
Console.WriteLine("\Clickeado !!!");
if (Button1Click != null) Button1Click(this, e);
}
}
public class ButtonInCellClickEventArgs1 : System.EventArgs
{
private int _row;
private int _col;
//Constructor.
//
public ButtonInCellClickEventArgs1(int row, int col)
{
_row = row;
_col = col;
}
public int RowIndex {get{return _row;}}
public int ColIndex {get{return _col;}}
}
public class Form3 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView1;
private Button1 button1 = new Button1();
private System.ComponentModel.Container components = null;
public Form3()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.button1 = new Button1();
//
// button1
//
this.button1.Location = new System.Drawing.Point(17, 220);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
//
// listView1
//
this.listView1.Font = new System.Drawing.Font("Verdana", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
this.listView1.Location = new System.Drawing.Point(8, 8);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(416, 216);
this.listView1.TabIndex = 0;
this.listView1.View = System.Windows.Forms.View.Details;
//
// Form3
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(440, 273);
this.Controls.Add(this.listView1);
this.Controls.Add(this.button1);
this.Name = "Form3";
this.Text = "Form3";
this.Load += new System.EventHandler(this.Form3_Load);
//this.ResumeLayout(false);
}
#endregion
static void Main()
{
Application.Run(new Form3());
}
public System.Windows.Forms.ProgressBar
ListView_AddProgressBar(System.Windows.Forms.ListView pListView, int
ListViewItemIndex, int ColumnIndex, int Valor)
{
Rectangle r ;
System.Windows.Forms.ProgressBar pb = new
System.Windows.Forms.ProgressBar();
r = pListView.Items[ListViewItemIndex].Bounds ;
r.Width = pListView.Columns[ColumnIndex].Width ;
if ( ColumnIndex > 0 )
{
r.X = r.X + pListView.Columns[ColumnIndex - 1].Width + 30;
}
pb.Parent = pListView ;
pb.Maximum = 5;
pb.Minimum = 0;
pb.SetBounds(r.X, r.Y, r.Width, r.Height) ;
pb.Value = Valor;
pb.Visible = true ;
return pb ;
}
public Button1 ListView_AddButton(System.Windows.Forms.ListView pListView,
int ListViewItemIndex, int ColumnIndex, string Valor)
{
Rectangle r ;
Button1 btn = new Button1();
r = pListView.Items[ListViewItemIndex].Bounds ;
r.Width = pListView.Columns[ColumnIndex].Width ;
if ( ColumnIndex > 0 )
{
r.X = r.X + pListView.Columns[ColumnIndex - 1].Width+130;
}
btn.Parent = pListView ;
btn.SetBounds(r.X, r.Y, r.Width, r.Height) ;
btn.Location = new System.Drawing.Point(r.X, r.Y);
btn.Size = new System.Drawing.Size(r.Width, r.Height);
btn.Text = Valor;
btn.Name = "button"+ListViewItemIndex;
btn.Tag = ListViewItemIndex;
btn.Visible = true ;
btn.Font = new System.Drawing.Font("Verdana", 8.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
ButtonInCellClickEventArgs1 e = new
ButtonInCellClickEventArgs1(ListViewItemIndex, ColumnIndex);
btn.Button1Click+=new ClickEventHandler(Button1Click);
//btn.Click+=new EventHandler(btn_Click);
//btn.MouseUp+=new MouseEventHandler(btn_MouseUp);
btn.BackColor = Color.SkyBlue;
return btn;
}
public System.Windows.Forms.TrackBar
ListView_AddTrackBar(System.Windows.Forms.ListView pListView, int
ListViewItemIndex, int ColumnIndex, int Valor)
{
Rectangle r ;
System.Windows.Forms.TrackBar trb = new TrackBar();
r = pListView.Items[ListViewItemIndex].Bounds ;
r.Width = pListView.Columns[ColumnIndex].Width ;
if ( ColumnIndex > 0 )
{
r.X = r.X + pListView.Columns[ColumnIndex - 1].Width + 225;
}
trb.Parent = pListView ;
trb.AutoSize = false;
trb.Maximum = 5;
trb.Minimum = 0;
trb.SetBounds(r.X, r.Y, r.Width+10, r.Height) ;
trb.Value = Valor;
trb.Visible = true ;
trb.Enabled = false;
return trb ;
}
private void Form3_Load(object sender, System.EventArgs e)
{
listView1.Clear();
this.listView1.Columns.Add("ID",30,HorizontalAlignment.Left);
this.listView1.Columns.Add("Titulo",100,HorizontalAlignment.Left);
this.listView1.Columns.Add("Estado",100,HorizontalAlignment.Left);
this.listView1.Columns.Add("Acción",50,HorizontalAlignment.Left);
this.listView1.Columns.Add("TrackBar",50,HorizontalAlignment.Left);
// Instancio la clase Contacto
for(int x=0;x<5;x++)
{
ListViewItem lvi = new ListViewItem(x.ToString());
if(x%2 != 0)
{
lvi.BackColor = Color.Lavender;
}
else
{
lvi.BackColor = Color.Ivory;
}
lvi.SubItems.Add("Titulo "+x.ToString());
lvi.SubItems.Add(x.ToString());
listView1.Items.Add(lvi);
ListView_AddProgressBar(this.listView1,Convert.ToInt32(x.ToString()),2,
x);
Rectangle r ;
Button1 btn = new Button1();
r = listView1.Items[Convert.ToInt32(x.ToString())].Bounds ;
r.Width = listView1.Columns[3].Width ;
if ( 3 > 0 )
{
r.X = r.X + listView1.Columns[3 - 1].Width+130;
}
btn.Parent = listView1 ;
btn.SetBounds(r.X, r.Y, r.Width, r.Height) ;
btn.Location = new System.Drawing.Point(r.X, r.Y);
btn.Size = new System.Drawing.Size(r.Width, r.Height);
btn.Text = "Editar";
btn.Name = "button"+Convert.ToInt32(x.ToString());
btn.Tag = Convert.ToInt32(x.ToString());
btn.Visible = true ;
btn.Font = new System.Drawing.Font("Verdana", 8.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
ButtonInCellClickEventArgs1 m = new
ButtonInCellClickEventArgs1(Convert.ToInt32(x.ToString()), 3);
btn.Button1Click+=new ClickEventHandler(Button1Click);
btn.Click+=new EventHandler(btn_Click);
//btn.MouseUp+=new MouseEventHandler(btn_MouseUp);
btn.BackColor = Color.SkyBlue;
ListView_AddTrackBar(this.listView1,Convert.ToInt32(x.ToString()),4, x);
}
}
private void Button1Click(object source, ButtonInCellClickEventArgs1 e)
{
MessageBox.Show(this, "nada");
}
private void btn_Click(object sender, EventArgs e)
{
MessageBox.Show(this, "nada");
}
}
}
Leer las respuestas