triangles of asterisks

Terabojin

Active member
Joined
Mar 4, 2012
Messages
33
Programming Experience
Beginner
i already have the code to produce triangles made of asterisks as follows:



Public Class Form1
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'pattern A
        For n = 1 To 10
            For i = 1 To n
                TextBox1.Text = TextBox1.Text & "*"
            Next
            TextBox1.Text = TextBox1.Text & vbNewLine
        Next

        'pattern B
        For n = 10 To 1 Step -1
            For i = 1 To n
                TextBox1.Text = TextBox1.Text & "*"
            Next
            TextBox1.Text = TextBox1.Text & vbNewLine
        Next
        TextBox1.Text = TextBox1.Text & vbNewLine

        'pattern C
        For n = 10 To 1 Step -1
            For blank = 10 - n To 1 Step -1
                TextBox1.Text = TextBox1.Text & " "
            Next
            For i = 1 To n
                TextBox1.Text = TextBox1.Text & "*"
            Next
            TextBox1.Text = TextBox1.Text & vbNewLine
        Next

        'pattern D
        For n = 10 To 1 Step -1
            For blank = n - 1 To 1 Step -1
                TextBox1.Text = TextBox1.Text & " "
            Next
            For i = 10 - n + 1 To 1 Step -1
                TextBox1.Text = TextBox1.Text & "*"
            Next
            TextBox1.Text = TextBox1.Text & vbNewLine
        Next
        TextBox1.Text = TextBox1.Text & vbNewLine
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = ""
    End Sub
End Class





i know that i have to adust the form and add the textboxes and labels, but i cant for the life of me figure out the code to allow users to enterin the number of rows and columns. in other words provide textboxs to enter the size of the triangles and the symbol to print. Print the triangles only when size is between 3 and 15, inlcusive. Also, provide "Print Triangles" and "Clear Traingles" buttons that perform the appropriate task. Any Ideas?
 
this is what it ays to do:


(Triangles of asterisks) Write a program that displays the following patterns separately, one below the other in a textbox. Use for...Next loops to generate the patterns. All asterisks (*) should be displayed one at a time by the statement outputTextBox.AppendText("*") (This causes the asterisk to display side by side.) The statement outputTextBox.AppendText(vbCrLf) can be used to display spaces for the last two patterns. There should be no other output statement for the program. [Hint: the last two pattern require that each line begin with an appropriate number of blanks.] Maximize your use of repititon(with nested For...Next statements) and minimize the number of output sttements. Set the TextBox's font property to Lucida Console, its MultiLine property to True and its ScrollBars property to verticle 'so that you can scroll through the results. Provide textboxs to enter the size of the triangles and the symbol to print. Print the triangles only when size is between 3 and 15, inlcusive. Also, provide "Print Triangles" and "Clear Traingles" buttons that perform the appropriate task.
 
Hi,

You understand the basic concept of using the for loop which is great but at the moment you are using a fixed integer of 10 for all the for loops. This value needs to be replaced by a user input variable from, for example, a TextBox which specifies the size of the triangle. You can achieve this by doing the following.

Add an additional TextBox to your form and call it txtTriangleSize then before you start to draw your triangle use the following qualifying code to ensure that the entry in the TextBox is a valid number:-

VB.NET:
  Private Sub btnDrawShape_Click(sender As System.Object, e As System.EventArgs) Handles btnDrawShape.Click
    'This variable will hold the valid triangle size if the TryParse statement is successful
    Dim triSize As Integer
    If Integer.TryParse(txtTriangleSize.Text, triSize) Then
      'Only Draw the triangle if it's in the correct size range
      If triSize >= 3 AndAlso triSize <= 15 Then
        'Your draw triangle code goes here....
      Else
        MsgBox("You must enter a valid Range for the size of the Triangle between 3 and 15", CType(vbExclamation + vbOKOnly, MsgBoxStyle), "Invalid Range!")
      End If
    Else
      MsgBox("You must enter a valid Number for the size of the Triangle", CType(vbCritical + vbOKOnly, MsgBoxStyle), "Invalid Entry!")
    End If
  End Sub
You will then need to change your Draw Triangle code to use the triSize variable in your for loops instead of your fixed integer of 10.

Also, to clear any previously drawn symbols in the TextBox you can just call the TextBox1.Clear method to start again.

Hope that helps,

Cheers,

Ian
 
Ian,

I tried to do what you suggested but then it started yelling me that i had many errors, I dont quite understand what i was doing wrong. this is what i tried doing. i know there are spots that need to be altered, but funny enough thats not what was throwing the errors.

Public Class triangleAsterisks
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = ""
    End Sub
    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Draw_Shape.Click
        Dim triSize As Integer
        If Integer.TryParse(txtTriangleSize.Text, triSize) Then
        End If
        'Only Draw the triangle if it's in the correct size range
        If triSize >= 3 AndAlso triSize <= 15 Then
            'pattern A
            For n = TextBox2.Text To TextBox2.Text
                For i = 1 To n
                    TextBox1.Text = TextBox1.Text & "*"
                Next
                TextBox1.Text = TextBox1.Text & vbNewLine
            Next

            'pattern B
            For n = TextBox2.Text To 1 Step -1
                For i = 1 To n
                    TextBox1.Text = TextBox1.Text & "*"
                Next
                TextBox1.Text = TextBox1.Text & vbNewLine
            Next
            TextBox1.Text = TextBox1.Text & vbNewLine

            'pattern C
            For n = 10 To 1 Step -1
                For blank = TextBox2.Text - n To 1 Step -1
                    TextBox1.Text = TextBox1.Text & " "
                Next
                For i = 1 To n
                    TextBox1.Text = TextBox1.Text & "*"
                Next
                TextBox1.Text = TextBox1.Text & vbNewLine
            Next

            'pattern D
            For n = TextBox2.Text To 1 Step -1
                For blank = n - 1 To 1 Step -1
                    TextBox1.Text = TextBox1.Text & " "
                Next
                For i = TextBox2.Text - n + 1 To 1 Step -1
                    TextBox1.Text = TextBox1.Text & "*"
                Next
                TextBox1.Text = TextBox1.Text & vbNewLine
            Next
            TextBox1.Text = TextBox1.Text & vbNewLine
        End If
    End Sub
End Class

 
Hi,

While I look at the rest re-read my post and compare what you have done with this comment that I made:-

You will then need to change your Draw Triangle code to use the triSize variable in your for loops instead of your fixed integer of 10.

Cheers,

Ian
 
Ian,
I cought that error, my applogies. now i am getting these errors:
Error 1 'txtTriangleSize' is not declared. It may be inaccessible due to its protection level. (line 35, column 29)

and

Error 2 Type of 'n' is ambiguous because the loop bounds and the step clause do not convert to the same type.(line 42, column 17)
 
Hi,

Since you have not followed what I suggested you need to change txtTriangleSize to the name of the TextBox that you are using to get the size of the Shape. I think the second issue is related to your use of the textbox again.

Sort the txtTriangleSize and then repost of you still have an issue.

You may find it easier to start again, and follow my points exactly to the letter. That may just sort things for you.

Cheers,

Ian
 
Rehashing this problem, I am not able to figure out a way to input values for the sizes of the triangles, any ideas? Maybe I am just missing something simple.
 
Back
Top