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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
FroglegPosted Jun 24, 2011, 2:49 AM
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();
}
}
}
Ananth prasathPosted Jun 25, 2011, 3:48 AM
Sam HobbsPosted Jun 24, 2011, 4:24 PM
Suthish NairPosted Jun 24, 2011, 6:40 AM
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();
}