Dynamic event handlers

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi all:

I am writing an app that parses a data file with rows of data, and each row
has an unknown number of data values separated by commas. What I need to do
is set up a column of text boxes and implement a MouseDOwn event in each of
the text boxes that replaces the text box by a drop-down when the mouse is
clicked in the text box. However, since I am dynamically declaring and
positioning the text boxes, how can I attach event handlers?

For example, if we declare a TB and event handler we get this:

this.textBox1.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.textBox1_MouseDown_1);
private void textBox1_MouseDown_1(object sender,
System.Windows.Forms.MouseEventArgs e)
{
}

But I am setting up my text boxes like this:



System.Windows.Forms.TextBox[] boxes = new System.Windows.Forms.TextBox[
delimiterCount ];
for ( int i=0; i<delimiterCount; i++)
{
boxes[ i ] = new System.Windows.Forms.TextBox();
boxes.Location = new System.Drawing.Point(0, yPoint );
boxes.Size = new System.Drawing.Size(100,25);
boxes.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.Controls.Add(boxes);
}

and I cannot add

boxes.MouseDown += new
System.Windows.Forms.MouseEventHandler(boxes._MouseDown_1);

to the loop...

Anyone have any ideas?

Thanks.

John.
 
Why not point them all to the same event handler and then check
the ID of the caller (sender) in the event handler and decide which
action to take based on that information (perhaps using a switch)

Something like this

private void panel1_MouseEnter(object sender, System.EventArgs e)
{
TextBox t = (TextBox)sender;

if( t.Name = "username" )
{
// Do something
}


if( t.Name = "password" )
{
// Do something else
}
}

Hope this gives you an idea on how to make your scenario work.

//Andreas
 
Back
Top