Help!! 2 Days to finish app and having problems with objectsVB 2008

Posted on 2009-04-23
Last Modified: 2012-05-06
I have wasted many days on errors related to objects.
I really need someone that has a lot of experience to respond to this question.  I only have two days left to finish this app... (They are using it on Saturday)

I am not sure what is going on, but I have been having some real problems with my app and objects.  A declaration will work in one area but not another.
For example.. in another procedure in the same form I have declared...
 Dim oID As Object
        oID = "txtID" & Me.TabRacing.SelectedTab.Tag.ToString
        Dim txt As TextBox = CType(oID, TextBox)

and it converts just fine.
Now in another procedure I have ...
Dim oID As Object = "txtID" & t
        Dim txt As TextBox = CType(oID, TextBox)

Where t is a string = "0"

In my app, I have multiple tabs and need to keep track of all these tabs so I append numbers to the end of the control according to the index of the tab they are working on.

I also had no problem setting the text and tag of a tabpage, but can't retrieve the values from them.  I know the values are there, but I get an error saying Object reference not set...
These already exist on the form.

Dim oID As Object = "txtID" & t

        Dim txt As TextBox = CType(oID, TextBox)

dim s as string = Me.TabRacing.TabPages(t).Text

Open in new window

Question by:Sheritlw
    LVL 85

    Accepted Solution

    Your first snippet can't possibly be working correctly!

            Dim oID As Object
            oID = "txtID" & Me.TabRacing.SelectedTab.Tag.ToString
            Dim txt As TextBox = CType(oID, TextBox)

    What you are doing is placing a STRING into "oID" and then attempt to convert it to a TextBox.  A String cannot be converted to a TextBox...

    I think what you probably want, though, is to find the ACTUAL TextBox on your Form that has the NAME contained in oID right?

    That could be accomplished with code like this:

            Dim txt As TextBox = Nothing
            Dim oID As String = "txtID" & Me.TabRacing.SelectedTab.Tag.ToString
            Dim matches() As Control = Me.Controls.Find(oID, True)
            If matches.Length > 0 Then
                If TypeOf (matches(0)) Is TextBox Then
                    txt = DirectCast(matches(0), TextBox)
                End If
            End If
            If Not IsNothing(txt) Then
                MessageBox.Show("Text: " & txt.Text, "Contents of " & oID, MessageBoxButtons.OK, MessageBoxIcon.Information)
                MessageBox.Show("TextBox not found: " & oID, "Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

    Open in new window


    Author Comment

    Thank you, Thank you, Thank you.
    I have been working on this program for months and what a learning curve between VB 6 and 2008.
    I really appreciate your help.  Your solution worked for all kinds of things I was trying to do.
    Thank you again!

    Author Closing Comment


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now