Delegados

14/09/2005 - 14:17 por Daniel Kurman | Informe spam
Hola. Necesito ayuda. Tengo el siguiente código, el cual agrega un botón en
cada celda de una columna de un listview. El código funciona correctamente,
pero no puedo identificar que botón se ha apretado, es decir, a que fila del
listview corresponde. He intentado hacerlo utilizando delegados, eventos y
EventArgs redefinidos, los cuales no se disparan.

public ButtonCell ListView_AddButton(System.Windows.Forms.ListView
pListView, int ListViewItemIndex, int ColumnIndex, string Valor)
{
Rectangle r ;
ButtonCell btn = new ButtonCell();
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.Visible = true ;
btn.Font = new System.Drawing.Font("Verdana", 8.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
ButtonCellClickEventArgs e = new
ButtonCellClickEventArgs(ListViewItemIndex);
btn.ButtonCellClick+=new
ButtonCellClickEventHandler(btn_ButtonCellClick);
btn.BackColor = Color.SkyBlue;
return btn ;
}


Luego, fuera de la clase, tengo el siguiente código que es lo que define el
delegado, los eventos y argumentos

public class ButtonCellClickEventArgs : System.EventArgs
{
private int _row;

//Constructor.
//
public ButtonCellClickEventArgs(int row)
{
_row = row;
}

public int RowIndex {get{return _row;}}
}
public delegate void ButtonCellClickEventHandler(object sender,
ButtonCellClickEventArgs e);
public class ButtonCell : Button
{
public event ButtonCellClickEventHandler ButtonCellClick;

protected virtual void OnButtonCellClick(ButtonCellClickEventArgs e)
{
if (ButtonCellClick != null)
{
// Invokes the delegates.
ButtonCellClick(this, e);
}
}
}


Muchas gracias

Daniel Kurman
 

Leer las respuestas

#1 Eduardo A. Morcillo [MS MVP VB]
15/09/2005 - 05:53 | Informe spam
Puedes aprovechar la propiedad Tag del control para guardar en ella el
indice de la fila, es decir:

btn.Tag = ListViewItemIndex ;


Eduardo A. Morcillo [MS MVP VB]
http://www.mvps.org/emorcillo
http://mvp.support.microsoft.com/pr...4EF5A4191C

Preguntas similares