473,395 Members | 1,452 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.

Casting a string to a user control

6
I have user controls named as userCtrlxx where xx is a two-digit number. I'm adding these controls dynamically based on certain strings that are also two-digit numbers parsed from strings. I've tried using CType and DirectCast but no lucks so far.
Has anyone done things like that? Any suggestions? Thank you in advance.

Sample codes:
...
Dim userCtrl As UserControl

'Adding based upon a certain 2-digit string
userCtrl = DirectCast("userCtrl" & 2DigitString, UserControl)
Me.PanelBox.Controls.Add(userCtrl)

'Adding using hard-coded name of user control
userCtrl = New userCtrl20
Me.PanelBox.Controls.Add(userCtrl)

I'm using VB Express 2005 on WinXP.
Sep 11 '07 #1
6 1300
Plater
7,872 Expert 4TB
Just set the ID property of your control to that string, then you will be able to search and find them.

userCtrl.ID = "userCtrl" & 2DigitString
Me.PanelBox.Controls.Add(userCtrl)
Sep 11 '07 #2
kto
6
Thanks Plater for the response. However, I did not find an ID property for the User Control. Did you mean its tag?

Thanks.
Sep 11 '07 #3
Plater
7,872 Expert 4TB
If this is a windows application I think you would use "Name" oopsy.
Just change where I put "ID" to "Name"
Sep 11 '07 #4
kto
6
Using the Name property would make sense. However, I will still need to instantiate it as a New type of user control as in my samples for the hard-coded method. Do you have any pointers how to accomplish that? Thanks

i.e.
This did not work
Dim tCtrl as UserControl
tCtrl.Name = "userCtrl" & 2DigitString
userCtrl = New tCtrl
Me.PanelBox.Controls.Add(userCtrl)

How about a way to get all user controls so that I can loop thru and find a matched one? Any ideas?
Sep 11 '07 #5
Plater
7,872 Expert 4TB
Ah.
Expand|Select|Wrap|Line Numbers
  1. Dim tCtrl as UserControl 
  2. tCtrl = New UserControl() 
  3. tCtrl.Name = "userCtrl" & 2DigitString
  4. Me.PanelBox.Controls.Add(tCtrl)
  5.  
Unless you are trying to get the class name by string (which I am starting to think that you are) and not the object name.
There is something like "reflection" that you can use, although I am entirely clueless on that subject.
Sep 11 '07 #6
kto
6
Already tried that after my response earlier but did not work. May have to learn about the reflection that you're talking about. Thanks
Sep 11 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Chris | last post by:
I created a control that derives from the System.Windows.Forms.Control namespace. I then created an interface to which this new control must adhere, so that I could require future controls to...
6
by: Keith Selbee | last post by:
I have a file that provides details on which user control to populate a web page with at runtime. I'm able to create and load the control successfully, but now I need to cast it from Control to...
8
by: Razak | last post by:
Hi, I have a class which basically do Impersonation in my web application. From MS KB sample:- ++++++++++++++++++++code starts Dim impersonationContext As...
7
by: I am Sam | last post by:
I have a DataGrid that is passing information to a stored procedure properly but the parameters aren't being casted properly. I was woundering if anyone can tell me how I should properly cast the...
9
by: Kris | last post by:
Hi. I have a asp class A with a public function AA() ----------------A.aspx.cs------------------------ public partial class A : System.Web.UI.Page { protected void Page_Load(object sender,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.