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. Smart Custom Control

Smart Custom Control

Scheduled Pinned Locked Moved C#
c++design
6 Posts 3 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.
  • M Offline
    M Offline
    Monty2
    wrote on last edited by
    #1

    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 !

    M P 2 Replies Last reply
    0
    • M Monty2

      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 !

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • M Monty2

        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 !

        P Offline
        P Offline
        Patrick Etc
        wrote on last edited by
        #3

        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.

        M M 2 Replies Last reply
        0
        • M Martin 0

          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

          M Offline
          M Offline
          Monty2
          wrote on last edited by
          #4

          Thanks a lot :)


          C++ where friends have access to your private members !

          1 Reply Last reply
          0
          • P Patrick Etc

            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.

            M Offline
            M Offline
            Monty2
            wrote on last edited by
            #5

            Excellent .... i am c++ coder and i am just loving C# :-D


            C++ where friends have access to your private members !

            1 Reply Last reply
            0
            • P Patrick Etc

              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.

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #6

              Very nice idea! Got my "5" All the best, MArtin

              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