1

このクラス Validators があり、WinForms プロジェクトのすべてのテキスト ボックスを検証します。方法がわからないのは、「検証に失敗したテキストボックスのボーダーカラーを変更できません」です。そこでLoginForm_Paint、同じクラス「バリデーター」でこのイベントを使用しました。使い方がわからない、そもそもあってはならないのかもしれない、使い方がわからないのかもしれない。誰か助けてくれませんか?

public void LoginForm_Paint(object sender, PaintEventArgs e)
{
    Graphics graphics = e.Graphics;
    Pen redPen = new Pen(Color.Red);
}

public bool ValidateTextBoxes(params TextBox[] textBoxes)
{
    foreach (var textBox in textBoxes)
    {
        if (textBox.Text.Equals(""))
        {
            textBox.BackColor = Color.Red;

            return false;
        }
    }

    return true;
}

私はこのように使いたかった(LoginFormのように):

public void LoginForm_Paint(object sender, PaintEventArgs e)
{
    Graphics graphics = e.Graphics;
    Pen redPen = new Pen(Color.Red);
}

public bool ValidateTextBoxes(params TextBox[] textBoxes)
{
    foreach (var textBox in textBoxes)
    {
        if (textBox.Text.Equals(""))
        {
            graphics.DrawRectangle(redPen, textBox.Location.X,
                          textBox.Location.Y, textBox.Width, textBox.Height);

            return false;
        }
    }

    return true;
}

しかし、そうはいきません。作成したインスタンスを認識しませんGraphics graphics = e.Graphics;

4

1 に答える 1

1

オブジェクトgraphicsは、使用しているメソッドの外部で定義されているため、「認識」されていません。つまり、 でローカルに定義され、LoginForm_Paintで使用されていValidateTextBoxesます。

ペイントしている TextBox のグラフィック オブジェクトを使用する必要があります。

public bool ValidateTextBoxes(params TextBox[] textBoxes)
{
    foreach (var textBox in textBoxes)
    {
        if (textBox.Text.Equals(""))
        {
            Graphics graphics = textBox.CreateGraphics();
            graphics.DrawRectangle(redPen, textBox.Location.X,
                          textBox.Location.Y, textBox.Width, textBox.Height);

            return false;
        }
    }

    return true;
}
于 2013-05-27T13:36:34.877 に答える