错误:';名称printForm1在当前上下文中不存在';为什么会这样

本文关键字:不存在 为什么 上下文 名称 printForm1 错误 | 更新日期: 2023-09-27 18:29:19

嗨,我从MSDN获得了这段代码。我想不通这个问题,希望你能帮忙。错误位于包含单词printForm1的两行中。我的表单实际上被称为Form1,所以我看不到问题。

  [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
    private Bitmap memoryImage;
    private void CaptureScreen()
    {
        Graphics mygraphics = this.CreateGraphics();
        Size s = this.Size;
        memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        IntPtr dc1 = mygraphics.GetHdc();
        IntPtr dc2 = memoryGraphics.GetHdc();
        BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
        mygraphics.ReleaseHdc(dc1);
        memoryGraphics.ReleaseHdc(dc2);
    }
    private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 0, 0);
    }
    private void btnCreate_Click(object sender, EventArgs e)
    {
        btncustCont.Visible = false; 
        btnprintCost.Visible = false;
        btnpdfCont.Visible = false;
        btnpdfBrochureCont.Visible = false;
        btnpaperCont.Visible = false;
        btnduplicateCont.Visible = false;
        btntypeCont.Visible = false;
        btnCalcCost.Visible = false;
        btnprintCost.Visible = false;
        Application.DoEvents();
        CaptureScreen();
        printForm1.PrinterSettings.DefaultPageSettings.Landscape = true;
        printForm1.Print();
        btncustCont.Visible = true; 
        btnprintCost.Visible = true;
        btnpdfCont.Visible = true;
        btnpdfBrochureCont.Visible = true;
        btnpaperCont.Visible = true;
        btnduplicateCont.Visible = true;
        btntypeCont.Visible = true;
        btnCalcCost.Visible = true;
        btnprintCost.Visible = true;
    }

错误:';名称printForm1在当前上下文中不存在';为什么会这样

看看这篇MSDN文章是如何在类的顶部声明printDocument1的:

private PrintDocument printDocument1 = new PrintDocument();

http://msdn.microsoft.com/en-us/library/vstudio/6he9hz8c(v=vs.100).aspx

您可能想要做同样的事情,只需使用printForm1

我能想到的两种可能性:

如果我没有看错的话,printForm1是PrintDocument对象的一个实例。对不起,如果我在这里没有给你足够的信用,但你确定printForm1真的在任何地方申报了吗?

否则,请确保您的(codeBehind).cs文件实际上继承了包含此元素的页面。