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

Preguntas similare

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
Respuesta Responder a este mensaje
#2 Daniel Kurman
15/09/2005 - 14:12 | Informe spam
Gracias Eduardo por la respuesta.
Ya he intentado guardarlo, pero cuando hace el raise del evento, no sé por
qué, no lo mantiene.

slds

"Eduardo A. Morcillo [MS MVP VB]" <emorcillo .AT. mvps.org> escribió en el
mensaje news:
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
Respuesta Responder a este mensaje
#3 Daniel Kurman
15/09/2005 - 16:46 | Informe spam
Les envío el código completo para ver donde está el problema, dado que he
probado otros ejemplos similares, pero en estos casos funciona, pero no en
este. Tal vez esté omitiendo algún paso o algo así. Desde ya, muchas
gracias.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsApplication1
{
public delegate void ButtonInCellClickEventHandler(object sender,
ButtonInCellClickEventArgs e);
public class ButtonInCell : Button
{
public event ButtonInCellClickEventHandler ButtonInCellClick;
public void Clicked(ButtonInCellClickEventArgs e)
{
if (ButtonInCellClick != null)
{
// Invoca el delegado.
ButtonInCellClick(this, e);
}
}
private int _row;
private int _col;
public int RowIndex
{
get
{
return _row;
}
set
{
_row = value;
}
}
public int ColIndex
{
get
{
return _col;
}
set
{
_col = value;
}
}
}
public class ButtonInCellClickEventArgs : System.EventArgs
{
private int _row;
private int _col;

//Constructor.
//
public ButtonInCellClickEventArgs(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 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
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.SuspendLayout();
//
// 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.Name = "Form3";
this.Text = "Form3";
this.Load += new System.EventHandler(this.Form3_Load);
this.ResumeLayout(false);

}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form3());
}
public void ListView_AddButton(System.Windows.Forms.ListView pListView,
int ListViewItemIndex, int ColumnIndex, string Valor)
{
Rectangle r ;
ButtonInCell btn = new ButtonInCell();

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.RowIndex = ListViewItemIndex;
btn.ColIndex = ColumnIndex;
btn.Visible = true ;
btn.Font = new System.Drawing.Font("Verdana", 8.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
ButtonInCellClickEventArgs e = new
ButtonInCellClickEventArgs(ListViewItemIndex, ColumnIndex);
btn.ButtonInCellClick+=new
ButtonInCellClickEventHandler(btn_ButtonInCellClick);
btn.BackColor = Color.SkyBlue;
}
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("Acción",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_AddButton(this.listView1,Convert.ToInt32(x.ToString()),2,
"Editar");
}
}
public void btn_ButtonInCellClick(object sender,
ButtonInCellClickEventArgs e)
{
MessageBox.Show(this, "nada");
}
}
}

"Daniel Kurman" escribió en el mensaje
news:
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
email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaRespuesta Tengo una respuesta
Search Busqueda sugerida