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 9178217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:30:01+00:00 2026-06-17T17:30:01+00:00

Hello it might be a silly question but i can’t figure out the problem

  • 0

Hello it might be a silly question but i can’t figure out the problem here.. here is my code to fill a form with a single block:

    private void drawBackground()
{
    Graphics g = genPan.CreateGraphics();
    Image Block = Image.FromFile(@"C:\Users\Administrator\Desktop\movment V1\movment V1\images\BrownBlock.png");
    float recWidth = Block.Width; 
    // rectangle width didnt change the name from previous code, it's picture width.

    float recHeight = Block.Height;
    // rectangle Heightdidnt change the name from previous code, it's picture Height.

    float WinWidth = genPan.Width; // genPan is a panel that docked to the form

    float WinHeight = genPan.Height;

    float curWidth = 0; //indicates where the next block will be placed int the X axis
    float curHeight = 0;//indicates where the next block will be placed int the Y axis

    while ((curHeight + recHeight) <= WinHeight)
    {
        if (curWidth >= WinWidth / 3 || curWidth <= WinWidth / 1.5 ||
            curHeight >= WinHeight / 3 || curHeight <= WinHeight / 1.5)
        {
            g.DrawImage(Block, curWidth, curHeight, recWidth , recHeight );
        }
        curWidth += recWidth;
        if ((WinWidth - curWidth) < recWidth)
        {
            curWidth = 0;
            curHeight += 50;
        }
    }
}

If I launch this func through a button it will work perfectly fine. But if I launch the func after the InitializeComponent(); method in the constructor OR in a FORM shown event, while the button is still on the form it will execute the func however the block backgroud wont be visible but the grey color will be. but if i remove the button the background will be visible. =\

I cant understand why is it happening, how to fix it and what am I doing wrong.. can anyone explain please..?

  • 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-17T17:30:02+00:00Added an answer on June 17, 2026 at 5:30 pm

    You can’t really do that using your current logic. The problem is that the control (genPan panel in your case) has its own Paint event that when called, overwriting any graphics you used on it.

    Even when you draw in button click it works only until the form is repainted e.g. try focus other window and focus your form again: you will lose what you have drawn.

    Proper way to do such things is to write your own class that inherit from some basic control (Panel in your case) then overriding its OnPaint event and draw whatever you want there.

    So first, have such class:

    public class BlockBackgroundPanel : Panel
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Image Block = Image.FromFile(@"C:\Users\Administrator\Desktop\movment V1\movment V1\images\BrownBlock.png");
            float recWidth = Block.Width; 
            //rest of your code, replace "genPan" with "this" as you are inside the Panel
        }
    }
    

    Then in your .Designer.cs file (you can open it in the Studio) change the code so that genPan will be of your new class instance:

    private BlockBackgroundPanel genPan;
    //...
    this.genPan = new BlockBackgroundPanel ();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello this might be really silly question but I am trying to make markers
hello there and sorry for the stupid question but i think i might be
Might be an unclear question so here's the code and explanation: Document doc =
Hello I want to create a simple blog where users can post comments, but
Hello I'm trying to dynamically generate some inputs for my form, but it's not
Hello having a bit of problem retrieving a value so i might use it.
Hello this is probably a typical question but i cant seem to find a
Hello and thanks for checking out my question. <meta property=og:image content=http://www.myurl.com/images/test.png/> I have an
This might seem like a very simple question, but I am struggling with it.
This might come across as a very silly question. I am trying to place

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.