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

  • SEARCH
  • Home
  • 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 7837721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:49:01+00:00 2026-06-02T14:49:01+00:00

I have problem with my C# winform project. I have function that draw squares:

  • 0

I have problem with my C# winform project.

I have function that draw squares:

public void DrawingSquares(int x, int y)
{
  System.Drawing.Graphics graphicsObj;
  graphicsObj = this.CreateGraphics();
  Pen myPen = new Pen(System.Drawing.Color.Black, 5);
  Rectangle myRectangle = new Rectangle(x, y, 100, 100);
  graphicsObj.DrawRectangle(myPen, myRectangle);
}

private void button1_Click(object sender, EventArgs e)
{
  z = Convert.ToInt16(textBox1.Text)-1;
  k = Convert.ToInt16(textBox2.Text)-1;
  DrawAllSquares();
}

private void DrawAllSquares()
{
  int tempy = y;
  for (int i = 0; i < z; i++)
  {
    DrawingSquares(x, y);
    for (int j = 0; j < k - 1; j++)
    {
      tempy += 50;
      DrawingSquares(x, tempy);
    }
    x += 50;
    tempy = y;
  }
}

In my project, I have a function that I use to move button around the form at runtime, but when the button is moved onto the drawing the drawing is deleted.

What can I do to make the drawing permanent?

  • 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-02T14:49:03+00:00Added an answer on June 2, 2026 at 2:49 pm

    If you need permanently (in terms of application life time), by any means, you need to use it inside you Control's (the Control where rectangle is have to be drawn), OnPaint method.

    If you need an animation too: it could be resolved by using a timer and changing coordinates that you pass like a parameters to your DrawSquares.

    Hope this helps.

    EDIT

    A pseudocode:

    public class MyControl : Control 
    {
        public override void OnPaint(PaintEventArgs e)
        {
           base.OnPaint(e); 
    
           DrawingSquares(e.Graphics, valueX, valueY);
        }
    
        public void DrawingSquares(Graphics graphicsObj, int x, int y)
        {      
           Pen myPen = new Pen(System.Drawing.Color.Black, 5);
           Rectangle myRectangle = new Rectangle(x, y, 100, 100);
           graphicsObj.DrawRectangle(myPen, myRectangle);
        }
    
    }
    

    valueX and valueY are relative X and Y coordinates where you want the rectangle to be drawn.

    These coordinates can be constant values, or you can change them from some timer and call Invalidate() on MyControl, so paint will be executed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem in my C# winform project. In my project I have
I have a winform project that is using a devexpress layout control and I
i am new in project for company that have bigger enterprise/accounting system based on
I have a WinForm project that I want to be usable as a console
I have a problem with winform application which uses threads to update the UI.
I have simple WinForms application where modifying Windows Registry. The problem is that in
I have problem in some JavaScript that I am writing where the Switch statement
I have a project where a user for the company will use a winform
Project: Winforms, .NET framework: 3.5 (Visual Studio 2008) My problem is: I have a
Project Structure I have a silverlight project SLProj, that references a silverlight class library

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.