0

Im doing this project for an online test in ASP.NET in VB im using Microsoft visual studios 2012.

Im trying to get a loop going through my textboxes and check them against a word this will be change to be validated against a database to see if the answer are correct but when I do my loop I cannot get my text from the textbox.

Please see below

Private Sub GoGoGo()

    Dim Textboxname As String        '
    Dim textbox As Object
    Dim TextboxText As Object
    Dim Labelname As String
    Dim label As Object
    Dim LabelText As Object
    Dim Number As Integer  = 1
    Dim MaxTime As Integer = 9
    Dim Currentloop As Integer = 1



    For check As Integer = Currentloop To MaxTime


        If Currentloop <= MaxTime Then
            Textboxname = "TextQ" + Number
            textbox = Textboxname
            TextboxText = textbox
            textbox.ReadOnly = True

        End If

        If Currentloop <= MaxTime Then
            Labelname = "Label" + Number
            label = Labelname
            LabelText = label.Text
            label.Visible = True

        End If

        Number = Number + 1



        If TextboxText = "" Then
            label.Text = "no imput"
            label.ForeColor = Drawing.Color.Black

        End If

        If TextboxText = "server" Then
            label.Text = "Correct"
            label.ForeColor = Drawing.Color.Green
        End If

        If TextboxText = "Wrong" Then
            label.Text = "Wrong"
            label.ForeColor = Drawing.Color.Red
        End If


        If check = 9 Then
            Exit For
        End If


    Next

End Sub
dash
  • 89,546
  • 4
  • 51
  • 71

2 Answers2

1

It looks like you are trying to use the string identifier of the control in place of the the actual control. Instead, you should take this identifier and search for the actual control on the page. You can do this using the FindControl method

Your function would therefore look something like (not compile tested):

Private Sub GoGoGo()
    '
    Dim oTextBox As TextBox
    Dim oLabel As Label

    Dim MaxTime As Integer = 9
    Dim Currentloop As Integer = 1

    For check As Integer = Currentloop To MaxTime

        If Currentloop <= MaxTime Then
            'NB You may have to use a recursive call to FindControl. See below.
            oTextBox = CType(Page.FindControl("TextQ" & CStr(check)), TextBox) 
            OTextBox.ReadOnly = True;
        End If

        If Currentloop <= MaxTime Then
            'NB You may have to use a recursive call to FindControl. See below.
            oLabel = CType(Page.FindControl("Label" & CStr(check)), Label)
            oLabel.Visible = True
        End If

        If oTextBox.Text = "" Then
            oLabel.Text = "no imput"
            oLabel.ForeColor = Drawing.Color.Black

        End If

        If oTextBox.Text = "server" Then
            oLabel.Text = "Correct"
            oLabel.ForeColor = Drawing.Color.Green
        End If

        If oTextBox.Text = "Wrong" Then
            oLabel.Text = "Wrong"
            oLabel.ForeColor = Drawing.Color.Red
        End If

    Next

End Sub

Some notes:

You don't need all of those variables. Instead, just find the actual controls, and interact with the properties directly - just check the TextBox.Text value when you need to, and set the Label.text property directly. The set is especially important as you want to update the original control property so it is shown on the page.

Similarly, you don't need Number - you can use check as this is your loop counting variable.

Whether you use the + operator or the & operator for string concatenation is up to you. There's already a good question and several answers here.

You also don't need the exit condition for the loop - the loop will exit as soon as you reach MaxTime. If you want it to exit early, just vary your To condition (e.g. Currentloop To MaxTime - 1)

UPDATE:

Page.FindControl will only work with controls that are immediate children of the root element on the page. Instead, you should try calling FindControl recursively. You should also make sure that a control with the id TextQ1 exists - look in the HTML source for the page on the client to make sure a TextBox with this id exists.

There are many examples of this on the net. Here's a VB.Net version (source: http://www.pavey.me/2007/09/recursive-pagefindcontrol-for-vbnet.html) that you can add to your page:

Public Function FindControlRecursive(Of ItemType)(ByVal Ctrl As Object, ByVal id As String) As ItemType
     If String.Compare(Ctrl.ID, id, StringComparison.OrdinalIgnoreCase) = 0 AndAlso TypeOf Ctrl Is ItemType Then
          Return CType(Ctrl, ItemType)
     End If

     For Each c As Control In Ctrl.Controls
          Dim t As ItemType = FindControlRecursive(Of ItemType)(c, id)

          If t IsNot Nothing Then
               Return t
          End If
     Next

     Return Nothing
End Function

Your line in the code above would then become:

oTextBox = FindControlRecursive(of TextBox)(Page.Controls(0), "TextQ" & CStr(check))

You'd also need to do the same for the Label control.

Community
  • 1
  • 1
dash
  • 89,546
  • 4
  • 51
  • 71
  • Hi Dash, I have just tried this out and on oTextBox = CType(Page.FindControl("TextQ" + Number), TextBox) im getting the error "Conversion from string "TextQ" to type 'Double' is not valid." what should I do and thank you for your help – Bennjamin Miles Apr 23 '13 at 08:44
  • @BennjaminMiles I've varied the answer slightly since first answering. Let me know how you get on. To paraphrase, though, `oTextBox = CType(Page.FindControl("TextQ" & check), TextBox)` might be more appropriate. – dash Apr 23 '13 at 08:46
  • Hi Dash, I have now change the code to check as you have said but this is still coming up with the error "Conversion from string "TextQ" to type 'Double' is not valid." Kind Regards Benjamin Miles – Bennjamin Miles Apr 23 '13 at 09:01
  • @BennjaminMiles Does `CType(Page.FindControl("TextQ" & CStr(check)), TextBox)` make any difference? – dash Apr 23 '13 at 09:06
  • Hi Dash, Thank you so much for your help so far but the next line oTextbox.ReadOnly = True is coming up with this error now - Object reference not set to an instance of an object. – Bennjamin Miles Apr 23 '13 at 09:15
  • Right, what that means is that it couldn't find a control on the page with the name `TextQ1` for example. Make sure this exists, and, if it does, then you need to then look at how to recursively search the page for this control. – dash Apr 23 '13 at 09:19
  • Hi Dash, When I do my breakpoint Im looking at the variable oTextbox for some reason on the line where its get the textQ1 name its says the variable stays as nothing? please advise this textbox does exists Kind Regards Benjamin Miles – Bennjamin Miles Apr 23 '13 at 09:28
  • @BennjaminMiles see the update. FindControl only works with the immediate children of a control, so you either need to know the name of the parent, and call FindControl on that, or call FindControl recursively, as per the update. – dash Apr 23 '13 at 09:31
  • @BennjaminMiles That's brilliant. It might seem like a simple task, but, as you've seen, it's actually quite tricky to accomplish. – dash Apr 23 '13 at 10:15
0

It Look like you are using only name istead of textbox try with the code below

Private Sub GoGoGo()

    Dim Textboxname As String        '
    Dim textbox As TextBox
    Dim TextboxText As Object
    Dim Labelname As String
    Dim label As Object
    Dim LabelText As Object
    Dim Number As Integer  = 1
    Dim MaxTime As Integer = 9
    Dim Currentloop As Integer = 1



For check As Integer = Currentloop To MaxTime


    If Currentloop <= MaxTime Then
        Textboxname = "TextQ" + Number
        textbox = Ctype(Me.Controls(Textboxname), TextBox)
        TextboxText = textbox.Text
        textbox.ReadOnly = True

    End If

    If Currentloop <= MaxTime Then
        Labelname = "Label" + Number
        label = Labelname
        LabelText = label.Text
        label.Visible = True

    End If

    Number = Number + 1



    If TextboxText = "" Then
        label.Text = "no imput"
        label.ForeColor = Drawing.Color.Black

    End If

    If TextboxText = "server" Then
        label.Text = "Correct"
        label.ForeColor = Drawing.Color.Green
    End If

    If TextboxText = "Wrong" Then
        label.Text = "Wrong"
        label.ForeColor = Drawing.Color.Red
    End If


    If check = 9 Then
        Exit For
    End If


Next

End Sub
Mandeep Singh
  • 2,016
  • 7
  • 19
  • 32
  • Hello Mandeep, I have tried this and on "textbox = Ctype(Me.Controls(Textboxname), TextBox) " I get this error please advise Conversion from string "TextQ1" to type 'Integer' is not valid. – Bennjamin Miles Apr 23 '13 at 09:30
  • try textbox = Ctype(Me.Controls(key:= Textboxname), TextBox) – Mandeep Singh Apr 23 '13 at 09:42