none
LOOP using an event RRS feed

  • Question

  • Hello, so, i'm creating an app and has a button that calculate something and send his value to a textbox, but, i want to create a loop that counts to 10, because i have 10 textboxes and everytime that i click on this button he cand send the value to a differente textbox, i want that when i click the first time on him, after he do the math send the value to the first textbox, and the next time that i click on the button he can send the value to textbox number 2, and and so on... until reach the textbox 10.

    Wednesday, June 20, 2018 4:10 AM

Answers

  • Hi Gustavo,

    If so, just need to change the code to:

    Public Class Form1
        Private count As Integer = 0
    
        Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
            count += 1
            If count >= 11 Then Return
            CType((Controls("tc" & count.ToString())), TextBox).Text = count.ToString()
        End Sub
    End Class

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Gustavo Nikov Wednesday, June 20, 2018 4:36 PM
    Wednesday, June 20, 2018 7:42 AM
  • ahh, ok, the textboxes r on a groupbox, how can i adapt the code to this?

    Include the name of the groupbox before the controls collection:

    CType((GroupBoxName.Controls("tc" & count.ToString())), TextBox).Text = ...


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Gustavo Nikov Wednesday, June 20, 2018 4:36 PM
    Wednesday, June 20, 2018 4:10 PM
    Moderator

All replies

  • i want that when i click the first time on him, after he do the math send the value to the first textbox, and the next time that i click on the button he can send the value to textbox number 2, and and so on... until reach the textbox 10.

    Create a collection of text boxes, such as a List.
    https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx

    In the form load event, add the textboxes to the collection in the order in which you want to update them.

    In the timer tick event, declare the variable you will use for the counter as 'Static'.
    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/static

    The Static declaration means that the variable retains its value between calls to the method: that is, it does not get re-initialised each time the timer ticks.

    Each time the timer ticks, use the counter variable as the index into the collection to update the textbox at that index.  Increment the counter variable, and if it reaches 10, set it back to 0.

    Wednesday, June 20, 2018 4:44 AM
  • Hi Gustavo,

    Try this:

    Public Class Form1
        Private count As Integer = 0
    
        Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
            count += 1
            If count >= 11 Then Return
            CType((Controls("textBox" & count.ToString())), TextBox).Text = count.ToString()
        End Sub
    End Class

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 20, 2018 6:28 AM
  • So, Frankie, i tried to do with your script but, the message is this:

    System.NullReferenceException: 'Object reference not defined for an instance of an object.'

    Wednesday, June 20, 2018 6:59 AM
  • Hi Gustavo,

    Please set the name of your textboxes to "textBox1", "textBox2" ... "textBox10".

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 20, 2018 7:04 AM
  • I can't just trade de name "textBox" to the name that my textboxes already have? they r name by tc1, tc2, tc3.... tc10

    Wednesday, June 20, 2018 7:38 AM
  • Hi Gustavo,

    If so, just need to change the code to:

    Public Class Form1
        Private count As Integer = 0
    
        Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
            count += 1
            If count >= 11 Then Return
            CType((Controls("tc" & count.ToString())), TextBox).Text = count.ToString()
        End Sub
    End Class

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Gustavo Nikov Wednesday, June 20, 2018 4:36 PM
    Wednesday, June 20, 2018 7:42 AM
  • i tried, but keeps telling the same thing :( System.NullReferenceException: 'Object reference not defined for an instance of an object.'
    Wednesday, June 20, 2018 8:48 AM
  • Hi,

    Which line triggered this error message?

    Please provide the code you used and the name of your textboxes, make sure they can correspond to each other.

    CType((Controls("tc" & count.ToString())), TextBox).Text = count.ToString()

    If your textboxes are tc1, tc2... you need to change the code above to "tc".

    If your textboxes are textBox1, textBox2... you need to change the code above to "textBox".

    Please check it.

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 20, 2018 9:51 AM
  • i used your code, just changed de "textBox" to "tc", the name of the textboxes are tc1, tc2, tc3,tc4...tc10. 

    
        Private Sub CALCULAR1_Click(sender As Object, e As EventArgs) Handles CALCULAR1.Click
    
            'Concentration time calculation
            Precip24 = P24.Text
            Compr1 = L1.Text
            Decliv1 = S1.Text
            Manning1 = N1.Text
            Operacao1 = 5.474 * ((Manning1 * Compr1) ^ 0.8) / (Precip24 ^ 0.5 * Decliv1 ^ 0.4)
    
            'textbox receives the value of operacao1
            tc1.Text = Operacao1
            'next time that someone press the button the value of operacao1 goes to tc2.text and so on
            count += 1
            If count >= 11 Then Return
            CType((Controls("tc" & count.ToString())), TextBox).Text = count.ToString()
    
            If MsgBox("Cálculo realizado com sucesso") Then
                P24.Text = 0
                L1.Text = 0
                S1.Text = 0
                N1.Text = 0
    
            End If
    
        End Sub

    Wednesday, June 20, 2018 11:05 AM
  • Are your textboxes on the form or in some other container like a panel?  You need to ensure that you are accessing the Controls collection of the actual control containing the textboxes.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, June 20, 2018 12:56 PM
    Moderator
  • ahh, ok, the textboxes r on a groupbox, how can i adapt the code to this?
    Wednesday, June 20, 2018 3:11 PM
  • ahh, ok, the textboxes r on a groupbox, how can i adapt the code to this?

    Include the name of the groupbox before the controls collection:

    CType((GroupBoxName.Controls("tc" & count.ToString())), TextBox).Text = ...


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Gustavo Nikov Wednesday, June 20, 2018 4:36 PM
    Wednesday, June 20, 2018 4:10 PM
    Moderator
  • thanks dude, worked out! i trade just the 

    CType((Controls("tc" & count.ToString())), TextBox).Text = count.ToString() by

    CType((Controls("tc" & count.ToString())), TextBox).Text = Operacao1,

    Operacao 1 is the value of the math done by the button!

    Wednesday, June 20, 2018 4:32 PM