Here is the thing I'm struggling with. I have a label which has a click event. When the click happens I want to be able to pass some custom data with this event like so:
private void OnLabel1l_Click(object sender, EventArgs e)
{
e.FileName = "C:\whatever\mypic.jpg";
e.Column = 10;
e.Row = 11;
}
Here is what I have so far:
1. My custom control class:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class MyControl : Label
{
protected override void OnClick(EventArgs e)
{
base.OnClick(new MyEventArgs(50, 12, "C:\\Temp\\mypic.jpg"));
}
}
public class MyEventArgs : EventArgs
{
public readonly int Column;
public readonly int Row;
public readonly string File;
public MyEventArgs(int col, int row, string file) { Column = col; Row = row; File = file; }
}
}
Here is my Form's code:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MyControl label = new MyControl();
label.Text = "My Label";
label.Location = new Point(10, 10);
label.Click += new EventHandler(OnMyControl_Click);
this.Controls.Add(label);
}
private void OnMyControl_Click(object sender, EventArgs e)
{
if (sender is MyControl)
{
MyEventArgs args = e as MyEventArgs;
string file = args.File;
int row = args.Row;
int col = args.Column;
}
}
}
}
The question is: Is it possible to do this in a way that I wouldn't have to cast/convert EventArgs to MyEventArgs inside my OnMyControl_Click method?
Or is there a better solution than this one?
Thanks!


Sign In
Create Account


Back to top









