C# 知道为什么“我的打印”对话框出现两次吗?
当我点击按钮1时,它会打开一个打印对话框,但当我关闭时,它会打开另一个对话框。你知道(根据我的代码)为什么吗?我检查了我的代码,没有发现任何冗余。谢谢大家,初始化组件后+=是什么意思C# 知道为什么“我的打印”对话框出现两次吗?,c#,printing,C#,Printing,当我点击按钮1时,它会打开一个打印对话框,但当我关闭时,它会打开另一个对话框。你知道(根据我的代码)为什么吗?我检查了我的代码,没有发现任何冗余。谢谢大家,初始化组件后+=是什么意思 public partial class Print2 : Form { public Print2() { InitializeComponent(); this.button1.Click += button1_Click; this.printD
public partial class Print2 : Form
{
public Print2()
{
InitializeComponent();
this.button1.Click += button1_Click;
this.printDocument1.PrintPage += printDocument1_PrintPage;
}
private void Print2_Load(object sender, EventArgs e)
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
using (SqlConnection connection = new SqlConnection("Data Source=XXXX;Initial Catalog=XXXXX;Integrated Security=True"))
{
SqlCommand command =
new SqlCommand("select [CustomerName],[Inserted] from baf where id=(select max(id) from baf)", connection);
connection.Open();
SqlDataReader read = command.ExecuteReader();
while (read.Read())
{
textBox1.Text = (read["CustomerName"].ToString());
textBox2.Text = (read["Inserted"].ToString());
}
read.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.Document.DocumentName = "PictureTextDesignerFile1";
this.printPreviewDialog1.Size = new Size(1200, 800);
this.printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float neededWidth = 840;
float neededHeight = 1320;
float availableWidth = 840;
float availableHeight = 1320;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
Bitmap bmpPrint = null;
try
{
double multiplier = neededWidth / neededHeight;
double dblRef = availableWidth / availableHeight;
float zoom = 1;
if (multiplier >= dblRef)
{
zoom = availableWidth / neededWidth;
}
else
{
zoom = availableHeight / neededHeight;
}
bmpPrint = new Bitmap(1200, 900);
bmpPrint.SetResolution(300, 300);
this.DrawToBitmap(bmpPrint, new Rectangle(0, 0, 2000, 2800));
//this.DrawToBitmap(bmpPrint, new Rectangle(0, 0, bmpPrint.Width, bmpPrint.Height));
e.Graphics.Clip = new Region(e.MarginBounds);
e.Graphics.DrawImage(bmpPrint, e.MarginBounds.Left, e.MarginBounds.Top, Math.Min(neededWidth * zoom, availableWidth), Math.Min(neededHeight * zoom, availableHeight));
e.Graphics.Clip.Dispose();
}
catch
{
}
finally
{
if ((bmpPrint != null))
{
bmpPrint.Dispose();
bmpPrint = null;
}
}
}
您可能要添加
按钮1\u单击处理程序两次(在InitializeComponent中)。如果您在VS designer中创建了该表单,并从中连接了单击事件,则可能您已将按钮1\u单击
方法分配给desiger中的一个事件,并在.cotr
中分配了另一次方法代码>不需要这一行。谢谢。这是this.button1.Click+=button1\u Click。此命令所做的是+=?++=运算符订阅一个方法,该方法实现一个事件处理程序。请看,您可能正在添加按钮1\u单击处理程序两次(在InitializeComponent中)。如果您在VS designer中创建了该表单,并从中连接了单击事件,则可能您已将按钮1\u单击
方法分配给desiger中的一个事件,并在.cotr
中分配了另一次方法代码>不需要这一行。谢谢。这是this.button1.Click+=button1\u Click。此命令所做的是+=?++=运算符订阅一个方法,该方法实现一个事件处理程序。看见