hi,
how can one Click on a button, then by using Mouse click onywhere on a Form or Panel in Form created object like a Circle?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
FroglegPosted Jun 30, 2011, 12:33 AM
But basically all he needs to do is set a point for the rectangle and call refresh of the panel
Edit here's the code. The timer only changes the circle color
Sam HobbsPosted Jun 29, 2011, 9:37 PM
- process a button click and set a flag or something indicating you will create a circle when the mouse is clicked
- process a mouse click and determine where the click occured
- draw a circle at a specified location
Then all you must do is to figure out how to do each one.Note that Frogleg's code has a timer and I don't think you need a timer, but probably the timer is used instead of a paint function.
Do you neeed to have just one circle and never more than one circle? Or do you need to have multiple circles, one for each time that the mouse is clicked or the button and the mouse are clicked? If you need more than one circle then I think you need a little more than what Frogleg shows.
You should look at the articles in this web site; what you are describing is the type of thing that is likely already described in articles.
FroglegPosted Jun 29, 2011, 7:42 PM
namespace PanelCircle
{
public partial class Form1 : Form
{
Rectangle rct;
Color myColor;
Brush myBrush;
Random rand;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
rct = new Rectangle(50, 50, 200, 200);
rand = new Random();
myColor = Color.Green;
setBrush();
}
private void panel1_Paint_1(object sender, PaintEventArgs e)
{
e.Graphics.FillEllipse(myBrush, rct);
}
public void setBrush()
{
int red = setColor();
int blue = setColor();
int green = setColor();
myColor = Color.FromArgb(red, blue, green);
myBrush = new System.Drawing.SolidBrush(myColor);
}
public int setColor()
{
int theColor = 0;
theColor = rand.Next(0,255);
return theColor;
}
private void bttnStart_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void bttnStop_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
setBrush();
panel1.Refresh();
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
Point p = new Point();
p = setCenter(e.X, e.Y);
rct = new Rectangle(p.X, p.Y, 200, 200);
panel1.Refresh();
}
public Point setCenter(int x,int y)// work out center of circle
{
Point p = new Point();
p.X = x - 100;
p.Y = y - 100;
return p;
}
}
}