Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I need to do a program like this:

If I click on on the picturebox it will move to an other random location in panel1. then I again click and It move to an other random location. Can anyone help me.
Posted
Updated 13-Aug-14 11:18am
v2
Comments
Sergey Alexandrovich Kryukov 13-Aug-14 17:21pm    
Do you mean C++/CLI and System.Windows.Forms.PictureBox? Or something else? What is the problem, anyway? Don't you have random class and Left/Top properties? :-)
—SA

1 solution

I did it like this:


C++
MainForm(void)
		{
			InitializeComponent();
			this->pictureBox1->Click += gcnew System::EventHandler(this, &AI_Events::MainForm::OnClick);
			this->rnd = gcnew Random(1234);
			this->imgFile = gcnew String("D:\\Code\\star.png");
			this->Init();
		}


void MainForm::Init()
	{
		this->pictureBox1->SizeMode = PictureBoxSizeMode::Zoom;
		this->pictureBox1->Image = Image::FromFile(this->imgFile);
	}

void MainForm::OnClick(System::Object ^sender, System::EventArgs ^e)
	{
		Rectangle rt = this->panel1->Bounds;
		Rectangle rp = this->pictureBox1->Bounds;
		int xrnd = this->rnd->Next(rt.Left, rt.Right - rp.Width);
		int yrnd = this->rnd->Next(rt.Top, rt.Bottom - rp.Height);
		Point p(xrnd, yrnd);
		this->pictureBox1->Location = p;
		String^ t = xrnd.ToString();
		t += "," + yrnd.ToString();
		this->Msg(t);
	}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900