Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 646723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:38:38+00:00 2026-05-13T21:38:38+00:00

I am doing a custom control (inherited from VisualBasic.PowerPacks.LineShape ), that should be painted

  • 0

I am doing a custom control (inherited from VisualBasic.PowerPacks.LineShape), that should be painted like as standard one, but also having a Icon displayed near it.

So, I just overrided OnPaint like this:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
    e.Graphics.DrawIcon(myIcon, StartPoint.X, StartPoint.Y);
    base.OnPaint(e);
}

Now, everything is OK, but when my control moves, the icon still remains drawn on the ancient place.

Is there a way to paint it properly?

alt text http://lh4.ggpht.com/_1TPOP7DzY1E/S5gXmp7xYiI/AAAAAAAADHI/pa1OhpKYSoM/Untitled-2.png
Real project situation

CODE: The sample code for tests

alt text http://lh6.ggpht.com/_1TPOP7DzY1E/S5jSluxvtDI/AAAAAAAADHw/EUz0Tfet-rw/s800/Capture2.png

using Microsoft.VisualBasic.PowerPacks;
using System.Windows.Forms;
using System.Drawing;

namespace LineShapeTest
{
    /// 
    /// Test Form
    /// 
    public class Form1 : Form
    {        
        IconLineShape myLine = new IconLineShape();
        ShapeContainer shapeContainer1 = new ShapeContainer();
        Panel panel1 = new Panel();

        public Form1()
        {
            this.panel1.Dock = DockStyle.Fill;
            // load your back image here
            this.panel1.BackgroundImage = 
                global::WindowsApplication22.Properties.Resources._13820t;
            this.panel1.Controls.Add(shapeContainer1);

            this.myLine.StartPoint = new Point(20, 30);
            this.myLine.EndPoint = new Point(80, 120);
            this.myLine.Parent = this.shapeContainer1;

            MouseEventHandler panelMouseMove = 
                new MouseEventHandler(this.panel1_MouseMove);
            this.panel1.MouseMove += panelMouseMove;
            this.shapeContainer1.MouseMove += panelMouseMove;

            this.Controls.Add(panel1);
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                myLine.StartPoint = e.Location;
            }
        }
    }

    /// 
    /// Test LineShape
    /// 
    public class IconLineShape : LineShape
    {
        Icon myIcon = SystemIcons.Exclamation;

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.DrawIcon(myIcon, StartPoint.X, StartPoint.Y);
            base.OnPaint(e);
        }
    }
}

Nota Bene, for the lineShape:

Parent = ShapeContainer
Parent.Parent = Panel

Update 1 TRACES

In this variant of OnPaint, we have traces:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
    Graphics g = Parent.Parent.CreateGraphics();
    g.DrawIcon(myIcon, StartPoint.X, StartPoint.Y);            
    base.OnPaint(e);
}        

alt text http://lh4.ggpht.com/_1TPOP7DzY1E/S5j29lutQ0I/AAAAAAAADH4/4yEnZd_hPjA/s800/Capture3.png

Update 2 BLINKS

In this variant of OnPaint, we have a blinking image:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
    Parent.Parent.Invalidate(this.Region, true);
    Graphics g = Parent.Parent.CreateGraphics();
    g.DrawIcon(myIcon, StartPoint.X, StartPoint.Y);            
    base.OnPaint(e);
}  

alt text http://lh5.ggpht.com/_1TPOP7DzY1E/S5j4Bam7hiI/AAAAAAAADIA/1hQWKyV8Fr0/s800/Capture4.png

Update 3: External Invalidation

This variant works well, but… from exterior of IconLineShape class:

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        Region r = myLine.Region;
        myLine.StartPoint = e.Location;
        panel1.Invalidate(r);
    }
}


/// 
/// Test LineShape
/// 
public class IconLineShape : LineShape
{
    Icon myIcon = SystemIcons.Exclamation;
    Graphics parentGraphics;

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        parentGraphics.DrawIcon(myIcon, StartPoint.X, StartPoint.Y);
        base.OnPaint(e);
    }

    protected override void OnParentChanged(System.EventArgs e)
    {
        // Parent is a ShapeContainer
        // Parent.Parent is a Panel
        parentGraphics = Parent.Parent.CreateGraphics();
        base.OnParentChanged(e);
    }
}

Even this resolves the problem of the test example, I need this control to be done inside the control, because I can’t force the external “clients” of this control do not forget to save the old region and invalidate the parent each time changing a location…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:38:38+00:00Added an answer on May 13, 2026 at 9:38 pm

    Finally, ended up to add a PictureBox instead of drawing the icon by myself.

    using Microsoft.VisualBasic.PowerPacks;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace LineShapeTest
    {
        /// 
        /// Test Form
        /// 
        public class Form1 : Form
        {
            IconLineShape myLine = new IconLineShape();
            ShapeContainer shapeContainer1 = new ShapeContainer();
            Panel panel1 = new Panel();
    
            public Form1()
            {
                this.panel1.Dock = DockStyle.Fill;
                // load your back image here
                //this.panel1.BackgroundImage =
                //global::WindowsApplication22.Properties.Resources._13820t;
                this.panel1.BackColor = Color.White;
                this.panel1.Controls.Add(shapeContainer1);
    
                this.myLine.StartPoint = new Point(20, 30);
                this.myLine.EndPoint = new Point(80, 120);
                this.myLine.Parent = this.shapeContainer1;
    
                MouseEventHandler panelMouseMove =
                    new MouseEventHandler(this.panel1_MouseMove);
                this.panel1.MouseMove += panelMouseMove;
                this.shapeContainer1.MouseMove += panelMouseMove;
    
                this.Controls.Add(panel1);
            }
    
            private void panel1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    myLine.StartPoint = e.Location;
                }
            }
        }
    
        /// 
        /// Test LineShape
        /// 
        public class IconLineShape : LineShape
        {
            Icon myIcon = SystemIcons.Exclamation;
            PictureBox pictureBox = new PictureBox();
    
            public IconLineShape()
            {
                pictureBox.Image = Bitmap.FromHicon(myIcon.Handle);
                pictureBox.Size = myIcon.Size;
                pictureBox.Visible = true;
            }
    
            protected override void OnMove(System.EventArgs e)
            {
                base.OnMove(e);
                pictureBox.Location = this.StartPoint;
            }
    
            protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
            {
                base.OnPaint(e);
                pictureBox.Invalidate();
            }
    
            public override bool HitTest(int x, int y)
            {            
                return base.HitTest(x, y) |
                    pictureBox.RectangleToScreen(
                        pictureBox.DisplayRectangle).Contains(new Point(x, y));
            }
    
            protected override void OnParentChanged(System.EventArgs e)
            {
                // Parent is a ShapeContainer
                // Parent.Parent is a Panel
                pictureBox.Parent = Parent.Parent;
                base.OnParentChanged(e);
            }
        }
    }
    

    Thanks everybody for participating!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My asp.net application has a custom base user control that is inherited from other
I need to write a custom WPF control that should look like a ComboBox
I'm doing a custom template in my MVC app, something like this: <%@ Control
I'm trying to create a binding from my custom control to objects that are
I am making a custom ComboBox, inherited from Winforms' standard ComboBox. For my custom
I have created a custom server control that inherits from CompositeControl. In the CreateChildControls
I want to create a custom control type that behaves exactly like a ListBox
I have a custom control that is doing a lot of 2D drawing straight
I'm trying to make a custom control inherited from TextBox. I'm looking to do
I'm new to working in ASP.NET but I'm developing a custom control that has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.