473,404 Members | 2,114 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,404 software developers and data experts.

cast from string to button

Im trying to make some code that can reference x amount of buttons and then
switch visible to false

For x = 1 To 10

btnSelected = CType("btnButton" & x.ToString, Button)

btnSelected.Visible = False

Next

Provides the error :

Value of type 'String' cannot be converted to 'System.Windows.Forms.Button'.

Any ideas, about converting string to object types such as buttons ?
Jun 13 '06 #1
3 3092
Jon,

If you will add this string on runtime, than the best approach is to add the
values from the string to the Tag of the button. (typed here so watch typos
or whatever)

Than you get

For each ctr in controls
if typeof ctr Is button then
if ctr.tag.toString = TheGivenString
btnSelected.Visible = False
end if
end if
Next
This goes fast, you can do it as well with late binding using reflection but
that will cost you performance.

In development time this kind of instructions are in my opinion without
sense when you know the name of a control than there is no need to use a
string. (It looks than like a misplaced way of using scripting).

I hope this helps,

Cor
Jun 13 '06 #2
Thanks cor , helped alot

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Jon,

If you will add this string on runtime, than the best approach is to add
the values from the string to the Tag of the button. (typed here so watch
typos or whatever)

Than you get

For each ctr in controls
if typeof ctr Is button then
if ctr.tag.toString = TheGivenString
btnSelected.Visible = False
end if
end if
Next
This goes fast, you can do it as well with late binding using reflection
but that will cost you performance.

In development time this kind of instructions are in my opinion without
sense when you know the name of a control than there is no need to use a
string. (It looks than like a misplaced way of using scripting).

I hope this helps,

Cor

Jun 13 '06 #3
"Jon Vaughan" <jo**********@hotmail.com> schrieb:
btnSelected = CType("btnButton" & x.ToString, Button)


Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Jun 13 '06 #4

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

Similar topics

2
by: Peter | last post by:
Hi everybody, I'm a new in C# and have a problem I need to create dynamically the name of object let's say "obj1", "obj2" etc. They're hashtables (Action Script objects) I'm doing like...
2
by: mp | last post by:
How do i cast a string from sql to a long? lDocumentVersion is long, oSqlDR.GetValue(0) returns a string. lDocumentVersion =oSqlDR.GetValue(0); Thanks
8
by: Paul | last post by:
I am new to C#, but have c experience. Anyhow I have a web application and am just trying to get the selected value of a dropdown box in integer form so I can pass it to a stored procedure, but...
2
by: ad | last post by:
I have a string like string sAge="13"; How Can I cast sAge to a integer?
3
by: dbuchanan | last post by:
How do I explicitly cast a string to Byte? Here are my circumstances; The string is from a textbox and could be empty. The Byte is a tinyint in SQL Server 2000 I am using the AddRow method as...
3
by: mgonzales3 | last post by:
I have an object string property that is a string. I want to convert to a DateTime. Any ideas? DateTime eventDt = ((DateTime)mEvent.EventDt).Date; Error 1 Cannot convert type 'string' to...
13
by: archuleta37 | last post by:
I'm trying to cast a String from a web forms textbox into an Int16 (see code below), but it's not quite working. When I post the textbox with numbers (no alpha chars) I get the following error in...
2
by: elena | last post by:
Hi, All Please, i need help with cast string to DateTime: DateTime dt = new DateTime(2006,09,17); it works i can update Field in Access DB Next field is Short Time data type, how i can cast...
13
by: pkirk25 | last post by:
I have a string with format "0|first_name|last_name|..." which i can split to a vector<stringby the "|" token. At the moment to get the row number I'm using size_t row_number =...
1
by: =?Utf-8?B?U2lzbmF6?= | last post by:
I'm having a very strange problem I can't seem to figure out and am hoping maybe somebody has seen it before. I get an exception "Cast string to date is invalid" with this chunk of code: '...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...

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.