Reputation:
I am working on a program in which I need to print out a string, I have the string ready, I just need to know how to print, I've never done it so i don't know where to start any ideas? this is what I have so far but I don't know where to go from here
PrintDocument output = new PrintDocument();
output.DocumentName = "Test Results";
Upvotes: 3
Views: 73116
Reputation: 1273
You can print with Console.WriteLine("text")
to standard output.
Have a look at string interpolation to make adding variables to string far easier. This was introduced in C# 6 and has following syntax.
Console.Writeline($"I am {cool}");
where cool
is the variable name :)
Upvotes: 1
Reputation: 3749
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace Test
{
public class Print
{
PrintDocument myPrintDocument = new PrintDocument();
public void ShowPrintDialog()
{
PrintDialog myDialog = new PrintDialog();
myDialog.Document = myPrintDocument;
if(myDialog.ShowDialog() == DialogResult.OK)
Print();
}
protected override void OnPrintPage(PrintPageEventArgs e)
{
e.Graphics.DrawString("Your String To Print In Document", this.Font, Brushes.Red, new PointF());
e.HasMorePages = false;
}
}
}
Upvotes: -1
Reputation: 16111
Your definition of 'Print' is pretty vague. The most common meaning of print in this part of the internet is to print or display text to the console or command window.
So, if you want to print to a console window you use methods on the System.Console class:
http://msdn.microsoft.com/en-us/library/system.console.writeline.aspx
For example:
String yourname = "Mr. Nice";
Console.WriteLine("Hello {0}", yourname);
would display:
Hello Mr. Nice
Upvotes: 1