Smart Custom Control
-
Is there any way a Custom control which when placed on a form displays its current location on the form (in design time not at runtime) So if it is placed on 0,0 then onthe control we have 0,0, drawn and if we move it to 50,50 it refreshes to 50,50 basically the component behaves differently according to its place on the form Thanks for your time.
C++ where friends have access to your private members !
-
Is there any way a Custom control which when placed on a form displays its current location on the form (in design time not at runtime) So if it is placed on 0,0 then onthe control we have 0,0, drawn and if we move it to 50,50 it refreshes to 50,50 basically the component behaves differently according to its place on the form Thanks for your time.
C++ where friends have access to your private members !
-
Is there any way a Custom control which when placed on a form displays its current location on the form (in design time not at runtime) So if it is placed on 0,0 then onthe control we have 0,0, drawn and if we move it to 50,50 it refreshes to 50,50 basically the component behaves differently according to its place on the form Thanks for your time.
C++ where friends have access to your private members !
What you need to do is create a custom designer for your control. Since a bit of code is worth a thousand words, I will stop talking and just post a drop dead simple example.
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms.Design; using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.ComponentModel; namespace WindowsApplication1 { [Designer(typeof(SomeControlDesigner))] public class SomeControl : Control { public SomeControl() { this.Size = new Size(100, 25); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); using (LinearGradientBrush brsh = new LinearGradientBrush(this.ClientRectangle, Color.CornflowerBlue, Color.White, LinearGradientMode.Vertical)) { e.Graphics.FillRectangle(brsh, e.ClipRectangle); } } } public class SomeControlDesigner : ControlDesigner { // This is the key step. Using this override, you can pretty much completely // customize the way the control appears on the designer. I've used this in my controls // to completely change the design-time appearance of controls to make their design // capabilities more obvious. protected override void OnPaintAdornments(System.Windows.Forms.PaintEventArgs pe) { base.OnPaintAdornments(pe); using(SolidBrush brsh = new SolidBrush(Color.Red)) { pe.Graphics.DrawString(this.Control.Location.ToString(), this.Control.Font, brsh, pe.ClipRectangle.Location); } } } }
The OnPaint override is there just to make the point that this is a custom control. You'll also need to add a reference to System.Design.dll. -
Hello, You could override the OnLocationChanged method, like this:
protected override void OnLocationChanged(EventArgs e) { if(DesignMode) { //Do what you whant here } base.OnLocationChanged (e); }
Hope it helps! All the best, Martin
-
What you need to do is create a custom designer for your control. Since a bit of code is worth a thousand words, I will stop talking and just post a drop dead simple example.
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms.Design; using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.ComponentModel; namespace WindowsApplication1 { [Designer(typeof(SomeControlDesigner))] public class SomeControl : Control { public SomeControl() { this.Size = new Size(100, 25); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); using (LinearGradientBrush brsh = new LinearGradientBrush(this.ClientRectangle, Color.CornflowerBlue, Color.White, LinearGradientMode.Vertical)) { e.Graphics.FillRectangle(brsh, e.ClipRectangle); } } } public class SomeControlDesigner : ControlDesigner { // This is the key step. Using this override, you can pretty much completely // customize the way the control appears on the designer. I've used this in my controls // to completely change the design-time appearance of controls to make their design // capabilities more obvious. protected override void OnPaintAdornments(System.Windows.Forms.PaintEventArgs pe) { base.OnPaintAdornments(pe); using(SolidBrush brsh = new SolidBrush(Color.Red)) { pe.Graphics.DrawString(this.Control.Location.ToString(), this.Control.Font, brsh, pe.ClipRectangle.Location); } } } }
The OnPaint override is there just to make the point that this is a custom control. You'll also need to add a reference to System.Design.dll. -
What you need to do is create a custom designer for your control. Since a bit of code is worth a thousand words, I will stop talking and just post a drop dead simple example.
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms.Design; using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.ComponentModel; namespace WindowsApplication1 { [Designer(typeof(SomeControlDesigner))] public class SomeControl : Control { public SomeControl() { this.Size = new Size(100, 25); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); using (LinearGradientBrush brsh = new LinearGradientBrush(this.ClientRectangle, Color.CornflowerBlue, Color.White, LinearGradientMode.Vertical)) { e.Graphics.FillRectangle(brsh, e.ClipRectangle); } } } public class SomeControlDesigner : ControlDesigner { // This is the key step. Using this override, you can pretty much completely // customize the way the control appears on the designer. I've used this in my controls // to completely change the design-time appearance of controls to make their design // capabilities more obvious. protected override void OnPaintAdornments(System.Windows.Forms.PaintEventArgs pe) { base.OnPaintAdornments(pe); using(SolidBrush brsh = new SolidBrush(Color.Red)) { pe.Graphics.DrawString(this.Control.Location.ToString(), this.Control.Font, brsh, pe.ClipRectangle.Location); } } } }
The OnPaint override is there just to make the point that this is a custom control. You'll also need to add a reference to System.Design.dll.