Home All Groups Group Topic Archive Search About

Draw ellipse with specify x,y,width, height

Author
29 Nov 2007 4:52 AM
Slickuser
I am trying to draw an ellipse with specify x,y,width, and height for
input argument in C# .net (Visual Studio 2005).

It seem it will not draw for me. What am I missing?

See the code below, thanks.


//CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {

            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            draw(10, 20, 20, 50);
            draw(50, 80, 50, 150);
        }

        private void draw(int x1, int y1, int w, int h)
        {
            Graphics g = this.CreateGraphics();
            g.FillEllipse(Brushes.Blue, x1, y1, w, h);
        }

    }
}

Author
29 Nov 2007 5:46 AM
Slickuser
I got it to work but when I added in the GroupBox.

It show in the back and group is over it. Is there a way set it to
back or show the drawing the group box as well?

Here is the update code:

///code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//using System.Media;

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {

            InitializeComponent();
            draw(50, 80, 50, 150);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            draw(10, 20, 20, 50);

            //SoundPlayer simpleSound = new SoundPlayer(@"C:
\911.wav");
           // simpleSound.Play();

        }

        private void draw(int x1, int y1, int w, int h)
        {
            Graphics dc = this.CreateGraphics();
            this.Show();

            dc.FillEllipse(Brushes.Blue, x1, y1, w, h);

            Pen RedPen = new Pen(Color.Red, 2);
            dc.DrawEllipse(RedPen, x1, y1, w, h);
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

    }
}
Author
29 Nov 2007 8:22 AM
Peter Duniho
On 2007-11-28 21:46:16 -0800, Slickuser <slick.us***@gmail.com> said:

> I got it to work but when I added in the GroupBox.
>
> It show in the back and group is over it. Is there a way set it to
> back or show the drawing the group box as well?

You are doing it wrong.  You cannot just draw whenever you want.  You
need to override the OnPaint() method, or subscribe to the Paint event,
and only draw there.

Search on those names ("OnPaint" and "Paint") in this newsgroup for a
number of related threads, including one that is as recent as today and
includes very specific details regarding the correct way to draw in a
form.

Pete
Author
29 Nov 2007 2:25 PM
Chris Dunaway
On Nov 29, 2:22 am, Peter Duniho <NpOeStPe***@NnOwSlPiAnMk.com> wrote:
Show quote
> On 2007-11-28 21:46:16 -0800, Slickuser <slick.us***@gmail.com> said:
>
> > I got it to work but when I added in the GroupBox.
>
> > It show in the back and group is over it. Is there a way set it to
> > back or show the drawing the group box as well?
>
> You are doing it wrong.  You cannot just draw whenever you want.  You
> need to override the OnPaint() method, or subscribe to the Paint event,
> and only draw there.
>
> Search on those names ("OnPaint" and "Paint") in this newsgroup for a
> number of related threads, including one that is as recent as today and
> includes very specific details regarding the correct way to draw in a
> form.
>
> Pete

Or you can try this link and look at the number 1 FAQ:

http://www.bobpowell.net/faqmain.htm

Chris
Author
29 Nov 2007 5:17 PM
MikeY
Yes Bob's pages are an excellent way of learning. Thats were I learnt how to
do my buttons
MikeY

Show quote
"Chris Dunaway" <dunaw***@gmail.com> wrote in message
news:3d8f45fb-8027-4941-8f6b-3c8a0818c1cc@x69g2000hsx.googlegroups.com...
> On Nov 29, 2:22 am, Peter Duniho <NpOeStPe***@NnOwSlPiAnMk.com> wrote:
>> On 2007-11-28 21:46:16 -0800, Slickuser <slick.us***@gmail.com> said:
>>
>> > I got it to work but when I added in the GroupBox.
>>
>> > It show in the back and group is over it. Is there a way set it to
>> > back or show the drawing the group box as well?
>>
>> You are doing it wrong.  You cannot just draw whenever you want.  You
>> need to override the OnPaint() method, or subscribe to the Paint event,
>> and only draw there.
>>
>> Search on those names ("OnPaint" and "Paint") in this newsgroup for a
>> number of related threads, including one that is as recent as today and
>> includes very specific details regarding the correct way to draw in a
>> form.
>>
>> Pete
>
> Or you can try this link and look at the number 1 FAQ:
>
> http://www.bobpowell.net/faqmain.htm
>
> Chris
>

AddThis Social Bookmark Button