Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8794957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:19:18+00:00 2026-06-13T23:19:18+00:00

I need to create rectangles when the button is clicked. I have tried the

  • 0

I need to create rectangles when the button is clicked.
I have tried the given code but its not working.
i have tried to create a flag “mdraw” that becomes true when a button is clicked. a condition is out in the form_paint where the rectangle will created only when the “mdraw” becomes true. But after clicking the button, nothing comes!

I m using windows forms application (CLR).
Microsoft visual c++ 2008.

    #pragma once


    namespace try1 {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
///          'Resource File Name' property for the managed resource compiler tool
///          associated with all .resx files this class depends on.  Otherwise,
///          the designers will not be able to interact properly with localized
///          resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
    Form1(void)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Form1()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::Button^  button1;
protected: 

private:
    /// <summary>
    /// Required designer variable.
    /// </summary>
    System::ComponentModel::Container ^components;
private: System::Windows::Forms::Label^  label1;
public:
    bool mdraw;
    #pragma region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    void InitializeComponent(void)
    {
        this->button1 = (gcnew System::Windows::Forms::Button());
        this->label1 = (gcnew System::Windows::Forms::Label());
        this->SuspendLayout();
        // 
        // button1
        // 
        this->button1->Location = System::Drawing::Point(98, 35);
        this->button1->Name = L"button1";
        this->button1->Size = System::Drawing::Size(75, 23);
        this->button1->TabIndex = 0;
        this->button1->Text = L"button1";
        this->button1->UseVisualStyleBackColor = true;
        this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
        // 
        // label1
        // 
        this->label1->AutoSize = true;
        this->label1->Location = System::Drawing::Point(51, 77);
        this->label1->Name = L"label1";
        this->label1->Size = System::Drawing::Size(0, 13);
        this->label1->TabIndex = 1;
        // 
        // Form1
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(284, 262);
        this->Controls->Add(this->label1);
        this->Controls->Add(this->button1);
        this->Name = L"Form1";
        this->Text = L"Form1";
        this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
        this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::Form1_Paint);
        this->ResumeLayout(false);
        this->PerformLayout();

    }
    #pragma endregion
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)               {
             mdraw=true;
             label1->Text=button1->Text;

         }

private: System::Void Form1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
             Pen^ redPen = gcnew Pen(Color::Red);
         int i,n;
         i=20;
         n=0;

         if(mdraw)
         {
                       while(n<=50)
         {
             e->Graphics->DrawRectangle(redPen,i,140,10,60);
             i=i+20;
             n+=1;

         }




         }
     }


    };
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:19:19+00:00Added an answer on June 13, 2026 at 11:19 pm

    Use as below
    first initialize mdraw to false

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
    { 
        if(!mdraw)
        {
          while(n<=50)
          {
             mdraw=true;
             label1->Text=button1->Text;
             e->Graphics->DrawRectangle(redPen,i,140,10,60);
             i=i+20;
             n+=1;
           }
         }
    
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need create clone repository. but I do not know where can I get
I have dynamically created WrapPanel (_wp) with several Borders. And I need create handler
I need to create a WP site which will have multiple subjects. Each subject
I have a project in Python 2.7 and PyGTK. I need to create a
I need to create a gradient that is 230dp high (i.e., not the whole
I need to create a panel where I can put some rectangles and it
I have a situation where i need to create a left panel. My manager
I need to create rectangles that are rotated around their center (so they don't
I am working on a project where I need to create a boundary around
I need to solve the following problem: I have multiple rectangles of sizes: width

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.