Dynamically added controls not available to programmer

  • Thread starter Thread starter jeff
  • Start date Start date
J

jeff

If I add a textbox to a form programmatically:

Dim tb As New TextBox()
Dim panel As New Panel()

tb.Size = New System.Drawing.Size(200, 40)
tb.Location = New System.Drawing.Point(100, 100)
tb.Text = "Test"
tb.Visible = True
Dim o As Windows.Forms.Control
For Each o In Me.Controls
If TypeOf o Is Panel Then
panel = o
Exit For
End If
Next
panel.Controls.Add(tb)
AddHandler tb.TextChanged, AddressOf Me.DataChanged

Why can't I access by it's name ? Why can't I access the panel (that was
also dynamically added) by it's name either?
 
jeff said:
If I add a textbox to a form programmatically: [...]
Why can't I access by it's name ? Why can't I access the panel
(that was also dynamically added) by it's name either?

You can use a 'Hashtable' to store (name, control) pairs. Use the name as
the key.
 
And the control part of the hash table would be what?

Also, you are saying that you cannot access dynamically added controls
programmatically?


Herfried K. Wagner said:
jeff said:
If I add a textbox to a form programmatically: [...]
Why can't I access by it's name ? Why can't I access the panel
(that was also dynamically added) by it's name either?

You can use a 'Hashtable' to store (name, control) pairs. Use the name as
the key.
 
jeff said:
And the control part of the hash table would be what?

Also, you are saying that you cannot access dynamically added controls
programmatically?

You will have to add the controls to the hash table, for example:

\\\
Private m_Controls As Hashtable
..
..
..
Public Sub New()
m_Controls = New Hashtable()
 
You are asking "Why can't I access it by it's name?", while in your example,
you are using the type to attempt to get to the panel, so I guess you mean
type? In order to get a control from "Me.Controls", you must first add it to
"Me.Controls" - check the code in a form in the "Windows Form Designer
generated code" up top in the form, and you will see that the designer
generated code does it for every control on the form, or did I
miss-understand you?
 
That does not help. I am not explaining this well. Here is a simpler
example. Start with a blank form. Paste this into the form's load event:

Dim tb As New TextBox

tb.Size = New System.Drawing.Size(200, 40)
tb.Location = New System.Drawing.Point(4, 4)
tb.Text = "Test"
tb.Name = "tbox"
tb.Visible = True
Me.Controls.Add(tb)

Now there is a textbox at top of form with the word "Test" entered into it.
Now, I cannot access in code in the form (like a button click) Me.tbox since
the error is 'tbox' is not a member of 'DynamicControls.Form1'.

I'd like to be able to access this textbox easily.

Regards,

Jeff
 
That does not help. I am not explaining this well. Here is a simpler
example. Start with a blank form. Paste this into the form's load event:

Dim tb As New TextBox

tb.Size = New System.Drawing.Size(200, 40)
tb.Location = New System.Drawing.Point(4, 4)
tb.Text = "Test"
tb.Name = "tbox"
tb.Visible = True
Me.Controls.Add(tb)

Now there is a textbox at top of form with the word "Test" entered into it.
Now, I cannot access in code in the form (like a button click) Me.tbox since
the error is 'tbox' is not a member of 'DynamicControls.Form1'.


I'd like to be able to access this textbox easily. In your example the
me.button1 would fail since the button would be added to the form
programmatically.

Regards,

Jeff
 
Dim tb As New TextBox

Did you put this line inside the method where you create the textbox? If
so, try moving it into the class declaration instead.
 
HI Jeff,
I'm afraid you couldn't do this, you see, create a control dynamically
and access it by its name means you need declare a variable at runtime. As
far as I know, VB.NET(and other languages like C# and VC++) doesn't support
this feature. You may use the hashtable to simplify you access to the
variable. If you do want this feature, you may take a look at Python, it's
a dynamic-type language.
You can generate type and even code at run-time.
Thanks!


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| From: "Jeff" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
| Subject: Re: Dynamically added controls not available to programmer
| Date: Wed, 1 Oct 2003 14:53:53 -0400
| Lines: 64
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: c-67-165-36-216.client.comcast.net 67.165.36.216
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:53534
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| That does not help. I am not explaining this well. Here is a simpler
| example. Start with a blank form. Paste this into the form's load event:
|
| Dim tb As New TextBox
|
| tb.Size = New System.Drawing.Size(200, 40)
| tb.Location = New System.Drawing.Point(4, 4)
| tb.Text = "Test"
| tb.Name = "tbox"
| tb.Visible = True
| Me.Controls.Add(tb)
|
| Now there is a textbox at top of form with the word "Test" entered into
it.
| Now, I cannot access in code in the form (like a button click) Me.tbox
since
| the error is 'tbox' is not a member of 'DynamicControls.Form1'.
|
|
| I'd like to be able to access this textbox easily. In your example the
| me.button1 would fail since the button would be added to the form
| programmatically.
|
| Regards,
|
| Jeff
|
|
| | > "jeff" <[email protected]> scripsit:
| > > And the control part of the hash table would be what?
| > >
| > > Also, you are saying that you cannot access dynamically added controls
| > > programmatically?
| >
| > You will have to add the controls to the hash table, for example:
| >
| > \\\
| > Private m_Controls As Hashtable
| > .
| > .
| > .
| > Public Sub New()
| > m_Controls = New Hashtable()
| > .
| > .
| > .
| > m_Controls.Add(Me.Button1.Name, Me.Button1)
| > ...
| > End Sub
| > .
| > .
| > .
| > DirectCast(m_Controls.Item("Button1"), Button).PerformClick()
| > ///
| >
| > If you create/remove controls at runtime, you will have to add/remove
| > them to/from the hash table too.
| >
| > --
| > Herfried K. Wagner
| > MVP · VB Classic, VB.NET
| > <http://www.mvps.org/dotnet>
|
|
|
 
Back
Top