473,387 Members | 1,516 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,387 software developers and data experts.

Can't cast as a textbox (VB)

Hi

Sorry this is (a sort of) repost. I am trying to trim all text boxes
on my web form. I've got no problem with the trim function, I have a
problem with casting. I am told that if I want to access the Text
property, I need to have typed reference (castit to TextBox with CType
and set the value to the property). I don't exactly know how to do
this. Could somebody show me the code?

Here is what I have so far.

---------------------------------------

Dim ThisControl As System.Web.UI.Control
Dim txtControl As TextBox

For Each ThisControl In Me.Controls
If ThisControl.GetType().ToString() =
"system.Web.UI.WebControls.TextBox" Then
txtControl = CType(ThisControl, TextBox) ' am I casting this
correctly???
txtControl.Text = "I'vechanged" 'Doesn't work
End If
Next ThisControl

-----------------------------------

I tried ThisControl = CType(ThisControl, TextBox), but the properties
won't appear for thiscontrol.

Can somebody help in this casting so I can update or
get the text property here

Many thanks

Alex
Nov 18 '05 #1
2 9980
"Alex Shirley" <po******@alexshirley.com> wrote in message
news:e9**************************@posting.google.c om...
.. . .
I need to have typed reference (cast it to TextBox with CType
and set the value to the property).


If you /know/ you've got an object that's the right "shape" for what
you want, DirectCast() should be quicker than CType(), because it
won't even try to do any conversion, something like

For Each eCtl As Control In Me.Controls
If TypeOf eCtl Is TextBox Then
Dim txtControl As TextBox _
= DirectCast(eCtl, TextBox)
txtControl.Text = "I've changed"
End If
Next

HTH,
Phill W.
Nov 18 '05 #2
Spot on the money, Phill thankyou!

'Trim all textboxes ->
Dim ThisControl As System.Web.UI.Control
For Each ThisControl In Contacts.Controls
If ThisControl.GetType().ToString() = "System.Web.UI.WebControls.TextBox" Then
Dim txtcontrol As TextBox = DirectCast(ThisControl, TextBox)
txtcontrol.Text = Trim(txtcontrol.Text)
End If
Next ThisControl

It's worth noting that "System.Web.UI.WebControls.TextBox" is case sensative.

Cheers

Alex
Nov 18 '05 #3

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

Similar topics

4
by: Hai Nguyen | last post by:
I'm learning C sharp and do not like vb much. I'm creatiing a wepage using panel to test myself. I tried to use these code below, which is written in VB, and to transform them to c sharp but I got...
2
by: Dica | last post by:
i want to allow my "Cut" command to dynamically determine the active control and cut any selected text from it (assuming it's a textBox control). so far i've got this: private void...
9
by: Matt Tapia | last post by:
I having a problem that receives the following error: Specified cast is not valid And I need some help. Here is what is happening: I have a form with a drop-down control that contains a list...
4
by: Andy Sutorius | last post by:
Hi, I have researched this error and have found that many before me have struggled with this. That makes me feel a little better. However, despite all of the solutions I have seen and...
6
by: Lore Leunoeg | last post by:
Hello I derived a class MyControl from the Control class. Public Class MyControl Inherits Control Sub New() MyBase.New() End Sub End Class
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...
1
by: .Net Sports | last post by:
I have a datagrid script where I modify data in an sql dbase in asp.net, when i hit the "update" button, I get a Specified cast is not valid error on my 'descript' declaration, whereas 'descript'...
0
by: prasanth1802 | last post by:
string Empid = e.Item.Cells.Text; string Empname = ((TextBox)e.Item.Cells.Controls).Text; string date = ((TextBox)e.Item.Cells.Controls).Text; string...
2
by: thithi | last post by:
Server Error in '/Assignment' Application. -------------------------------------------------------------------------------- Specified cast is not valid. Description: An unhandled exception...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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...

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.