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
:
public class ShelfSplitControl : Panel
{
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);
}
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();
}
}
protected override void OnResize(EventArgs eventargs)
{
this.Hide();
this.Show();
base.OnResize(eventargs);
}
public void SetParentShelf(ShelfControl parent)
{
this.ParentShelf = parent;
this.ParentShelf.Paint += new PaintEventHandler(OnParentPaint);
}
public void SetPositionDirectly(int Pos)
{
if (this._Vertical) this.Left = Pos;
if (!this._Vertical) this.Top = Pos;
}
private void OnParentPaint(object sender, PaintEventArgs e)
{
if (Visible) Refresh();
}
private void OnMouseDownHandler(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = Cursor.Position;
SplitterDownKeyPosition = this.Location;
}
}
protected override void OnPaint(PaintEventArgs pe)
{
Graphics gfx = pe.Graphics;
Rectangle rc = ClientRectangle;
rc.Height -= 1;
rc.Width -= 1;
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);
}
protected override void OnLocationChanged(EventArgs e)
{
if (!Runtime && ParentShelf != null && Pressed == false)
ParentShelf.UpDateSplitters();
base.OnLocationChanged(e);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
base.OnPaintBackground(pevent);
}
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);
}
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);
}
protected override void OnMouseDown(MouseEventArgs e)
{
this._Pressed = true;
if (e.Button == MouseButtons.Left)
base.BackColor = this._MouseClickedColor;
base.OnMouseDown(e);
}
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);
}
}
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;
}
}
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;
[Category("MyProject.Forms")]
public bool Anchor_Min
{
get { return _Anchor_Min; }
set
{
_Anchor_Min = value;
}
}
private bool _Anchor_Min = false;
[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;
private bool _IsMouseOver = false;
[Category("MyProject.Forms")]
public bool Runtime
{
get { return (!DesignMode); }
}
[Category("MyProject.Forms")]
public int Row
{
get { return _Row; }
set { _Row = value; }
}
private int _Row = 0;
[Category("MyProject.Forms")]
public bool Vertical
{
get { return _Vertical; }
set
{
_Vertical = value;
Invalidate();
}
}
private bool _Vertical = true;
internal float SuperPosition
{
get { return _SuperPosition; }
set
{
_SuperPosition = value;
if (_SuperPosition < 0) _SuperPosition = 0;
if (_SuperPosition > 100) _SuperPosition = 100;
}
}
private float _SuperPosition = 0.0f;
[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;
[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;
[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;
[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;
[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;
}
}
}
public bool Pressed
{
get { return _Pressed; }
set { this._Pressed = value; }
}
private bool _Pressed = false;
[Category("MyProject.Forms")]
public new Color Color_DefaultBackColor
{
get { return _DefaultBackColor; }
set { this._DefaultBackColor = value; }
}
private new Color _DefaultBackColor = Color.DimGray;
[Category("MyProject.Forms")]
public Color Color_FontColor
{
get { return _FontColor; }
set
{
this._FontColor = value;
Invalidate();
}
}
private Color _FontColor = Color.WhiteSmoke;
[Category("MyProject.Forms")]
public Color Color_MouseOverColor
{
get { return _MouseOverColor; }
set { this._MouseOverColor = value; }
}
public new Color _MouseOverColor = Color.LightGray;
[Category("MyProject.Forms")]
public Color Color_MouseClickedColor
{
get { return _MouseClickedColor; }
set { this._MouseClickedColor = value; }
}
private new Color _MouseClickedColor = Color.FromArgb(255, 128, 0);
[Category("MyProject.Forms")]
public int Row_Upper
{
get { return _UpperRow; }
set { _UpperRow = value; }
}
private int _UpperRow = 0;
[Category("MyProject.Forms")]
public int Row_Lower
{
get { return _LowerRow; }
set { _LowerRow = value; }
}
private int _LowerRow = 0;
[Category("MyProject.Forms"),
DefaultValue(0),
Browsable(true)]
public int Row_LeftElement
{
get { return _LeftRowElement; }
set { _LeftRowElement = value; }
}
private int _LeftRowElement = 0;
[Category("MyProject.Forms")]
public int Row_RightElement
{
get { return _RightRowElement; }
set { _RightRowElement = value; }
}
private int _RightRowElement = 0;
[Category("MyProject.Forms")]
public System.Drawing.Point Location
{
get
{
return base.Location;
}
set
{
base.Location = value;
}
}
[Category("MyProject.Forms")]
public System.Drawing.Size Size
{
get { return base.Size; }
set { base.Size = value; }
}
}