Form graphics not set when form loads

My form has a group box which contains two overlapping rectangles. The form's other controls are two sets of four numeric up down controls to set the rectangles' colors. (nudF1,2,3 and 4 set the rectangle that's in front, and nudB1,2,3 and 4 set the rectangle that's behind.) Everything works fine, except that the rectangles do not display the colors set in the numeric up downs when the form first loads. The numeric up down controls' ChangeValue events all call the ShowColors() method. The form's Load event calls the csColorsForm_Load() method. Any suggestions?

namespace csColors { public partial class csColorsForm : Form { public csColorsForm() { InitializeComponent(); } private void csColorsForm_Load(object sender, EventArgs e) { this.BackColor = System.Drawing.Color.DarkBlue; SetColors(sender, e); } private void SetColors(object sender, EventArgs e) { Control control = (Control)sender; String ctrlName = control.Name; Graphics objGraphics; Rectangle rect1, rect2; int colorBack, colorFore; objGraphics = this.grpColor.CreateGraphics(); // If calling control is not a forecolor control, paint backcolor rectangle if (ctrlName.Substring(0,4)!="nudF") { colorBack = int.Parse(SetColorsB("nudB"), NumberStyles.HexNumber); SolidBrush BrushB = new SolidBrush(Color.FromArgb(colorBack)); rect1 = new Rectangle(this.grpColor.Left, this.grpColor.Top, this.grpColor.Width, this.grpColor.Height); objGraphics.FillRectangle(BrushB, rect1); } // Always paint forecolor rectangle colorFore = int.Parse(SetColorsB("nudF"), NumberStyles.HexNumber); SolidBrush BrushF = new SolidBrush(Color.FromArgb(colorFore)); rect2 = new Rectangle(this.grpColor.Left, this.grpColor.Top, this.grpColor.Width, this.grpColor.Height); objGraphics.FillRectangle(BrushF, rect2); objGraphics.Dispose(); } private string SetColorsB(string nam) { string txt=""; for (int n = 1; n <= 4; ++n) { var ud = Controls[nam + n] as NumericUpDown; int hex = (int)ud.Value; txt += hex.ToString("X2"); } return txt; } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } } }

-------------Problems Reply------------

I suspect that painting in the Load event is a little premature. Try painting in the Paint event.


Could you just use a couple of Panel controls, and set their background colors?

A group box, like any control, will paint itself when Windows sends it the WM_PAINT message. You can indeed use Control.CreateGraphics() to draw yourself, bypassing the normal paint logic. But that's not going to last and will randomly disappear when Windows decides that the control needs to repaint itself again. Which is obvious when you minimize the form and restore it. Less obvious on Vista and Win7 with Aero enabled, repaints are not necessary when your form is overlapped by another window. But it will be quite obvious on XP or with Aero disabled.

You cannot make this work reliably, you have to use the Paint event. Not your form's, the group box controls'. Call its Invalidate() method to force it to repaint when the color changes.

Category:c# Views:0 Time:2010-04-21

Related post

  • Clicking Icons(all the same "e" graphic) does not open or load anything 2012-01-20

    Out of nowhere, all the Icons on my desktop changed to flat white with the Internet explorer logo on them. When I click on them or even go to the programs menu to select a game or other application, a window appears "Do you want to open or save this

  • Android xml graphical layout/design not loaded properly of existing project 2011-10-22

    I am trying to import an existing android project, project xml graphical/design layout is not loaded . I made a new test project and the xml graphical layout working fine, I dont know what is the problem.I just installed new android sdk r14, I dont k

  • Fancybox 2: how to turn off loading graphic for img sequence? 2011-12-16

    I tried adding $.fancybox.hideLoading() in the $(document).ready but I still see the image loading graphic for each image that loads in the auto play sequence. Is there a way to suppress it? Note: using the Fancybox 2 API. --------------Solutions----

  • smart art graphics styles won't load spinning icon 2013-10-24

    I'm having the same problem others have posted, but I do not see the answer. When I try to add a smart art graphic, the submenus won't load and they just spin. I also find that I get blank documents that others have sent to me....when I know there is

  • Loading images into NSArray using initWithObjects crashes but not with an NSMutableArray? 2010-01-26

    I'm doing some lazy loading of images into an array when the app has loaded. I have tried using an NSMutableArray and an NSArray (I don't need to alter the array once it's been created) but the latter crashes on me. ... [self performSelectorInBackgro

  • How do I keep from running out of memory on graphics for an Android app? 2010-05-10

    I've been working on an Android app in Eclipse, and so far, my program hasn't really grown past midget size. However I've already run into an issue with an Out of Memory error. You see, I've been using graphics comprised solely of bitmaps and PNGs in

  • How to utilize 2d/3d Graphics Acceleration on a Single Board Computer 2011-03-25

    This may be a somewhat silly question, but if you are working with a single board computer that boasts that it has 2d/3d graphics acceleration, what does this actually mean? If it supports DirectX or OpenGL obviously I could just use that framework,

  • Taking a snapshot of a loader 2011-04-04

    I'm attempting to take an image from a networked camera and store it as a bitmap. I discovered that flash didn't support MJPEG so I found a class someone had written that could talk to the camera and output on the screen. Then I edited it so that I c

  • GWT Image Loading for Canvas 2011-05-01

    Without using any image loading library, I am loading images like below: final Image img = new Image(); img.setVisible(false); RootPanel.get().add(img); img.addErrorHandler(new ErrorHandler() { @Override public void onError(ErrorEvent event) { System

  • Windows Phone 7 XNA game quits on load, no error messages 2011-06-22

    I'm trying to get a Windows Phone 7 XNA game to run in the emulator, however it simply quits after calling the Game.Initialize function. The only output it gives is: A first chance exception of type 'System.NotSupportedException' occurred in Microsof

  • jquery manipulate ajax loading content 2011-08-28

    I pupulate with images a div by using ajax. In the index page I have the $(document).ready(function () { which manipulates the images loaded trough ajax. Should this work, because the ready function is alled before the images are loaded trough ajax -

  • Speedometer graphic help w/ jQuery and CSS3 2012-02-16

    I'm working on a project for shits and giggles to learn CSS3/jQuery animations. I have 10 icons and a speedometer graphic. On the page load the speedometer graphic is rotated to '-60deg' and when the mousemoves the speedometer graphic moves to a hard

  • Certain Pages not loading in IE 9 2012-02-12

    As of this last Sunday I have 2 websites that I go to regularly that no longer loads correctly in IE 9. Also now any page that has alot of data or graphics is taking forever to load. I have not done anything different and I haven't downloaded any upd

  • ATI graphics driver has stopped responding and has restarted sucessfully 2012-03-05

    This issue has been stated in other posts with no solution, but i was asked to start a new thread with my specific details so here it goes. I have a Acer laptop Aspire 8942G with a Intel core i3 CPU and 4 gigs of RAM with a ATI Radeon mobility HD 565

  • blurry photos and graphics 2012-05-05

    why are all the pictures and graphics on my screen so grainy and blurry? I have set the resolution as high as it will go but it does not change anything. I have windows XP home edition and internet explorer ver. 8. Also, it appears I have service pac

  • why do graphics from internet sites end up in my pictures file or my c drive? 2012-06-22

    If I do a search to find a certain picture I have transferred from my camera to my computer, I end sorthing through thousands of graphics from websites I have visited and advertisement, logos, etc. How can I eliminate these from being saved on my com

  • Windows 8.1 Graphics/Screen Problems 2012-11-25

    Samsung NP680Z5E-X01US laptop which has the Intel HD 4000/AMD Radeon 8700/8800 hybrid graphics system. After upgrade to 8.1: 1. System just booted to a black screen with the mouse pointer in the center, nothing else. Googling shows this was a problem

  • Photos sent to my email picks up additional graphics and attaches to it. 2013-01-12

    When I receive an email with a paperclip photo attached, the content adds about 100 to 350 additional photos and graphic images from websites I've recently visited to it. This is on my MSN, AOL, or Yahoo emails. Then when I foreward the photo to some

  • Are ATI Radeon graphics drivers digitally signed? 2013-04-05

    I choose the 'Enforce microsoft signature driver requirement' on startup. Does this mean the graphics drivers will not be loaded? --------------Solutions------------- Hi, Thanks for asking this question in Microsoft Community. I understand that you w

Copyright (C), All Rights Reserved.

processed in 0.112 (s). 11 q(s)