日期:2014-05-17  浏览次数:20991 次

如何直接打印textbox的内容?
我现在textbox中准备显示两行内容,想旁边做个按钮,按下后直接把textbox内容输出到打印机打印出来,怎么做?
谢谢

------解决方案--------------------
PrintDocument
------解决方案--------------------
在你的按钮单击事件下加入下面的代码:

C# code

            PrintDocument printDoc = new PrintDocument();
            PrintPreviewDialog ppvw = new PrintPreviewDialog();
            ppvw.Document = printDoc;
            //显示页面的打印预览
            printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
            printDoc.Print();

------解决方案--------------------
完整代码:
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;//打印命名空间

namespace Study
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void btnCommon_Click(object sender, EventArgs e)
        {
            PrintDocument printDoc = new PrintDocument();
            PrintPreviewDialog ppvw = new PrintPreviewDialog();
            ppvw.Document = printDoc;
            printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintTextValue);
            if (ppvw.ShowDialog() != DialogResult.OK)
            {
                printDoc.PrintPage -= new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintTextValue);//取消打印
                return;
            }
            printDoc.Print();
        }

        private void PrintDoc_PrintTextValue(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            try
            {
                e.Graphics.DrawString(richTextBox1.Text, new Font("宋体", 14, FontStyle.Bold), Brushes.Black, 30, 105);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}