为什么打印预览没有显示任何内容?

时间:2012-11-17 12:50:08

标签: c# winforms

我遇到打印预览对话框的问题。当我点击打印预览时,它没有显示我打印预览页面的任何内容。我创建了一种设置预览打印输出的方法。我无法弄清楚我在哪里错过了!

下面是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        // setup output
        Font printFont = new Font("Arial", 14);
        //print heading
        e.Graphics.DrawString("Select Name", printFont, Brushes.Black, 100, 100);

    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        // terminate application
        this.Close();
    }

    private void clearAllCategoriesToolStripMenuItem_Click(object sender, EventArgs e)
    {
        // clear all the list of the category 
        // first we use dialog box for confirmation request
        DialogResult confirmDialog = MessageBox.Show("Do you want to delete all category list ?","Clear Category List",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
        if (confirmDialog == DialogResult.Yes) 
        {
            // clearing the comboBox list
            categoryComboBox.Items.Clear();
        }
    }

    private void displayTheMovieCategoryCountToolStripMenuItem_Click(object sender, EventArgs e)
    {
        // count the number of category list
        int listCountInteger = categoryComboBox.Items.Count;
        MessageBox.Show("There are " + listCountInteger + " categories in the list", "ComboBox Count", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    private void removeACategoryToolStripMenuItem_Click(object sender, EventArgs e)
    {
        // remove a category name from list
        // first checking if a category has selected
        if (categoryComboBox.SelectedIndex == -1)
        {
            MessageBox.Show("Please Select a Category", "Wrong Selection", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else 
        {
            categoryComboBox.Items.RemoveAt(categoryComboBox.SelectedIndex);
        }
    }

    private void addACategoryToolStripMenuItem_Click(object sender, EventArgs e)
    {
        // add a category to list 
        // first checking for no duplicate name

        int indexNumberInteger=0;
        bool foundNameBoolean=false;

        if (categoryComboBox.Text == string.Empty)
        {
            //empty string has been entered
            MessageBox.Show("Please Enter the new category Name !", "Empty Name", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        { 
            while(indexNumberInteger<categoryComboBox.Items.Count && !foundNameBoolean)
            {
                if (categoryComboBox.Text.ToUpper() == categoryComboBox.Items[indexNumberInteger++].ToString().ToUpper())
                {
                    MessageBox.Show("This Category is already in the list, Please write a new one !", "Duplicate Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    foundNameBoolean = true;
                }

            }

            if (!foundNameBoolean)
            { 
                // add new name to category
                categoryComboBox.Items.Add(categoryComboBox.Text);
                categoryComboBox.Text = string.Empty;
                MessageBox.Show("Category has been updated !");
            }
        }

    }

    private void printTheCategoryListToolStripMenuItem_Click(object sender, EventArgs e)
    {
       // print preview
        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.ShowDialog();
    }
}

}

预览图片:

enter image description here

1 个答案:

答案 0 :(得分:3)

确保您的PrintPage事件已连线。

public Form1() {
  InitializeComponent();
  printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
}