ioSilent
ioSilent

Reputation: 1

How to delete one of existed rectangles In windows forms application

I have created some rectangles in pictureBox, and I want to delete one of them by clicking another button, not affecting other rectangles.I use "g->DrawRectangle()" to draw a rectangle but i can't delete one of them. I tried pitureBox1->Refresh() but it deleted all of my rectangles.All I want is to delete one of them.

How can I do that?Here's the code:

private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
{
    int x;
    int y;
    int length;
    int width;
    Color b; 

    Graphics^ g = pictureBox1->CreateGraphics();

    b = Color::FromArgb(Convert::ToInt32(textBox7->Text),Convert::ToInt32(textBox8->Text),Convert::ToInt32(textBox9->Text));
    Pen^ myPen = gcnew Pen(b);
    myPen->Width = 2.0F;

    x = Convert::ToInt32(textBox10->Text);
    y = Convert::ToInt32(textBox13->Text);
    length = Convert::ToInt32(textBox11->Text);
    width = Convert::ToInt32(textBox12->Text);

    //Rectangle 
    hh = Rectangle(x, y, length, width);    
    g->DrawRectangle(myPen,hh);

}

Upvotes: 0

Views: 1618

Answers (1)

Ryan
Ryan

Reputation: 1797

You need to keep a list of rectangle to draw, to delete one simply stop drawing it.

Example:

std::vector<Rectangle> rectangles;

void YourButtonClick(...){
  // do your stuff
  hh = Rectangle(x, y, length, width);
  rectangles.push_back(hh);

  draw();
}

void draw()
{
  pictureBox1->Refresh() 
  Graphics^ g = pictureBox1->CreateGraphics();
  for(int i = 0, max = rectangles.size(); i<max; i++){
    g->DrawRectangle(pen, rectangles[i]);
  }
}

void deleteRectangle(int index){
  Rectangle* rect = rectangles[index];
  rectangles.erase(rectangles.begin()+index);

  draw();
}

Upvotes: 1

Related Questions