-1

http://www.c-sharpcorner.com/uploadfile/mgold/printingw2form09162005061136am/printingw2form.aspx

       for (int i = 0; i < this.Controls.Count; i++)
        {
            // Check if its a TextBox type by comparing to the type of one of the textboxes
            if (Controls[i].GetType() == this.Wages.GetType())
            {
                // Unbox the Textbox
                TextBox theText = (TextBox)Controls[i];
                // Draw the textbox string at the position of the textbox on the form, scaled to the print page
                g.DrawString(theText.Text, theText.Font, Brushes.Black, theText.Bounds.Left * scalex, theText.Bounds.Top * scaley, new StringFormat());
            }
            if (Controls[i].GetType() == this.RetirementPlanCheck.GetType())
            {
                // Unbox the Checkbox
                CheckBox theCheck = (CheckBox)Controls[i];
                // Draw the checkbox rectangle on the form scaled to the print page
                Rectangle aRect = theCheck.Bounds;
                g.DrawRectangle(aPen, aRect.Left * scalex, aRect.Top * scaley, aRect.Width * scalex, aRect.Height * scaley);
                // If the checkbox is checked, Draw the x inside the checkbox on the form scaled to the print page
                if (theCheck.Checked)
                {
                    g.DrawString("x", theCheck.Font, Brushes.Black, theCheck.Left * scalex + 1, theCheck.Top * scaley + 1, new StringFormat());
                }
            }

}

我将此代码用于打印预览,但它给出了一个错误

if (Controls[i].GetType() == this.RetirementPlanCheck.GetType())//RetirementPlanCheck

 if (Controls[i].GetType() == this.Wages.GetType())// wages

错误说缺少参考,那么这些参考是什么类型的?请帮我解决这个问题。

错误消息 1.“WindowsFormsApplication1.Sinhala”不包含“Wages”的定义,并且找不到接受“WindowsFormsApplication1.Sinhala”类型的第一个参数的扩展方法“Wages”(您是否缺少 using 指令或程序集引用?) D:\yashpppp_modi\WindowsFormsApplication1\Sinhala.cs 825 51 WindowsFormsApplication1

2.“WindowsFormsApplication1.Sinhala”不包含“RetirementPlanCheck”的定义,并且找不到接受“WindowsFormsApplication1.Sinhala”类型的第一个参数的扩展方法“RetirementPlanCheck”(您是否缺少 using 指令或程序集引用?) D:\yashpppp_modi\WindowsFormsApplication1\Sinhala.cs WindowsFormsApplication1

4

2 回答 2

1

在 C# Corner 的示例中,您引用的代码片段位于 class 的方法中Form1。这个类Form1确实有两个名为Wagesand的属性RetirementPlanCheck,定义为

public System.Windows.Forms.TextBox Wages;
public System.Windows.Forms.CheckBox RetirementPlanCheck;

您尝试使用它的类没有这些属性,这就是编译器所抱怨的。

您是否真的尝试从您提供的链接下载完整示例?它的构建和运行对我来说没有任何问题。或者,如果您确实在提供的示例中遇到了这个问题,您是否可能不小心从表单中删除了Wages文本框和复选框控件?RetirementPlanCheck

于 2012-07-18T02:57:54.963 回答
0

看起来您没有任何名为Wagesor的控件RetirementPlanCheck

我猜那些实际上被称为别的东西,也许txtWageschkRetirementPlan..

查看您的代码,我认为您可能可以更改您的 if 语句以避免这种情况:

//if (Controls[i].GetType() == this.Wages.GetType())
if (Controls[i] is TextBox)
...
//if (Controls[i].GetType() == this.RetirementPlanCheck.GetType())
if (Controls[i] is CheckBox)
于 2012-07-18T02:55:19.613 回答