-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

エラーは参照が欠落していることを示していますが、それらはどのタイプの参照ですか?この問題を解決するのを手伝ってください。

エラーMSG1.「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には、 と という 2 つの属性がWagesありRetirementPlanCheck、次のように定義されます。

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

これを使用しようとしているクラスにはこれらの属性がなく、それがコンパイラーの不満です。

あなたが提供したリンクから完全な例を実際にダウンロードしようとしましたか? それは私にとって何の問題もなく構築され、実行されました。または、提供された例でこの問題に遭遇した場合、フォームから誤ってWagesテキストボックスとRetirementPlanCheckチェックボックスのコントロールを削除したのでしょうか?

于 2012-07-18T02:57:54.963 に答える
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)
于 2012-07-18T02:55:19.613 に答える