473,804 Members | 3,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CType not working for Button

I have the following:

sub submitQuestion_ click(Sender as Object, e as EventArgs)
Dim submitButton as Button = CType(sender,Bu tton)

This gives me an error that says:

" System.InvalidC astException: Specified cast is not valid. "

Why is that?

Sender is a button.

Tom
Nov 19 '05 #1
11 1649

"tshad" <ts**********@f tsolutions.com> wrote in message
news:OB******** ******@TK2MSFTN GP14.phx.gbl...
I have the following:

sub submitQuestion_ click(Sender as Object, e as EventArgs)
Dim submitButton as Button = CType(sender,Bu tton)

This gives me an error that says:

" System.InvalidC astException: Specified cast is not valid. "

Why is that?

Sender is a button.

Tom


Try this and if you still can't figure it out, post the results...

Sub submitQuestion_ Click(ByVal Sender As Object, ByVal e As EventArgs)
Response.Write( Sender.GetType( ).ToString())
End Sub

Then run the application and click the button.

If this doesn't help, remember to post what is printed.

Mythran
Nov 19 '05 #2
Try this:

Dim submitButton = CType(sender, Button)
Jason Bentley

Nov 19 '05 #3
"Mythran" <ki********@hot mail.comREMOVET RAIL> wrote in message
news:e5******** ******@TK2MSFTN GP14.phx.gbl...

"tshad" <ts**********@f tsolutions.com> wrote in message
news:OB******** ******@TK2MSFTN GP14.phx.gbl...
I have the following:

sub submitQuestion_ click(Sender as Object, e as EventArgs)
Dim submitButton as Button = CType(sender,Bu tton)

This gives me an error that says:

" System.InvalidC astException: Specified cast is not valid. "

Why is that?

Sender is a button.

Tom

Try this and if you still can't figure it out, post the results...

Sub submitQuestion_ Click(ByVal Sender As Object, ByVal e As EventArgs)
Response.Write( Sender.GetType( ).ToString())
End Sub

Then run the application and click the button.

If this doesn't help, remember to post what is printed.


I did that and got the result:

sender type = System.Web.UI.W ebControls.Butt on

Tom
Mythran

Nov 19 '05 #4

<im****@gmail.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Try this:

Dim submitButton = CType(sender, Button)
Actually, it seems both ways work.

I don't know why my old line wasn't working. I just copied the line back in
from my original post and it work. Not sure what happened.

You're way works also, BTW.

Why would that work, I thought you had to have the "as" when dimming a
variable?

Thanks,

Tom

Jason Bentley

Nov 19 '05 #5
Yes, but you are not really creating a new button, you are just copying
an existing button.

Jason Bentley
http://geekswithblogs.net/jbentley

Nov 19 '05 #6
<im****@gmail.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Yes, but you are not really creating a new button, you are just copying
an existing button.
But aren't you just making a new instance of that button?

And I thought you always had to give it a type (which is obviously not the
case here as it does work) if you Dimmed it.

Tom
Jason Bentley
http://geekswithblogs.net/jbentley

Nov 19 '05 #7
I do not think so, you are creating a button that references the
original button which also takes the original button's type.

Jason Bentley
http://geekswithblogs.net/jbentley

Nov 19 '05 #8
<im****@gmail.c om> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
I do not think so, you are creating a button that references the
original button which also takes the original button's type.
So if I am creating a variable and assigning it a value, does that mean I
don't need to use the "as", such as:

Dim ktr = 135

or

Dim name = "Franklin"

or

Can I change:

Dim oGrid as DataGrid =
CType(DataList1 .Items(DataList 1.SelectedIndex ).FindControl(" DataGrid1"),Dat aGrid)

to

Dim oGrid =
CType(DataList1 .Items(DataList 1.SelectedIndex ).FindControl(" DataGrid1"),Dat aGrid)

Not that I would, just trying to see what you can and cannot do and why.

Thanks,

Tom


Jason Bentley
http://geekswithblogs.net/jbentley

Nov 19 '05 #9
Tom, I don't see a problem with most of those but your samples above
were not exactly what we were doing before. We were using CType. The
oGrid may be a little different. I don't see any reason why it will not
work if you use Dim oGrid = CType(DataGrid1 , DataGrid). Keep in mind,
though, you are referencing an existing object, not creating a new. C#
is my first language, also, and you should consult a knowledgable
VB.NET person before using it in production.

Nov 19 '05 #10

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

Similar topics

4
6575
by: Joeri | last post by:
Hi, I'm struggling with this issue, maybe someone has already solved ik before. Here's the problem I Have : clsA witch is a base class clsB inherits from clsA clsC inherits from clsA clsD inherits from clsA
10
2356
by: Douglas Buchanan | last post by:
I am using the following code instead of a very lengthly select case statement. (I have a lot of lookup tables in a settings form that are selected from a ListBox. The data adapters are given a similar name to the table. Rather than making a long Select Case that could become obsolete if lookup tables are added and the source table of the ListBox is edited I came up with this code.) This code works but of course it gives me build...
8
2024
by: lawrence | last post by:
I'm a beginner with Javascript and especially cross-browser Javascript. I got this working in IE, but not in Netscape 7. It seems like, in Netscape, every time I click on a button, the focus shifts to that button, so there is no text to be selected. What should I do? Below you'll see some code that I have in one of my forms. I was hoping to have these buttons and when I click on them they would take selected text from a textarea box and...
13
1679
by: Carl Howarth | last post by:
Hello there, I am currently in the process of 'getting strict' with some of our aspx.vb files. I have a function that is called either when a datarepeater is bound or when a button on the datareader is selected. The function accepts a 'sender' object, which may be of a different type depending on how the function is ran. In one instance the type is "System.Web.UI.WebControls.RepeaterItem", and on others it is...
2
1459
by: Marcus Kwok | last post by:
I was following the tutorial given at: http://msdn.microsoft.com/netframework/programming/winforms/?pull=/library/en-us/dndotnet/html/mdiappsmenus.asp except writing Managed C++ instead of VB.NET. In particular, under "Using Menu Groups", they have this code to implement radio-button-like functionality in menu items: Private Sub RadioCheck_Click( _ ByVal sender As System.Object, _
4
1923
by: AXH | last post by:
Hi All, I have to following problem. Is it possible type convert a stringvalue to a type value. Please look below for the example dim regvalue as string= "123456" dim regtype as string = "String"
6
2775
by: Don | last post by:
Using VS 2005 and VB.NET. In the sample code below, GetFirst returns an Item Object, but it's wrapped in the System.__ComObject type. When I try to use CType to get a MailItem object, I get the error "No Such Interface Supported". And if I try to use lobjItem properties directly, such as lobjItem.Subject, I get the error "Option Strict On disallows late binding". Anyone have a solution for this? Thanks! Dim lobjFolder As...
0
9707
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9585
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,...
1
10323
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
10082
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
9161
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
6856
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
5525
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
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.