Asterisks Patterns in C#

Asterisks Pyramid patterns

Page 1 of 1

13 Replies - 12566 Views - Last Post: 13 November 2010 - 09:08 PM Rate Topic: -----

#1 Tribute_Kronos™  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-November 10

Asterisks Patterns in C#

Posted 09 November 2010 - 05:39 PM

How can I show the output of this program in the windows.form Textbox?
I mean for DOS it works but when I bring the code over to C# windows.forms every time I click on the button that's suppose to print out the double sided Asterisk Pyramid like this below:
* * * * 
 * * * 
  * * 
   * 
   * 
  * * 
 * * * 
* * * *


It only prints out one Asterisk mark, not even an entire row of asterisk. Help would be appreciated.




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Asterisks
{
    public partial class frmAsterisks : Form
    {
        public frmAsterisks()
        {
            InitializeComponent();
        }

        public void Asterisk(string TemporaryText)
        {
        //local variables
        int Asterisks;
        int Asterisk2;
        int Asterisk3;
        int Text;
        int Rows = 0;
            
        //I assign the variable Text to the textbox here.
        Text = Convert.ToInt32(this.txbAsterisks.Text);

        //if the value is less then 1 then throw an invalid argument to the user.
        if (Text < 1)
        {
            //This is the first line in the command that asks the user for a number.
            MessageBox.Show("You entered in a number or a value that can not be excepted, therefore it is an invalid value. Please enter in a number that is greater then 0 but not 0 itself.", "Invalid input", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
            this.lstAsterisks.Text = null;
        }

        //This prints the top half of the hourglass, which is the inverse of the bottom half
        for (Rows = 0; Rows < Text; Rows++)
        {
            //This for loop defines the space lenght between the corner of the textbox and the location of the the asterisks.
            for (Asterisks = Text; Asterisks >= Rows; Asterisks--)
            {
                //Console.write
                TemporaryText = "";
            }

            //This is where the first asterisks is being print, but I left it empty.
            for (Asterisk2 = 1; Asterisk2 <= Rows + 1; Asterisk2++)
            {
                TemporaryText = " ";
            }

            //This prints out the asterisks after number 1 of asterisk. ex. 3,4,5
            for (Asterisk3 = Text; Asterisk3 >= Rows + 1; Asterisk3--)
            {
                TemporaryText = " *";
            }
            //This draws out or prints out the asterisks on the bottom half.
            //Console.Writeline
            this.lstAsterisks.Text = Convert.ToString(TemporaryText);
        }

        //This prints the bottom half of the hourglass
        for (Rows = 0; Rows < Text; Rows++)
        {
            //This for loop defines the space lenght between the corner of the textbox and the location of the the asterisks.
            for (Asterisks = Text; Asterisks >= Rows; Asterisks--)
            {
                TemporaryText = " ";
            }

            //This is to print the first asterisks with one space to the left of the asterisks
            for (Asterisk2 = 1; Asterisk2 <= Rows + 1; Asterisk2++)
            {
                TemporaryText = " *";
            }

            //This prints out the asterisks after number 1 of asterisk. ex. 3,4,5
            for (Asterisk3 = Text; Asterisk3 <= Rows - 1; Asterisk3++)
            {
                TemporaryText = "*";
            }
            //This draws out or prints out the asterisks on the bottom half.
            this.lstAsterisks.Text = Convert.ToString(TemporaryText);
        }
    }

        private void btnAsterisks_Click(object sender, EventArgs e)
        {
            //This is to call the function above.
            Asterisk(this.lstAsterisks.Text);
        }
    }
}


This post has been edited by macosxnerd101: 09 November 2010 - 05:43 PM
Reason for edit:: Added end code tag.


Is This A Good Question/Topic? 0
  • +

Replies To: Asterisks Patterns in C#

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9568
  • View blog
  • Posts: 36,222
  • Joined: 12-June 08

Re: Asterisks Patterns in C#

Posted 09 November 2010 - 08:06 PM

I am going to guess you are not allowed to use 'padleft' for this are you?

Is your textbox's property for multiline set to true?

Additionally each time you do:
TemporaryText = " ";

that means the variable 'temporarytext' is *SET* to space.. ie it *GETS* only that value.. ie.. it WIPES OUT anything else you have in there.

You are probably looking for the append option.. which is the +=.
Was This Post Helpful? 1
  • +
  • -

#3 Tribute_Kronos™  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-November 10

Re: Asterisks Patterns in C#

Posted 11 November 2010 - 11:55 AM

View Postmodi123_1, on 09 November 2010 - 07:06 PM, said:

I am going to guess you are not allowed to use 'padleft' for this are you?

Is your textbox's property for multiline set to true?

Additionally each time you do:
TemporaryText = " ";

that means the variable 'temporarytext' is *SET* to space.. ie it *GETS* only that value.. ie.. it WIPES OUT anything else you have in there.

You are probably looking for the append option.. which is the +=.


no, not yet, but I just changed = to +=.
but they put it all one one line and I want it below the other, like the picture I showed to you so what should I do in order to make this:

* * * * * *

to this:
1
* * * *
2
* * *
3
* *
4
*
5
*
6
* *
7
* * *
8
* * * *
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9568
  • View blog
  • Posts: 36,222
  • Joined: 12-June 08

Re: Asterisks Patterns in C#

Posted 11 November 2010 - 05:05 PM

Have you looked at inserting the

System.Environment.NewLine;


after each set of asterisks to your new line?
Was This Post Helpful? 0
  • +
  • -

#5 Tribute_Kronos™  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-November 10

Re: Asterisks Patterns in C#

Posted 12 November 2010 - 10:55 AM

View Postmodi123_1, on 11 November 2010 - 04:05 PM, said:

Have you looked at inserting the

System.Environment.NewLine;


after each set of asterisks to your new line?


How would you be able to do that?
sorry my programming skills are very limited
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9568
  • View blog
  • Posts: 36,222
  • Joined: 12-June 08

Re: Asterisks Patterns in C#

Posted 12 November 2010 - 11:03 AM

Did you even look it up?
http://msdn.microsof...nt.newline.aspx

"foo"  + 	System.Environment.NewLine + "bar"


Should produce:

Quote

foo
bar

Was This Post Helpful? 1
  • +
  • -

#7 Tribute_Kronos™  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-November 10

Re: Asterisks Patterns in C#

Posted 12 November 2010 - 02:14 PM

View Postmodi123_1, on 12 November 2010 - 10:03 AM, said:

Did you even look it up?
http://msdn.microsof...nt.newline.aspx

"foo"  + 	System.Environment.NewLine + "bar"


Should produce:

Quote

foo
bar


Hey, thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#8 Tribute_Kronos™  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-November 10

Re: Asterisks Patterns in C#

Posted 12 November 2010 - 03:14 PM

View PostTribute_Kronos™, on 12 November 2010 - 01:14 PM, said:

View Postmodi123_1, on 12 November 2010 - 10:03 AM, said:

Did you even look it up?
http://msdn.microsof...nt.newline.aspx

"foo"  + 	System.Environment.NewLine + "bar"


Should produce:

Quote

foo
bar


Srry I got to ask you one more thing.
After you send the information to a new line
how do you make the information centered?
like the first picture I showed you.

Hey, thanks for your help.

Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9568
  • View blog
  • Posts: 36,222
  • Joined: 12-June 08

Re: Asterisks Patterns in C#

Posted 12 November 2010 - 04:46 PM

You pad it with spaces on the left. I ran my hand at his and got it down to one method being called in two for-loops.. heh...
Was This Post Helpful? 0
  • +
  • -

#10 Tribute_Kronos™  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-November 10

Re: Asterisks Patterns in C#

Posted 12 November 2010 - 05:52 PM

View Postmodi123_1, on 12 November 2010 - 03:46 PM, said:

You pad it with spaces on the left. I ran my hand at his and got it down to one method being called in two for-loops.. heh...

Ok, but when I tried it PadLeft only works when there is an integer inside the open/close brackets, I'm using a string through the entire program to execute the commands.
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9568
  • View blog
  • Posts: 36,222
  • Joined: 12-June 08

Re: Asterisks Patterns in C#

Posted 13 November 2010 - 10:09 AM

Per MSDN on PadLeft:

http://msdn.microsof...ng.padleft.aspx

Quote

PadLeft(Int32)
Parameter: totalWidth
Type: System.Int32
- The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters.


So you feed it how many characters you want to the entire returned string to be.. not how many characters of padding.

From the MSDN example:
string str = "BBQ and Slaw";
Console.WriteLine(str.PadLeft(15));  // Displays "   BBQ and Slaw".
Console.WriteLine(str.PadLeft(5));   // Displays "BBQ and Slaw".

Was This Post Helpful? 0
  • +
  • -

#12 Tribute_Kronos™  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-November 10

Re: Asterisks Patterns in C#

Posted 13 November 2010 - 02:33 PM

View Postmodi123_1, on 13 November 2010 - 09:09 AM, said:

Per MSDN on PadLeft:

http://msdn.microsof...ng.padleft.aspx

Quote

PadLeft(Int32)
Parameter: totalWidth
Type: System.Int32
- The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters.


So you feed it how many characters you want to the entire returned string to be.. not how many characters of padding.

From the MSDN example:
string str = "BBQ and Slaw";
Console.WriteLine(str.PadLeft(15));  // Displays "   BBQ and Slaw".
Console.WriteLine(str.PadLeft(5));   // Displays "BBQ and Slaw".


Ok, so there's still one more thing that I don't quite know how to approach, how would you make this code:


/*
* Created by: Joseph Do
* Created on: 09-Nov-2010
* Created for: ICS4U
* Assignment 5- Assignment 5
* This is the Asterisk double side pyramid program.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Asterisks
{
public partial class frmAsterisks : Form
{
public frmAsterisks()
{
InitializeComponent();
}

public void Asterisk(string Text)
{
//local variables
int Asterisks;
int Asterisk2;
int Asterisk3;
int Rows = 0;

/*
* This code is to make the string go onto a new line, it's like return and carry
System.Environment.NewLine
* This code makes spaces to the left of the string
String.Padleft
*/

//I assign the variable Text to the textbox here.
Text = Convert.ToInt32(this.txbAsterisks.Text);

//if the value is less then 1 then throw an invalid argument to the user.
if (Text < 1 || Text == null)
{
//This is the first line in the command that asks the user for a number.
MessageBox.Show("You entered in a number or a value that can not be excepted, therefore it is an invalid value. Please enter in a number that is greater then 0 but not 0 itself.", "Invalid input", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
this.lstAsterisks.Text = null;
}

//This prints the top half of the hourglass, which is the inverse of the bottom half
for (Rows = 0; Rows < Text; Rows++)
{
//This for loop defines the space lenght between the corner of the textbox and the location of the the asterisks.
for (Asterisks = Text - Text - 2; Asterisks >= Rows; Asterisks--)
{
this.lstAsterisks.Text += "" + System.Environment.NewLine;

}

//This is where the first asterisks is being print, but I left it empty.
for (Asterisk2 = 1; Asterisk2 <= Rows + 1; Asterisk2++)
{
this.lstAsterisks.Text += "" + System.Environment.NewLine;
}

//This prints out the asterisks after number 1 of asterisk. ex. 3,4,5
for (Asterisk3 = Text; Asterisk3 >= Rows + 1; Asterisk3--)
{
this.lstAsterisks.Text += " *";
}
}

//This prints the bottom half of the hourglass
for (Rows = 0; Rows < Text; Rows++)
{
//This for loop defines the space lenght between the corner of the textbox and the location of the the asterisks.
for (Asterisks = Text; Asterisks >= Rows; Asterisks--)
{
this.lstAsterisks.Text += "" + System.Environment.NewLine;
}

//This is to print the first asterisks with one space to the left of the asterisks
for (Asterisk2 = 1; Asterisk2 <= Rows + 1; Asterisk2++)
{
this.lstAsterisks.Text += " *";
}

//This prints out the asterisks after number 1 of asterisk. ex. 3,4,5
for (Asterisk3 = Text; Asterisk3 <= Rows - 1; Asterisk3++)
{
this.lstAsterisks.Text += " * " + System.Environment.NewLine;
}
}
}

private void btnClear_Click(object sender, EventArgs e)
{
//This clears the textbox
this.lstAsterisks.Text = null;
}

private void btnAsterisks_Click(object sender, EventArgs e)
{
//This is to call the function
Asterisk();
}

//Extra
private void mniNew_Click(object sender, EventArgs e)
{
//This basically re-installizes the form.
this.Refresh();
this.lstAsterisks.Text = null;
this.txbAsterisks.Text = null;
}

private void mniExit_Click(object sender, EventArgs e)
{
//This closes the entire program.
Application.Exit();
}

private void mniHelp_Click(object sender, EventArgs e)
{
MessageBox.Show(" This is a non-refundable, non-tradable and fully copyrighted product that was created by the existing members of Tribute_Kronos™¸ for Computer Science. The program allows the user to create Asterisk patterns.", "Help", MessageBoxButtons.AbortRetryIgnore);
}

private void mniAbout_Click(object sender, EventArgs e)
{
MessageBox.Show(" Tribute_Kronos™,code RmCi#A000000001ZCsPc-1.00 : States that this program shall not be copied and distributed to others, this product should only be viewed by the Administrators of Computer Science, the members of Tribute_Kronos™, and the essential client. Have a good day, and enjoy our first and finests product, because it's here where we treat our clients as our finest patrons.", "About", MessageBoxButtons.AbortRetryIgnore);
}
}
}

become a recursive function? I mean everything else works just fine but when to me I figure that there is probably not a reasonable way to make it become a recursive function. So can you please help?
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9568
  • View blog
  • Posts: 36,222
  • Joined: 12-June 08

Re: Asterisks Patterns in C#

Posted 13 November 2010 - 02:56 PM

It all depends on what you want to repeat, right? In this case I believe you can safely say you want the ability to print a line out with spaces and asterisks, right?

You should be able to have a method that takes in the current line number.. figures out how many spaces and asterisks it needs... if that line is the max line then stop the method.. if not increment the line number and call itself again.

A quick example...

A call from your main:
            MyRecursion(0, 8);


The method:
        public void MyRecursion(int lineNumber, int max)
        {
            if (lineNumber < max)
            {
                Console.WriteLine(lineNumber);
                MyRecursion(lineNumber+1, max);
            }
        }



Side note - use 'code' tags around your code above.
Side question - what's with the goofy statement?
private void mniAbout_Click(object sender, EventArgs e)
{
MessageBox.Show(" Tribute_Kronos™,code RmCi#A000000001ZCsPc-1.00 : States that this program shall not be copied and distributed to others, this product should only be viewed by the Administrators of Computer Science, the members of Tribute_Kronos™, and the essential client. Have a good day, and enjoy our first and finests product, because it's here where we treat our clients as our finest patrons.", "About", MessageBoxButtons.AbortRetryIgnore);
}

This post has been edited by modi123_1: 13 November 2010 - 03:01 PM

Was This Post Helpful? 0
  • +
  • -

#14 Tribute_Kronos™  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-November 10

Re: Asterisks Patterns in C#

Posted 13 November 2010 - 09:08 PM

Tribute_Kronos
Don't worry about it it's just some random thing that we have to do, the teacher says write goofy then we write goofy lawl. :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1