Click here to Skip to main content
15,868,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello I am developing a custom control for my GUI but now i have strange thing happening. After rebiulding the project, control`s Location property turns to (0,0).
Designer changing it`s value in design mode but it sets to (0,0) everytime i rebuild the project.
My Control is child of Panel control. And its Dock and Anchor properties are set to None.

Could anybody help with that?

this is the code of the control
:
C#
public class ShelfSplitControl : Panel
    { 
        /// <summary>
        /// Constructor
        /// </summary>
        public ShelfSplitControl()
        {
            InitializeComponent();
            base.BackColor = Color.DimGray;            
            this.MouseDown += new MouseEventHandler(OnMouseDownHandler);
            this.MouseMove += new MouseEventHandler(OnMouseMoveHandler);

            this.Anchor = AnchorStyles.None;
            this.Dock = DockStyle.None;


            this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
                        ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Mouse Handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMouseMoveHandler(object sender, MouseEventArgs e)
        {
            if (!this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
            {
                  if (this.ParentShelf != null && this.Runtime == true)
                    ParentShelf.UpDateSplitters();
            }


            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (this._Vertical)
                {
                    int left = SplitterDownKeyPosition.X + (Cursor.Position.X - MouseDownLocation.X);

                    if (left < this._Position_Min) { this.Left = this._Position_Min; return; this.FindForm().Refresh(); }
                    if (left > this._Position_Max) { this.Left = this._Position_Max; return; this.FindForm().Refresh(); }

                    this.Position = left;
                }


                if (!this._Vertical)
                {
                    int top = SplitterDownKeyPosition.Y + (Cursor.Position.Y - MouseDownLocation.Y);

                    if (top < this._Position_Min) { this.Top = this._Position_Min; return; this.FindForm().Refresh(); }
                    if (top > this._Position_Max) { this.Top = this._Position_Max; return; this.FindForm().Refresh(); }

                    this.Position = top;
                }


                if (this.ParentShelf != null && this.Runtime == true)
                    ParentShelf.UpDateSplitters();
            }

        }
        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //

        /// <summary>
        /// Changing size
        /// </summary>
        /// <param name="eventargs"></param>
        protected override void OnResize(EventArgs eventargs)
        {
            this.Hide();
            this.Show();
            base.OnResize(eventargs);
        }    

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Adding parent control
        /// </summary>
        /// <param name="parent"></param>
        public void SetParentShelf(ShelfControl parent)
        {
           this.ParentShelf = parent;
           this.ParentShelf.Paint += new PaintEventHandler(OnParentPaint);
        }
        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Setting position directly
        /// </summary>
        /// <param name="Pos"></param>
        public void SetPositionDirectly(int Pos)
        {
            if (this._Vertical) this.Left = Pos;
            if (!this._Vertical) this.Top = Pos;
        }
        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        ///re paint
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnParentPaint(object sender, PaintEventArgs e)
        {
            if (Visible) Refresh();
        }       
       

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        ///Mouse handling
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMouseDownHandler(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                MouseDownLocation = Cursor.Position;
                SplitterDownKeyPosition = this.Location; 
            }
        }       

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// paint 
        /// </summary>
        /// <param name="pe"></param>
        protected override void OnPaint(PaintEventArgs pe)
        {
            Graphics gfx = pe.Graphics;
            Rectangle rc = ClientRectangle;

            rc.Height -= 1;
            rc.Width -= 1;
      
          
             // gfx.FillRectangle(new SolidBrush(base.ForeColor), rc);


            float dotDistance = 4;
            int dotsCount = 0;

            if (this.Vertical)  dotsCount = (int)(this.Height  / dotDistance);
            if (!this.Vertical) dotsCount = (int)(this.Width   / dotDistance);


            float dotHalfSize = 1.2f;            
            PointF dotLoc = new PointF (0,0);
            RectangleF dotRect = new RectangleF();
            float dotStart = 0;
            float dotEnd = 0;
            float delta = 1.5f;


            if (this._Vertical == true)
            {
                dotStart = this.Height / 2.0f - dotDistance * dotsCount / 2.0f;
                dotEnd   = this.Height / 2.0f + dotDistance * dotsCount / 2.0f; 

                for (int i = 0; i < dotsCount; i++)
                {
                    delta = delta * -1;
                    gfx.FillEllipse(new SolidBrush(Color.Gray), new RectangleF(new PointF(this.Width / 2.0f - dotHalfSize + delta, dotStart + (dotEnd - dotStart) / dotsCount * i),
                                                          new SizeF(dotHalfSize*2, dotHalfSize*2)));
                }
            }


            if (this._Vertical== false)
            {
                dotStart = this.Width / 2.0f - dotDistance * dotsCount / 2.0f;
                dotEnd = this.Width / 2.0f + dotDistance * dotsCount / 2.0f;

                for (int i = 0; i < dotsCount; i++)
                {
                    delta = delta * -1;
                    gfx.FillEllipse(new SolidBrush(Color.Gray), new RectangleF(new PointF(dotStart + (dotEnd - dotStart) / dotsCount * i, this.Height / 2.0f - dotHalfSize +  delta),
                                                          new SizeF(dotHalfSize * 2, dotHalfSize * 2)));
                }
            }

            base.OnPaint(pe);

        }

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Changing size handling
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLocationChanged(EventArgs e)
        {
            if (!Runtime && ParentShelf != null && Pressed == false)
                ParentShelf.UpDateSplitters();         

            base.OnLocationChanged(e);
        }
        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Nackground changing
        /// </summary>
        /// <param name="pevent"></param>
        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);
        }


        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseEnter(EventArgs e)
        {
            this._IsMouseOver = true;

            if (this._Vertical) Cursor = Cursors.VSplit;
            if (!this._Vertical) Cursor = Cursors.HSplit;

            base.BackColor = this._MouseOverColor;
            base.OnMouseEnter(e);
        }

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Mouse leaave handling
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e)
        {
            this._IsMouseOver = false;
            Cursor = Cursors.Arrow;
            base.BackColor = this._DefaultBackColor;          


            if (_Pressed)
            {
                if (!_Vertical)
                {
                    if (MouseDownLocation.Y < Cursor.Position.Y)
                        this.Position = this.Position_Max;
                    
                    if (MouseDownLocation.Y > Cursor.Position.Y)                  
                        this.Position = this.Position_Min;                   
                } 
           

                if (_Vertical)
                {
                   if (MouseDownLocation.X > Cursor.Position.X) 
                       this.Position = this.Position_Max;
                      
                   if (MouseDownLocation.X < Cursor.Position.X)
                      this.Position = this.Position_Min;
                }                  
            }

         
            if (Runtime && this.Pressed)
                if (this.ParentShelf != null)
                    this.ParentShelf.UpDateSplitters();

            this.FindForm().Refresh();
            base.OnMouseLeave(e);
        }

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        ///Mouse Button handling
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            this._Pressed = true; 

            if (e.Button == MouseButtons.Left)
                base.BackColor = this._MouseClickedColor;

            base.OnMouseDown(e);
        }

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Mouse Up handling
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (this._IsMouseOver == false) base.BackColor = this._DefaultBackColor;
                if (this._IsMouseOver == true) base.BackColor = this._MouseOverColor;
               
                if (Runtime && this.Pressed)
                    if (this.ParentShelf != null)
                        this.ParentShelf.UpDateSplitters();          
             
                this._Pressed = false;               

                this.Refresh();
                base.OnMouseUp(e);
            }
        }

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Checking splitter out of client size
        /// </summary>
        public void CheckPositions()
        {
            if (Vertical)
            {
                if (this.Left < 0)
                    this.Left = 0;
                if (this.Left > this.Parent.ClientSize.Width - this.SplitSize)
                    this.Left = this.Parent.ClientSize.Width - this.SplitSize;

                if (this.Left < this.Position_Min) this.Left = this.Position_Min;
                if (this.Left > this.Position_Max) this.Left = this.Position_Max;
            }

            if (!Vertical)
            {
                if (this.Top < 0)
                    this.Top = 0;
                if (this.Top > this.Parent.ClientSize.Height - this.SplitSize)
                    this.Top = this.Parent.ClientSize.Height - this.SplitSize;

                if (this.Top < this.Position_Min) this.Left = this.Position_Min;
                if (this.Top > this.Position_Max) this.Left = this.Position_Max;
            }
            
        }


        /// <summary>
        /// Sets super position from outside the class
        /// </summary>
        /// <param name="sPos"></param>
        public void SetSuperPositionMax(int sPos)
        {        
            this.Super_Position_Max = sPos;
        }

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //

        private Point MouseDownLocation = new Point();
        private Point SplitterDownKeyPosition = new Point();
        private ShelfControl ParentShelf;
        private int snapDist = 0;


        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Follow top/left border depends on (bool Vertical value)
        /// </summary>
        [Category("MyProject.Forms")]
        public bool Anchor_Min
        {
            get { return _Anchor_Min; }
            set
            {
                _Anchor_Min = value;
            }
        }
        private bool _Anchor_Min = false;        

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Follow  Bottom/ right border depends on (bool Vertical value)
        /// </summary>
        [Category("MyProject.Forms")]
        public bool Anchor_Max
        {
            get { return _Anchor_Max; }
            set
            {
                _Anchor_Max = value;
            }
        }
        private bool _Anchor_Max = false; 

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        [Category("MyProject.Forms")]
        public int SplitSize
        {
            get { return _SplitSize; }
            set {this._SplitSize = value; }
        }
        private int _SplitSize = 12;      
        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Is mouse over the control
        /// </summary>
        private bool _IsMouseOver = false; 
    
        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        [Category("MyProject.Forms")]
        public bool Runtime
        {
            get { return (!DesignMode); }     
        } 
        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Row wich this splitter is pleaced in
        /// </summary>
        [Category("MyProject.Forms")]
        public int Row
        {
            get { return _Row; }
            set { _Row = value; }
        }
        private int _Row = 0;
        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Is this splitter vertical or not
        /// </summary>
        [Category("MyProject.Forms")]
        public bool Vertical
        {
            get { return _Vertical; }
            set 
            {
                _Vertical = value;
                Invalidate();
            }
        }
        private bool _Vertical = true;      


        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Position in percents
        /// </summary> 
        internal float SuperPosition
        {
            get { return _SuperPosition; }
            set 
            { 
                _SuperPosition = value;
                if (_SuperPosition < 0) _SuperPosition = 0;
                if (_SuperPosition > 100) _SuperPosition = 100;
            }
        }

        private float _SuperPosition = 0.0f;       

      
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Minimum position in pixels
        /// </summary>
        [Category("MyProject.Forms")]
        public int Position_Min
        {
            get { return _Position_Min; }
            set
            { 
                if (_Position_Min == value) return;
                _Position_Min = value;       
            }
        }
        private int _Position_Min = 0;

        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Super Maximum position in pixels 
        /// </summary>
        [Category("MyProject.Forms")]
        public int Super_Position_Min
        {
            get { return _Super_Position_Min; }
            set
            {
                if (_Super_Position_Min == value) return;
                _Super_Position_Min = value;

               if (this.Position < _Position_Min)
                        this.Position = this._Super_Position_Min;

               if (this._Position_Min < this._Super_Position_Min)
                   this._Position_Min = this._Super_Position_Min;
              
            }
        }
        private int _Super_Position_Min = 0;

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Maximum position in pixels
        /// </summary>
        [Category("MyProject.Forms")]
        public int Position_Max
        {
           get { return _Position_Max; }
           set
            {
                if (_Position_Max == value) return;
                _Position_Max = value;                              
                                            
           }
        }     
        private int _Position_Max =  0;

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Super Maximum Position in pixels
        /// </summary>
        [Category("MyProject.Forms")]
        public int Super_Position_Max
        {
            get { return _Super_Position_Max; }
            set
            {
                if (_Super_Position_Max == value) return;
                _Super_Position_Max = value;                      

                if (this.Position > this._Super_Position_Max && this._Super_Position_Max > 0)
                   this.Position = this._Super_Position_Max; 

                if (this.Position_Max > this._Super_Position_Max && this._Super_Position_Max > 0)
                   this.Position_Max = this._Super_Position_Max;               
            }
        }
        private int _Super_Position_Max = 0;
        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        ///Sets Left or Top of the splitter depends on (bool Vertical value)
        /// </summary>
        [Category("MyProject.Forms")]
        public int Position
        {
            get 
            {
                if (_Vertical) 
                    return this.Left;
                else 
                    return this.Top;
            }
            set
            {
                if (this._Vertical)
                {
                    int left = value;

                    if (left <= this._Position_Min + this.snapDist ) { this.Left = this._Position_Min; return; }
                    if (left >= this._Position_Max - this.snapDist)  { this.Left = this._Position_Max; return; }

                    if (left < this.Super_Position_Min) { this.Left = this.Super_Position_Min; return; }
                    if (left > this.Super_Position_Max && this.Super_Position_Max > 0) { this.Left = this.Super_Position_Max; return; }

                    this.Left = left;
                }

                if (!this._Vertical)
                {
                    int top = value;                    

                    if (top <= this._Position_Min + this.snapDist) { this.Top = this._Position_Min; return; }
                    if (top >= this._Position_Max - this.snapDist) { this.Top = this._Position_Max; return; }

                    if (top < this.Super_Position_Min) { this.Top = this.Super_Position_Min; return; }
                    if (top > this.Super_Position_Max && this.Super_Position_Max > 0) {this.Top = this.Super_Position_Max; return; }

                    this.Top = top; 
                }
                   
            }
        }       

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Pressed state
        /// </summary>
        /// 
        public bool Pressed
        {
            get { return _Pressed; }
            set { this._Pressed = value; }
        }
        private bool _Pressed = false;


        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Default back color
        /// </summary>
        [Category("MyProject.Forms")]
        public new Color Color_DefaultBackColor
        {
            get { return _DefaultBackColor; }
            set { this._DefaultBackColor = value; }
        }
        private new Color _DefaultBackColor = Color.DimGray;
        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Back Color
        /// </summary>
        [Category("MyProject.Forms")]
        public Color Color_FontColor
        {
            get { return _FontColor; }
            set
            {
                this._FontColor = value;
                Invalidate();
            }
        }
        private Color _FontColor = Color.WhiteSmoke;

        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Mouse Over Back Color
        /// </summary>
        [Category("MyProject.Forms")]
        public Color Color_MouseOverColor
        {
            get { return _MouseOverColor; }
            set { this._MouseOverColor = value; }
        }
        public new Color _MouseOverColor = Color.LightGray;


        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Mouse Clicked Color
        /// </summary>
        [Category("MyProject.Forms")]
        public Color Color_MouseClickedColor
        {
            get { return _MouseClickedColor; }
            set { this._MouseClickedColor = value; }
        }
        private new Color _MouseClickedColor = Color.FromArgb(255, 128, 0);


        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Sets the upper Row of Shelfs which will be controlled by this splitter  Vartical values is False
        /// </summary>
        [Category("MyProject.Forms")]
        public int Row_Upper
        {
            get { return _UpperRow; }
            set { _UpperRow = value; }
        }
        private int _UpperRow = 0;
        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Bottom row of shelfs which is controlled by this splitter if Vertical value of this splitter is false.
        /// </summary>
        [Category("MyProject.Forms")]
        public int Row_Lower
        {
            get { return _LowerRow; }
            set { _LowerRow = value; }
        }
        private int _LowerRow = 0;
        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Left shelf of the row which is controlled by this splitter if this Vertical value is true
        /// </summary>
        [Category("MyProject.Forms"),
        DefaultValue(0),
        Browsable(true)]
        public int Row_LeftElement
        {
            get { return _LeftRowElement; }
            set { _LeftRowElement = value; }
        }
        private int _LeftRowElement = 0;


        //
        //----------------------------------------------------------------***-------------------------------------------------------------------------
        //
        /// <summary>
        /// Right shelf of the row which is controlled by this splitter if this Vertical value is true
        /// </summary>
        [Category("MyProject.Forms")]
        public int Row_RightElement
        {
            get { return _RightRowElement; }
            set { _RightRowElement = value; }
        }
        private int _RightRowElement = 0;

        /// <summary>
        /// Actual location
        /// </summary>
        [Category("MyProject.Forms")]
        public  System.Drawing.Point Location
        {
            get
            {
                return base.Location;
            }
            set
            {
                base.Location = value;
            }
        }

        /// <summary>
        /// Actual size
        /// </summary>
        [Category("MyProject.Forms")]
        public  System.Drawing.Size Size
        {
            get { return base.Size; }
            set { base.Size = value; }
        }  

}        
Posted
Updated 26-Dec-14 10:50am
v4
Comments
BillWoodruff 25-Dec-14 23:59pm    
Is this WinForms or WPF or ... ?

What type of "container object" is your custom control placed in: a Form, a Panel ... or ... ?
Sergey Alexandrovich Kryukov 26-Dec-14 0:46am    
Whenever I developed some controls, they kept the location and other properties properly. Therefore, it would be logical to assume you screwed up something. But how could we know where, without seeing your code?
—SA

1 solution

Your properties that you want to set from the designer code need the DesignerSerializationVisibility attribute added to them.

For example:
C#
[Category("MyProject.Forms"), DefaultValue(0),Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int Row_LeftElement
{
    get { return _LeftRowElement; }
    set { _LeftRowElement = value; }
}
 
Share this answer
 
Comments
Alexey Gapon 27-Dec-14 7:25am    
Hello i tried that but it makes no difference. The "Location" is still changing it`s value to (0,0). And once [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] have been added to the properties all solution stop working i need to rerun the VS.
Dave Kreskowiak 27-Dec-14 10:54am    
OK, so provide your own Location property and do the same this you did for other properties. Set and return the Location property of the base control you inherited from. Put the attribute on your property.
Alexey Gapon 27-Dec-14 19:15pm    
I don`t know it just dose not work ((
Dave Kreskowiak 28-Dec-14 0:48am    
I just told you what to do. I'm not going to write your code for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900