使用文本框更改范围(createGraphics)

时间:2018-09-05 12:00:04

标签: c#

我正在尝试此操作,但不起作用。

我想用“ getal1”改变日食的范围。

这是代码:

private void panel1_Paint(object sender, PaintEventArgs e)
        {

            int getal1 = 0;

            SolidBrush sldBrush1 = new SolidBrush(Color.Red);
            Graphics tknn1 = panel1.CreateGraphics();
            tknn1.FillEllipse(sldBrush1, 0, 0, getal1, getal1);


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            int getal1 = int.Parse(textBox1.Text);

            textBox1.Text = getal1.ToString();
        }

1 个答案:

答案 0 :(得分:1)

根据@TaW的评论,这是一种实现方法。我建议为文本框添加一些更好的验证。您会看到this这样的问题,关于制作仅接受数字的TextBox。需要注意的一件事是,由于在您的代码中创建的是带有预定义红色的SolidBrush,因此您只需使用Brushes类中的静态红色画笔即可。

private void panel1_Paint(object sender, PaintEventArgs e)
{
    // Check if the text box can be parsed as an int and only
    // update the elipse if it is valid
    int getal1 = 0;
    if (int.TryParse(textBox1.Text, out getal1))
    {
        e.Graphics.FillEllipse(Brushes.Red, 0, 0, getal1, getal1);
    }
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    panel1.Invalidate();
}
相关问题