473,592 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting a string to a user control

6 New Member
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("use rCtrl" & 2DigitString, UserControl)
Me.PanelBox.Con trols.Add(userC trl)

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

I'm using VB Express 2005 on WinXP.
Sep 11 '07 #1
6 1304
Plater
7,872 Recognized Expert Expert
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.Con trols.Add(userC trl)
Sep 11 '07 #2
kto
6 New Member
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 Recognized Expert Expert
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 New Member
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.Con trols.Add(userC trl)

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 Recognized Expert Expert
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 New Member
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
1854
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 provide certain functionality. In my form code, I'm not going to be sure of what types of controls the form will be using until run-time. So, I created an array of controls, all cast as Control. This all works fine, so far. Now, in the form code,...
6
1215
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 its specific type so I can access it's specific members. I have the type as a string ("MyUserControl" for example). So, having the name, how do I cast it from Control to this type? Thanks... Keith
8
9445
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 System.Security.Principal.WindowsImpersonationContext Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity
7
2107
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 following: (TextBox)UserPrefix=(TextBox)e.Item.Cells.Controls; string strUserPrefix=UserPrefix.Text; I keep getting the following error and I don't know why because I have declared the UserPrefix as a textbox using "protected...
9
1789
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, EventArgs e) {
0
7871
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8236
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8227
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5400
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3851
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1467
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1202
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.