user4899320
user4899320

Reputation: 1

C# triangle pattern

I have a problem with printing triangle patterns in windows forms app making mirror image of the triangle below the first one. I´m trying to achieve pattern 1 and 2 shown at

How to Display 4 Triangle Patterns Side by Side

connected together with bottom of the first one to the top of second. If the user chooses 5 lines the triangle must have 10 lines

I've seen dozens of videos how to do it but none of them explains how to do it in forms application, all of them show the Console way.

this is the code for the first 5 lines

        txtoutput.Text = null;
        int lines = Convert.ToInt32(txtLines.Text);
        string hash = "#";

        for (int i = 0; i < lines; i++)
        {
            rTxtOutputD2.Text += hash + "\n";

            hash = hash + "#";
        }

can anybody help me with the mirror 5 lines ?

Upvotes: 0

Views: 1863

Answers (2)

komodosp
komodosp

Reputation: 3616

Follow the same procedure as the console one. But use a StringBuilder for the entire set of triangles. (e.g. StringBuilder strTriangles = new StringBuilder())

But instead of writing to the console, add to the string, e.g. instead of Console.Write("#"), use strTriangles.Append("#"), and instead of using Console.Writeline, use strTriangles.AppendLine().

Then add a label lblTriangles to your WinForm, and set lblTriangles.Text = strTriangles.ToString().

Upvotes: 1

komodosp
komodosp

Reputation: 3616

And to "mirror" the triangle...

    int lines = Convert.ToInt32(txtLines.Text);

    for (int i = 0; i < lines; i++)
    {
        for (int j = 0; j <= i; j++) {
            rTxtOutputD2.Text += "#";
        }
        rTxtOutputD2.Text += "\n";
    }

    for (int i = lines; i > 0; i--)
    {
        for (int j = 0; j < i; j++) {
            rTxtOutputD2.Text += "#";
        }
        rTxtOutputD2.Text += "\n";
    }

produces (for lines = 10)...

#
##
###
####
#####
######
#######
########
#########
##########
##########
#########
########
#######
######
#####
####
###
##
#

Upvotes: 0

Related Questions