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

C# 为什么打印预览没有显示任何内容?,c#,winforms,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.Window

打印预览对话框有问题。当我点击打印预览时,它不会显示任何我打印的预览页面。我创建了一个设置预览打印输出的方法。我不知道我错过了哪里

下面是我的代码:

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

和预览图片:


确保PrintPage事件已连接

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

确保PrintPage事件已连接

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

这对我有用。确保PrintPage事件已连接到printDocument1对象。@LarsTech我该怎么做?它对我有效。确保PrintPage事件已连接到printDocument1对象。@LarsTech我该怎么做?