locked
how to update custom control properties at design time immediately ? RRS feed

  • Question

  • I create a custom control which inherit from panel control using c# class library then i used it inside a form. when I select any of properties during design time, my custom control doesn't update ! if i close the form and i reopen it changes will take place !

    i want to have the control updated when any of properties is changed ?

    here is my custom control code example :

    public class PersoCont  :Panel
        {
            private int borderSize = 2;
            private Color borderColor = Color.DarkRed;
            private bool isBorder = true;
            private int paddingTopTitle = 0;
            private int paddingBorder = 0;
    
            public int padding_TopTitle
            {
                get { return paddingTopTitle; }
                set { paddingTopTitle = value; Invalidate(); }
            }
    
            public int padding_border
            {
                get { return paddingBorder; }
                set { paddingBorder = value; Invalidate(); }
            }
    
            public int border_size
            {
                get { return borderSize; }
                set { borderSize = value; Invalidate(); }
            }
    
            public Color border_color
            {
                get { return borderColor; }
                set { borderColor = value; Invalidate(); }
            }
    
            public bool is_border
            {
                get { return isBorder; }
                set { isBorder = value; Invalidate(); }
            }
    
            public PersoCont()
            {
    
            }
    
            protected override void OnHandleCreated(EventArgs e)
            {
                if (this.Controls.Find("xlblTitle", true).Length == 0)
                {
                    if (isBorder == true)
                    {
                        Label lblUp = new Label();
                        lblUp.Text = "";
                        lblUp.AutoSize = false;
                        lblUp.BackColor = borderColor;
                        int lblUpWidth = this.Width - (2 * paddingBorder) - (2 * borderSize);
                        lblUp.Size = new Size(lblUpWidth, borderSize);
                        lblUp.Location = new Point(borderSize + paddingBorder, paddingBorder);
                        lblUp.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
    
                        Label lblDown = new Label();
                        lblDown.Text = "";
                        lblDown.AutoSize = false;
                        lblDown.BackColor = borderColor;
                        lblDown.Size = new Size(lblUpWidth, borderSize);
                        int lblDownTop = this.Height - paddingBorder - borderSize;
                        lblDown.Location = new Point(borderSize + paddingBorder, lblDownTop);
                        lblDown.Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
    
                        Label lblLeft = new Label();
                        lblLeft.Text = "";
                        lblLeft.AutoSize = false;
                        lblLeft.BackColor = borderColor;
                        int lblLeftHeight = this.Height - (2 * paddingBorder);
                        lblLeft.Size =new Size(borderSize,lblLeftHeight);
                        lblLeft.Location = new Point(paddingBorder, paddingBorder);
                        lblLeft.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom;
    
                        Label lblRight = new Label();
                        lblRight.Text = "";
                        lblRight.AutoSize = false;
                        lblRight.BackColor = borderColor;
                        lblRight.Size = new Size(borderSize, lblLeftHeight);
                        int lblRightLeft = this.Width - paddingBorder - borderSize;
                        lblRight.Location = new Point(lblRightLeft, paddingBorder);
                        lblRight.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
    
                        this.Controls.Add(lblUp);
                        this.Controls.Add(lblDown);
                        this.Controls.Add(lblLeft);
                        this.Controls.Add(lblRight);
                    }
                }
                base.OnHandleCreated(e);
            }
        }

    • Moved by CoolDadTx Thursday, October 31, 2019 1:41 PM Winforms related
    Wednesday, October 30, 2019 2:46 AM

Answers

  • Consider an approach like this:

    public class PersoCont2 : Panel
    {
    	Label lblUp = null;
    
    	bool isBorder;
    	Color borderColor;
    
    	public bool is_border
    	{
    		get { return isBorder; }
    		set { isBorder = value; Rearrange( ); }
    	}
    
    	public Color border_color
    	{
    		get { return borderColor; }
    		set { borderColor = value; Rearrange( ); }
    	}
    
    
    	private void Rearrange( )
    	{
    		if( lblUp != null )
    		{
    			Controls.Remove( lblUp );
    			lblUp = null;
    		}
    		if( is_border )
    		{
    			lblUp = new Label( );
    			lblUp.BackColor = borderColor;
    			// TODO: set properties
    			// . . .
    			lblUp.Location = new Point( 10, 10 );
    			Controls.Add( lblUp );
    		}
    	}
    }
    

    • Marked as answer by MohanedAmine Wednesday, October 30, 2019 12:41 PM
    Wednesday, October 30, 2019 6:06 AM

All replies

  • Consider an approach like this:

    public class PersoCont2 : Panel
    {
    	Label lblUp = null;
    
    	bool isBorder;
    	Color borderColor;
    
    	public bool is_border
    	{
    		get { return isBorder; }
    		set { isBorder = value; Rearrange( ); }
    	}
    
    	public Color border_color
    	{
    		get { return borderColor; }
    		set { borderColor = value; Rearrange( ); }
    	}
    
    
    	private void Rearrange( )
    	{
    		if( lblUp != null )
    		{
    			Controls.Remove( lblUp );
    			lblUp = null;
    		}
    		if( is_border )
    		{
    			lblUp = new Label( );
    			lblUp.BackColor = borderColor;
    			// TODO: set properties
    			// . . .
    			lblUp.Location = new Point( 10, 10 );
    			Controls.Add( lblUp );
    		}
    	}
    }
    

    • Marked as answer by MohanedAmine Wednesday, October 30, 2019 12:41 PM
    Wednesday, October 30, 2019 6:06 AM
  • Very nice 

    That's exactly what i need

    Thnak you.

    Thursday, October 31, 2019 12:09 PM