Question Can someone help me with this exercise?

Philippe Slot

New member
Joined
Aug 9, 2014
Messages
1
Programming Experience
Beginner
So I passed my year by slimmest of chances, so that's why I got an exercise for the vacation.

I can't just jump in, and do the task, so that's why I'm redoing my whole education book again.

But I'm having trouble with one exercise and was hoping someone could help me, so I can study for my REAL task.

so it's exercise 9 I'm having trouble with, The description might be dutch, but it's not hard to tell what I have to do.

so when I select a number from the listbox, the label should appear multiple times and the number selected gets multiplied by 1 through 10


here is my code, hopefully you can understand and help
  Private Sub frmTafels_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim intteller As Integer


        For intteller = 0 To 10
            lstBox.Items.Add(intteller)
        Next
    End Sub

    Private Sub lstBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstBox.SelectedIndexChanged
        Dim intteller, intgetal, intUitkomst As Integer
        Dim strTeller As String

        For intteller = 1 To 10 Step 1
            strTeller += intteller
            intgetal = lstBox.SelectedItem.ToString
            intUitkomst = intteller * intgetal

        Next
        lblTafels.Text = (intteller.ToString & " x " & intgetal.ToString & " = " & intUitkomst.ToString)



    End Sub




http://prntscr.com/4b57bi
 
Last edited by a moderator:
The first obvious issue is that you are only setting the Text of the Label once with one String, so how do you expect to show ten lines? You have a loop that goes from 1 to 10 so you need to build up your String there. You need to start with an empty String and then, on each iteration of the loop, add a line to it for the current multiplier. Once the loop is done, your String will contain all ten lines and you can assign it to the Text of the Label.

Another option would be to append to the Text of the Label inside the loop but that's a bad idea. Never make changes to controls repeatedly like that. Always do all the calculations and processing first, then update the control at the end.
 
Back
Top