473,799 Members | 2,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.C ontrol
Dim txtControl As TextBox

For Each ThisControl In Me.Controls
If ThisControl.Get Type().ToString () =
"system.Web.UI. WebControls.Tex tBox" Then
txtControl = CType(ThisContr ol, TextBox) ' am I casting this
correctly???
txtControl.Text = "I'vechange d" 'Doesn't work
End If
Next ThisControl

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

I tried ThisControl = CType(ThisContr ol, 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 10063
"Alex Shirley" <po******@alexs hirley.com> wrote in message
news:e9******** *************** ***@posting.goo gle.com...
.. . .
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.C ontrol
For Each ThisControl In Contacts.Contro ls
If ThisControl.Get Type().ToString () = "System.Web.UI. WebControls.Tex tBox" Then
Dim txtcontrol As TextBox = DirectCast(This Control, TextBox)
txtcontrol.Text = Trim(txtcontrol .Text)
End If
Next ThisControl

It's worth noting that "System.Web.UI. WebControls.Tex tBox" 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
2514
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 hard time to understand vb syntax. I don't know if anyone in here can point out which is the equivalent object used in c sharp. Translate these two lines to C sharp: Sub Next_Click(Sender As Object, e As EventArgs) Select Case...
2
1809
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 menuItem5_Click(object sender, System.EventArgs e) { Object oControlType = System.Windows.Forms.Form.ActiveForm.ActiveControl.GetType();
9
2157
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 of control names (TextBox, DropDownList, Calendar). On Post back of this drop-down control, I clear a panel control (I have on the same page) of all its controls, then dymamically add the selected control to the panel. For example, if in ddl
4
316
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 implemented I still have the error. My situation is that I have a datagrid and I click on the update button I get the error. The code: Dim txtDescription As TextBox txtDescription = e.Item.Cells(2).FindControl("txtArticleDesc")
6
6732
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
1810
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 my web page: Exception Details: System.FormatException: Input string was not in a correct format. String qty; foreach (GridViewRow row in gridView1.Rows) {
1
1661
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' is a multiline text box and a varchar datatype (everything else is either a char or a datetime datatype). Am I assigning 'descript' a wrong datatype? Tried making it a nvarchar, still get same result ''''''''''''''''''''''''''''''' Sub...
0
948
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 Special=((TextBox)e.Item.Cells.Controls).Text; string Sex=((TextBox)e.Item.Cells.Controls).Text; am getting Specified cast is not valid. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more...
2
2506
by: thithi | last post by:
Server Error in '/Assignment' Application. -------------------------------------------------------------------------------- Specified cast is not valid. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Specified cast is not valid. Source...
0
10495
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
10269
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
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...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5469
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...
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2942
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.