2012-11-17 77 views
1

我遇到了打印預覽對話框的問題。當我點擊打印預覽時,它不會顯示我打印預覽頁面的任何內容。我創建了一個方法來設置預覽打印輸出。我無法弄清楚我在哪裏錯過!下面爲什麼打印預覽不顯示任何內容?

是我的代碼:

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

它爲我工作。確保您的PrintPage事件連接到printDocument1對象。 – LarsTech

+0

@LarsTech我該怎麼做? – user1725155

回答

3

確保您的PrintPage事件的接線。

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

非常感謝 – user1725155

相關問題