473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

access properties of dynamically created controls from event handler

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.
Nov 17 '05 #1
7 2708
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 = "";
}

--
Tim Wilson
..Net Compact Framework MVP

"Gelios" <ge****@rbcmail.ru> wrote in message
news:d3**********@gavrilo.mtu.ru...
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.

Nov 17 '05 #2
Tim Wilson wrote:
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)
Nov 17 '05 #3
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/?...eindex&lang=en

--
Tim Wilson
..Net Compact Framework MVP

"Gelios" <ge****@rbcmail.ru> wrote in message
news:d3**********@gavrilo.mtu.ru...
Tim Wilson wrote:
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)

Nov 17 '05 #4
Tim Wilson wrote:
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/?...eindex&lang=en


Thanks a lot Tim. I'll try.
Nov 17 '05 #5
Gelios wrote:
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
Nov 17 '05 #6
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.

"John Davison" <no**********@yahoo.com> wrote in message
news:99**************************@FUSE.NET...
Gelios wrote:
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

Nov 17 '05 #7
Tad Marshall wrote:
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.

"John Davison" <no**********@yahoo.com> wrote in message
news:99**************************@FUSE.NET...
Gelios wrote:
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


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?
}
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
5
by: MS Newsgroups | last post by:
Hi, I have a scenario where I am dynamically adding a control from code when a controls event is fired. The problem I have is that when the newly created control is clicked, the click event does...
2
by: Tim_Mac | last post by:
hi, i have an aspx page which dynamically loads a user control and adds it to a placeholder. the control is recreated and added to the placeholder for postbacks as well. the user control...
5
by: Sosh | last post by:
Hi, I can't seem to find much information on this. Please could someone explain to me how to wire up events (selectedIndexChanged) to a bunch of dynamically created controls (Dropdownlists),...
6
by: TB | last post by:
Hi All: I have this page where a rows / cells are programmatically added to to table by pushing a button. The rows contain a textbox and a associated button. What I want to is to be able to...
6
by: Bjorn Sagbakken | last post by:
Hello In VS2005: I am adding buttons and textboxes dynamically into a table, that also dynamically expands. So far, so good, actually very nice. But I am having trouble starting the desired...
3
by: Mark | last post by:
Assume you want to dynamically add one to many link button controls to a web page dynamically at run time. Each link button needs to post back and execute code. As the link buttons are created at...
1
by: euroluxantiques | last post by:
Hi All, Using VB.Net with SQL Server. I have a form that I create a number of Picture Boxes on dynamically, based on a stored procedure that retrieves a listing of images available for a...
4
by: =?Utf-8?B?RHlsYW5TbWl0aA==?= | last post by:
I have a WebForm where I'm dynamically creating some controls and I'm having difficulty understanding how the state is being persisted and how to work with it. I've created a simplified example...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.