简体   繁体   中英

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";

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

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 :)

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;
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM