4 Replies - 15193 Views - Last Post: 20 March 2011 - 05:17 PM

#1 Cyclone  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 13-February 09

Deleting Picturebox from memory - Dispose function

Posted 19 March 2011 - 11:24 AM

Right now my program creates instances of picturebox control by pressing the up arrow. They are animated to move upwords. I'm trying to use the dispose function to remove them from memory after a set amount of time. I tried the Dispose() function but I get errors. Thanks.

#include "stdafx.h"
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;



public ref class object
{
	public:
			PictureBox^ Box3;
			Timer^ Timer2;
			int t;
			
			object( Form ^ form ) // Class Constructor.
			{
				Timer2 = gcnew Timer;
				Timer2->Interval = 1;
				Timer2->Start();
				Box3 = gcnew PictureBox();
				Box3->Left = 150;
				Box3->Top = 240;
				Box3->Width = 10;
				Box3->Height = 10;
				Box3->BackColor = System::Drawing::Color::Red;
				form->Controls->Add(Box3);
				Timer2->Tick += gcnew System::EventHandler(this, &object::timer2_Tick);

			}

		System::Void timer2_Tick(System::Object^  sender, System::EventArgs^  e)
		{
			t += 1;
			Box3->Top -= 10;
		}
};

public ref class missile
{
	public:
			PictureBox^ Box1;
			Timer^ Timer1;
			int t;
			
			missile( Form ^ form ) // Class Constructor.
			{
				Timer1 = gcnew Timer;
				Timer1->Interval = 1;
				Timer1->Start();
				Box1 = gcnew PictureBox();
				Box1->Left = 150;
				Box1->Top = 240;
				Box1->Width = 10;
				Box1->Height = 10;
				Box1->BackColor = System::Drawing::Color::Blue;
				form->Controls->Add(Box1);
				Timer1->Tick += gcnew System::EventHandler(this, &missile::timer1_Tick);

			}

		System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e)
		{
			t += 1;
			Box1->Top -= 1;
		}
};


public ref class Form1 : public Form 
{
	public:
			PictureBox^ Box2;

			Form1() // Class constructor.
			{	
				Box2 = gcnew PictureBox();
				Box2->BackColor = Color::Blue;
				Box2->Top = 240;
				Box2->Width = 40;
				Box2->Height = 10;
				this->Controls->Add(Box2);
				this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form_KeyDown);
			}

			System::Void Form_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e)
			{	missile ^ m1;
				object ^ o1;
				
				if(e->KeyCode == Keys::Up) {m1 = gcnew missile(this); m1->Box1->Left = Box2->Left + (Box2->Width / 2) - 10; }
				if(e->KeyCode == Keys::Left){Box2->Left -= 4;}
				if(e->KeyCode == Keys::Right){Box2->Left += 4;}
				if(e->KeyCode == Keys::Down) {o1 = gcnew object(this); o1->Box3->Left = Box2->Left + (Box2->Width / 2) - 10; }
			}		
};


[STAThread]
int main()
{	
    Application::Run(gcnew Form1());
}




Is This A Good Question/Topic? 0
  • +

Replies To: Deleting Picturebox from memory - Dispose function

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Deleting Picturebox from memory - Dispose function

Posted 20 March 2011 - 08:46 AM

What the specific errors?
Was This Post Helpful? 0
  • +
  • -

#3 Cyclone  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 13-February 09

Re: Deleting Picturebox from memory - Dispose function

Posted 20 March 2011 - 12:08 PM

I want to delete the m1,m2 objects after they collide with the larger box. Using m1->Box1->Dispose(); throws this error.

error C2039: 'Dispose' : is not a member of 'System::Windows::Forms::PictureBox'

Also wouldn't that just delete the box(picturebox) and not the actual m1 object?


#include "stdafx.h"
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;

public ref class object
{
	public:
			PictureBox^ Box3;
			
			object( Form ^ form ) // Class Constructor.
			{
				Box3 = gcnew PictureBox();
				Box3->Left = 150;
				Box3->Top = 50;
				Box3->Width = 100;
				Box3->Height = 100;
				Box3->BackColor = System::Drawing::Color::Red;
				form->Controls->Add(Box3);
			}
};

public ref class missile
{
	public:
			PictureBox^ Box1;
			Timer^ Timer1;

			missile( Form ^ form ) // Class Constructor.
			{
				Timer1 = gcnew Timer;
				Timer1->Interval = 1;
				Timer1->Start();
				Box1 = gcnew PictureBox();
				Box1->Left = 150;
				Box1->Top = 240;
				Box1->Width = 10;
				Box1->Height = 10;
				Box1->BackColor = System::Drawing::Color::Blue;
				form->Controls->Add(Box1);
				Timer1->Tick += gcnew System::EventHandler(this, &missile::timer1_Tick);
			}

		System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e)
		{
			Box1->Top -= 1;
		}
};


public ref class Form1 : public Form 
{
	public:
			PictureBox^ Box2;
			missile^ m1;
			missile^ m2;
			object^ o1;
			Timer^ Timer3;
			Label^ l1;
			int mcount;
			bool mcount1;
			bool mcount2;

			Form1() // Class constructor.
			{	
				l1 = gcnew Label();
				l1->Text = "0";
				mcount = 0;
				mcount1 = false;
				mcount2 = false;
				Timer3 = gcnew Timer();
				Timer3->Interval = 1;
				Timer3->Start();
				Box2 = gcnew PictureBox();
				Box2->BackColor = Color::Blue;
				Box2->Top = 240;
				Box2->Left = (this->Width / 2) - 40;
				Box2->Width = 40;
				Box2->Height = 10;
				this->Controls->Add(Box2);
				this->Controls->Add(l1);
				this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form_KeyDown);
				Timer3->Tick += gcnew System::EventHandler(this, &Form1::timer3_Tick);
			}

			System::Void Form_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e)
			{	
				if(e->KeyCode == Keys::Up) // Check if m1,m2 objects exist or not. If they don't then create them.
				{
					mcount += 1;
					l1->Text = mcount.ToString();
					if(mcount == 1 && !m1){mcount1 = true; m1 = gcnew missile(this);m1->Box1->Left = Box2->Left + (Box2->Width / 2) - 10;}
					if(mcount == 2 && !m2){mcount=0;mcount2 = true;m2 = gcnew missile(this);m2->Box1->Left = Box2->Left + (Box2->Width / 2) - 10;}
					if(mcount == 2){mcount=0;}
				}
				
				if(e->KeyCode == Keys::Left){Box2->Left -= 4;}
				if(e->KeyCode == Keys::Right){Box2->Left += 4;}
				if(e->KeyCode == Keys::Down) {o1 = gcnew object(this);}
			}

			System::Void timer3_Tick(System::Object^  sender, System::EventArgs^  e)
			{
				if(mcount1 == true)
				{
					if(m1->Box1->Top == o1->Box3->Bottom){m1->Box1->BackColor = Color::Green;m1->Box1->Dispose();}	
				}
				
				if(mcount2 == true)
				{
					if(m2->Box1->Top == o1->Box3->Bottom){m2->Box1->BackColor = Color::Red;}
				}
			}

			bool isColliding(PictureBox^ box1, PictureBox^ box3)
			{
				Rectangle r1 = m1->Box1->Bounds;
				Rectangle r2 = o1->Box3->Bounds;
				return r1.IntersectsWith(r2); //return true if they collide
			}	
};


[STAThread]
int main()
{	
    Application::Run(gcnew Form1());
}



Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Deleting Picturebox from memory - Dispose function

Posted 20 March 2011 - 12:34 PM

delete ^ m1;

Was This Post Helpful? 0
  • +
  • -

#5 Cyclone  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 13-February 09

Re: Deleting Picturebox from memory - Dispose function

Posted 20 March 2011 - 05:17 PM

Thanks. That gave me an error. I changed the code(with some help). It now uses a list to hold the objects and remove them when necessary.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1