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'的定义,并且没有扩展方法'Wages'接受类型'WindowsFormsApplication1.Sinhala'的第一个参数可以找到(你是否缺少using指令或汇编引用?) D:\ yashpppp_modi \ WindowsFormsApplication1 \ Sinhala.cs 825 51 WindowsFormsApplication1
2.'WindowsFormsApplication1.Sinhala'不包含'RetirementPlanCheck'的定义,并且没有扩展方法'RetirementPlanCheck'接受类型为'WindowsFormsApplication1.Sinhala'的第一个参数'(你是否缺少using指令或程序集)参考?)D:\ yashpppp_modi \ WindowsFormsApplication1 \ Sinhala.cs WindowsFormsApplication1
答案 0 :(得分:1)
在C#Corner的示例中,您引用的代码段位于类Form1
的方法中。此类Form1
有两个名为Wages
和RetirementPlanCheck
的属性,定义为
public System.Windows.Forms.TextBox Wages;
public System.Windows.Forms.CheckBox RetirementPlanCheck;
您尝试使用它的类没有这些属性,这就是编译器所抱怨的。
您是否真的尝试从您提供的链接下载完整示例?它为我构建和运行没有任何问题。或者,如果您使用提供的示例遇到此问题,您是否可能会从表单中意外删除Wages
文本框和RetirementPlanCheck
复选框控件?
答案 1 :(得分:0)
您似乎没有任何名为Wages
或RetirementPlanCheck
的控件。
我猜这些实际上被称为其他东西,可能是txtWages
和chkRetirementPlan
..
查看你的代码,我认为你可以改变你的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)