user1926913
user1926913

Reputation:

Printing Strings

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

Answers (3)

magna_nz
magna_nz

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

kazem
kazem

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

C.J.
C.J.

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

Related Questions