0

I am trying this but it doesn't work.

I want to change the range of the eclipse with "getal1".

this is the code:

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
    Your `getal1` variable is local to both methods. You need to make it a class level variable. Also, in your `textBox1_TextChanged` method you probably do not want to set the text with the text that you just read from the textbox and you especially would not want to be setting the text in the `TextChanged` event. Something else to have in mind, `Int.Parse` will throw an exception if it cannot parse the text into an `int`. You may want to consider `int.TryParse` which will return a boolean telling you whether the parsing succeeded or not. – pstrjds Sep 05 '18 at 12:03
  • Winforms graphics basic rule #1 : Never use `control.CreateGraphics`! Never try to cache a `Graphics` object! Either draw into a `Bitmap bmp` using a `Graphics g = Graphics.FromImage(bmp)` or in the `Paint` event of a control, using the `e.Graphics` parameter you have!! - I would parse the number in the Paint event and trigger Painrt by doing a panel1.Invalidate in the TextChanged event. – TaW Sep 05 '18 at 12:39
  • @pstrjds Okey, i am just beginning with C# and don't understand your answer. Can you make your answer not as an comment but more in details in a answer. I tried what you said but it's not working. I put the variable not inside panel_paint1 but under initializeComponent(); . And i tried to change it to TryParse but the the whole syntax sentence is wrong. –  Sep 05 '18 at 13:32
  • @TaW I will keep that in mind after this is fixed –  Sep 05 '18 at 13:32
  • I got it, never mind. Thank you for your time –  Sep 05 '18 at 13:42
  • @TaW - added an answer based on your comments. – pstrjds Sep 05 '18 at 18:33

1 Answers1

1

Taking from @TaW's comment, here is a way to do this. I would recommend adding some better validation for the text box. You can see this SO question about making a TextBox that only accepts numbers. One thing to note, since in your code you were creating a SolidBrush with the predefined color red you can just use the static red brush from the Brushes class.

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();
}
pstrjds
  • 16,840
  • 6
  • 52
  • 61
  • _Graphics tknn1 = panel1.CreateGraphics();_ Delete this useless line, please! – TaW Sep 05 '18 at 18:39
  • 1
    @TaW - Done - that was leftover from copying. I thought I had deleted it, just missed it. Thanks! – pstrjds Sep 05 '18 at 19:04