Question:
Visual Basic help?!?!? nested for next and matrix!?
lindsey
2010-05-06 06:50:39 UTC
Public Class frmMultiplicationTable

Private Sub BntTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntTable.Click

Dim x As Integer, y As Integer

For y = 1 To 12
For x = 1 To 12
lstTable.Items.Add(x * y)
Next x
Next y
End Sub

End Class


The assignment was to make a multiplication table in a list box, using a loop.

i got this far, the numbers display in the list box. But its just one long row and i need it too actually look like a table, so what should i add?

& vbTab & ? but where?

help pleaseee
Five answers:
?
2010-05-06 14:12:16 UTC
Your on the right track as your code basically works. What you need to do is construct a string for display that includes the value of X, a multiplication sign, value of Y , an equals sign and the value of the answer.



To make things a little easier make another variable call it Z which you will use to hold the answer

Z = X*Y This way you don't have a computation inside the string you construct.



We will also create some extra string variables to copy data into and build the string which will be loaded into the list box



DIM X,Y,Z as integer

DIM strX, strY,strZ, strDisp as string



Before we get too far along I would highly recommend that you change the font in the list box to "Courier New" . this is a fixed space font where the width of each character including the space is the same. Using a fixed space font will help you line up your data



After you compute Z you are going to do teh following:



strDisp = "" initalize and erase any previous string



Copy the data into string variables and PAD the strings...

strX = X.ToString

strY = Y.ToString

strZ = Z.ToString



Padding strings means that you add spaces to the begining or end of a string so as to make the string a standard length.



Since we are using the numbers 1 to 12 some numbers are single digit while others are two digits. Using teh PadLeft function the single digits will get an extra space stuck in front of them.



strX = strX.PadLeft(2, " ") this says make the string strX atleast 2 characters wide and pad it with a space



I preferr to do the copying and padding in seperate steps but you can combine thime into a single step



strX = X.ToString.PadLeft(2, " ")



I don't recommend doing this for now until you become familar as if you need to trroubleshoot you can easily comment out the padding without having to re-write you code.





The answer strZ doesn't need to be padded as it is at the end of teh string.





Now all you need to do is construct your string that will be loaded into the list box.

We do this by concatenation which is a fancy way of saying stick a couple of strings together.



The operator to do this is the Ampersand (&) . DO NOT USE THE PLUS SIGN . Even though a plus sign will work it has a potential bug in that you can in advertantly perform addition especiallay if you are concatenating numbers without first converting them to strings. This is another reason why I recommend copying number into string variables. ( The use of + for concatenation was depreciated by microsoft for these reasons)





Build your string for display:



strDisp = strX & " x " & strY & " = " & strZ



now add strDisp to the list box.
2016-12-07 09:47:04 UTC
relies upon on what you want to do. activity smart, in case you want to develop right into a specialist developer learn Java or C#. in case you want to be a potential consumer on the agency end, how you could combine your present day vb information with get proper of entry to or perhaps excel. if you're only messing round, attempt out those different languages any human beings pronounced. those aren't from now on used very typically in vast agencies yet there continues to be a gap marketplace for it. i imagine the subsequent logical step for you is to bypass as a lot as creating internet applications with VB .internet. you'll learn aspx markup, html, javascript and css. The latter 3 will continually be functional no count number what.
ʃοχειλ
2010-05-06 06:59:11 UTC
If the nature of control does not matter, a ListView control would be better for tabular display.
kataruna
2010-05-07 14:34:29 UTC
I didn't read all the answers but here's the line of code that will work for you:



lstTable.Items.Add(x & vbTab & "*" & vbTab & y & vbTab & "=" & vbTab & x * y)



Happy programming!
Zel
2010-05-06 06:53:39 UTC
dont know dude...but you can try these sites for examples... you may find what you seek



www.a1vbcode.com



www.vbcode.com


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...