VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 300,403 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,631 people online right now. Registration is fast and FREE... Join Now!




TryCast operator

 

TryCast operator

eowyn.

3 Jul, 2009 - 10:11 AM
Post #1

New D.I.C Head
*

Joined: 28 Jun, 2009
Posts: 6

Hi all, I have a problem with understanding how to use the TryCast operator. I've tried all different object and dataType variations and guess I just don't get it. I am supposed to use the TryCast operator to determine if the object passed into this procedure is the textbox or the label. Change the ForeColor property of the object to be the color assigned to the newColor variable. I'm to do this in the ChangeColor Sub procedure. Here's my code so far:
Any hints would be appreciated. Thanks.

CODE
Option Strict On
Option Explicit On
Option Infer On


Public Class mainForm

    Private newColor As Color


    Private Sub ChangeColor(ByVal sender As Object)




    End Sub

    Private Sub startButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles startButton.Click
        Timer1.Enabled = True
        Timer2.Enabled = True

    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        clockLabel.Text = DateTime.Now.ToLongTimeString

    End Sub

    Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim changeObject As Object
        'decide which control to change
        If DateTime.Now.Second Mod 5 = 0 Then
            changeObject = nameTextBox
        Else
            changeObject = clockLabel

        End If

        'set the new color
        If DateTime.Now.Second Mod 2 = 0 Then
            newColor = Color.Coral
        Else
            newColor = Color.BlueViolet

        End If
        ChangeColor(changeObject)

    End Sub
End Class


User is offlineProfile CardPM
+Quote Post


mark.bottomley

RE: TryCast Operator

3 Jul, 2009 - 11:11 AM
Post #2

D.I.C Addict
****

Joined: 22 Apr, 2009
Posts: 780



Thanked: 127 times
My Contributions
I think you are looking for for the documentation - I think this should differentiate a Label and a TextBox. e.g.

Dim myTB As TextBox
Dim myLbl as Label

myTB = TryCast (myObject, TextBox)
If IsNothing(myTB ) Then
myLbl = Trycast (myObject, Label)
' do magic with myLbl
Else
'do magic with myTB
End If
User is offlineProfile CardPM
+Quote Post

eowyn.

RE: TryCast Operator

3 Jul, 2009 - 12:47 PM
Post #3

New D.I.C Head
*

Joined: 28 Jun, 2009
Posts: 6

QUOTE(mark.bottomley @ 3 Jul, 2009 - 11:11 AM) *

I think you are looking for for the documentation - I think this should differentiate a Label and a TextBox. e.g.

Dim myTB As TextBox
Dim myLbl as Label

myTB = TryCast (myObject, TextBox)
If IsNothing(myTB ) Then
myLbl = Trycast (myObject, Label)
' do magic with myLbl
Else
'do magic with myTB
End If




Thank you!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:30PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month