access properties of dynamically created controls from event handler

G

Gelios

Hello all!
I have a Windows.Forms code where I dynamically created controls. How
can I access to properties from event handler?
For example:

createSomeControls()
{
TextBox textBox = new TextBox();
Button doSomeAction = new Button();
doSomeAction.Click += new EventHandler(doSomeAction_Click);
}

private void btnNewSkillCancel_Click(object sender, EventArgs e)
{
// how do I access textBox.Text?
}

Thanks in advance.
 
T

Tim Wilson

One way to do this would be to scope the TextBox to the Form instead of the
method.

private TextBox textBox;
private Button doSomeAction;

createSomeControls()
{
textBox = new TextBox();
doSomeAction = new Button();
doSomeAction.Click += new EventHandler(doSomeAction_Click);
}

private void btnNewSkillCancel_Click(object sender, EventArgs e)
{
textBox.Text = "";
}
 
G

Gelios

Tim said:
One way to do this would be to scope the TextBox to the Form instead of the
method.

I know this way, but unfortunately it is not acceptable, because I have
a lot of controls which will placed to from related on various
conditions. (This is why I don't show real code, it has a lot of lines)
 
T

Tim Wilson

Then I would say give your control an appropriate name, one that you know
won't clash with another, and then add the control to the Controls
collection of the container (Form I assume). You can lookup the control in
the collection, retrieve a reference to it, and then set the text. The code
at the link below presents a few methods to do this. It's in VB.Net but
should be easily translated.
http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en
 
G

Gelios

Tim said:
Then I would say give your control an appropriate name, one that you know
won't clash with another, and then add the control to the Controls
collection of the container (Form I assume). You can lookup the control in
the collection, retrieve a reference to it, and then set the text. The code
at the link below presents a few methods to do this. It's in VB.Net but
should be easily translated.
http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en

Thanks a lot Tim. I'll try.
 
J

John Davison

Gelios said:
Hello all!
I have a Windows.Forms code where I dynamically created controls. How
can I access to properties from event handler?
For example:

createSomeControls()
{
TextBox textBox = new TextBox();
Button doSomeAction = new Button();
doSomeAction.Click += new EventHandler(doSomeAction_Click);
}

private void btnNewSkillCancel_Click(object sender, EventArgs e)
{
// how do I access textBox.Text?
}

Thanks in advance.

((TextBox)sender).Text
 
T

Tad Marshall

I had the same thought, but then I noticed that this is a Click handler for
btnNewSkillCancel, not the TextBox. This won't work. You need some other
way of getting at the TextBox object. This would be the way to go for
events in the TextBox, but this is an event in a button, not the TextBox.
 
G

Gelios

Tad said:
I had the same thought, but then I noticed that this is a Click handler for
btnNewSkillCancel, not the TextBox. This won't work. You need some other
way of getting at the TextBox object. This would be the way to go for
events in the TextBox, but this is an event in a button, not the TextBox.
Sorry, mityped:
Correct code:

createSomeControls()
{
TextBox textBox = new TextBox();
Button doSomeAction = new Button();
doSomeAction.Click += new EventHandler(doSomeAction_Click);
}

private void doSomeAction_Click(object sender, EventArgs e)
{
// how do I access textBox.Text?
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top