Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. OnPaint problem

OnPaint problem

Scheduled Pinned Locked Moved C#
helpcssgraphicsquestionworkspace
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Guinness4Strength
    wrote on last edited by
    #1

    I am creating a UserControl that is a collection of Panles setup in a grid format. Each element of the grid is a Panel. I allow the user to add text to the panel in one of Nine locations described by the ContentAllingment Structure. The Text object is nothing more than a Label control. I'm using a gradient brush to paint the background of each panel in the grid and am setting the background color or each Label control to transparent. My problem is on the initial paintof the control, all the grid boxes are transparent until I move the window or resize the control. If I choose to not set the Label control's background property to transparent, then all the Panels get painted correctly, but the Label controls llok bad because they have a flat backgournd color while it's parent Panel gas a gradient background color. My question is, why would nothing be getting painted until the control is resized or moved. Here is the OnPaint handler override

    protected override void OnPaint(PaintEventArgs e)
    {
    try
    {
    //paint the background
    LinearGradientBrush brush;
    brush = new LinearGradientBrush(
    ClientRectangle.Location,
    new Point(ClientRectangle.Left,ClientRectangle.Bottom),
    this.BackColor,
    ControlPaint.Light(this.BackColor));
    e.Graphics.FillRectangle(brush, ClientRectangle);
    brush.Dispose();

    	//add labels here
    	foreach(PanelLabel PL in LabelArray)
    	{
    		PanelLabel lb=null;
    		bool LabelFound=false;
    		foreach(Control ctrl in this.Controls)
    		{	
    			if(ctrl is PanelLabel)
    			{
    				lb = (PanelLabel)ctrl;
    				if(lb!=null)
    				{
    					ContentAlignment CA = (ContentAlignment)lb.SimpleLocation;
    					if(lb.Text==PL.Text && CA==PL.SimpleLocation)
    					{
    						LabelFound=true;
    						break;
    					}
    				}
    			}
    			else
    				continue;
    		}
    		if(!LabelFound)
    		{
    			lb = new PanelLabel();
    			lb.Font=new Font(lb.Font,PL.TextStyle);
    			lb.Text=PL.Text;
    			lb.SimpleLocation=PL.SimpleLocation;
    			lb.BackColor=Color.Transparent;//comment out this line allows painting
    		}
    		lb.Bounds=SetLabelLocation(lb,PL.SimpleLocation,e.Graphics);
    		if(lb.Bounds!=Rectangle.Empty && lb.Bounds!=ClientRectangle)
    			this.Controls.Add(lb);
    		else 
    			throw new Exception("Label bounds are empty");
    	}
    	base.OnPaint (e);
    }
    catch(Exception Err)
    {
    	MessageBox.Show("On Paint Error:"+Err.Message);
    }
    

    }

    Any ideas are appreciated

    P 1 Reply Last reply
    0
    • G Guinness4Strength

      I am creating a UserControl that is a collection of Panles setup in a grid format. Each element of the grid is a Panel. I allow the user to add text to the panel in one of Nine locations described by the ContentAllingment Structure. The Text object is nothing more than a Label control. I'm using a gradient brush to paint the background of each panel in the grid and am setting the background color or each Label control to transparent. My problem is on the initial paintof the control, all the grid boxes are transparent until I move the window or resize the control. If I choose to not set the Label control's background property to transparent, then all the Panels get painted correctly, but the Label controls llok bad because they have a flat backgournd color while it's parent Panel gas a gradient background color. My question is, why would nothing be getting painted until the control is resized or moved. Here is the OnPaint handler override

      protected override void OnPaint(PaintEventArgs e)
      {
      try
      {
      //paint the background
      LinearGradientBrush brush;
      brush = new LinearGradientBrush(
      ClientRectangle.Location,
      new Point(ClientRectangle.Left,ClientRectangle.Bottom),
      this.BackColor,
      ControlPaint.Light(this.BackColor));
      e.Graphics.FillRectangle(brush, ClientRectangle);
      brush.Dispose();

      	//add labels here
      	foreach(PanelLabel PL in LabelArray)
      	{
      		PanelLabel lb=null;
      		bool LabelFound=false;
      		foreach(Control ctrl in this.Controls)
      		{	
      			if(ctrl is PanelLabel)
      			{
      				lb = (PanelLabel)ctrl;
      				if(lb!=null)
      				{
      					ContentAlignment CA = (ContentAlignment)lb.SimpleLocation;
      					if(lb.Text==PL.Text && CA==PL.SimpleLocation)
      					{
      						LabelFound=true;
      						break;
      					}
      				}
      			}
      			else
      				continue;
      		}
      		if(!LabelFound)
      		{
      			lb = new PanelLabel();
      			lb.Font=new Font(lb.Font,PL.TextStyle);
      			lb.Text=PL.Text;
      			lb.SimpleLocation=PL.SimpleLocation;
      			lb.BackColor=Color.Transparent;//comment out this line allows painting
      		}
      		lb.Bounds=SetLabelLocation(lb,PL.SimpleLocation,e.Graphics);
      		if(lb.Bounds!=Rectangle.Empty && lb.Bounds!=ClientRectangle)
      			this.Controls.Add(lb);
      		else 
      			throw new Exception("Label bounds are empty");
      	}
      	base.OnPaint (e);
      }
      catch(Exception Err)
      {
      	MessageBox.Show("On Paint Error:"+Err.Message);
      }
      

      }

      Any ideas are appreciated

      P Offline
      P Offline
      Palladino
      wrote on last edited by
      #2

      Don't I know I understood if well the problem, but and if you force the shot of the event?

      protected override void OnResize(EventArgs e)
      {
      base.OnResize (e);
      using (Graphics g = this.CreateGraphics())
      {
      this.OnPaint(new PaintEventArgs(g,
      this.ClientRectangle));
      }
      }

      protected override void OnMove(EventArgs e)
      {
      base.OnMove (e);
      using (Graphics g = this.CreateGraphics())
      {
      this.OnPaint(new PaintEventArgs(g,
      this.ClientRectangle));
      }
      }

      Marcelo Palladino Brazil

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups