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>
|
|
|