Draw ellipse with specify x,y,width, height

S

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);
}

}
}
 
S

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)
{

}

}
}
 
P

Peter Duniho

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
 
C

Chris Dunaway

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
 
M

MikeY

Yes Bob's pages are an excellent way of learning. Thats were I learnt how to
do my buttons
MikeY
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top