Hi
Can any one tell me how can i print this format in c#
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
FroglegRank #1,020 · 1.9K reputationPosted Jun 23, 2011, 10:49 PM EDT
Add printPreviewDialog and a printDocument to your form
namespace StarPrint
{
public partial class Form1 : Form
{
string[] str = new string[5];
Font myFont;
int strLength=0,strCenter = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
myFont = new Font("Arial", 36);
loadString();
}
public void loadString()
{
str[0]="*";
str[1]="***";
str[2]="*****";
str[3]="*******";
str[4]="*********";
strLength = measureChar(str[4]);
label1.Text = Convert.ToString(strLength);
strCenter = (strLength / 2) + 20;
}
private void button1_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int lineSpace=20,isX = 0,wordWidth = 0;
for (int i = 0; i < 5; i++)
{
wordWidth= measureChar(str[i]);
isX = strCenter -(wordWidth / 2) ;
e.Graphics.DrawString(str[i], myFont, Brushes.Black, isX, lineSpace);
lineSpace=lineSpace+20;
}
e.Graphics.DrawLine(Pens.Red, strCenter, 20, strCenter, 150);
}
public int measureChar(string strmeasure)
{
Graphics g = this.CreateGraphics();
SizeF sz = new SizeF();
sz = g.MeasureString(strmeasure, myFont);
int strWidth = Convert.ToInt32(sz.Width);
return strWidth;
}
private void button2_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
}
}
Suthish NairRank #92 · 22.8K reputationPosted Jun 24, 2011, 2:40 AM EDT
static void Main(string[] args)
{
int j = 1;
for (int x = 0; x < 9; x++)
{
for (int i = x; i < 5; i++)
{
if (i < 4)
{
Console.Write(" ");
}
else
{
for (int jj = 0; jj < j; jj++)
{
Console.Write("*");
}
j = j + 2;
Console.WriteLine();
}
}
}
Console.ReadLine();
}
Sam HobbsRank #107 · 20.5K reputationPosted Jun 24, 2011, 12:24 PM EDT
Ananth prasath24 reputationPosted Jun 24, 2011, 11:48 PM EDT