0

I am new to C# and Visual Studio in general. What I want to do is create a customized TrackBar control that has the ability to save tab stops and draw markers at those locations. I am calling it MarkerBar.

In the OnPaint() override, I add some simple graphics trying different things from the internet I found. The control still draws as a normal TrackBar (with a red background) and none of my graphics showing. It visually functions normally at run time. Ultimately I want to draw the control myself, but at this point I'm just wondering how the original appearance of the slider and ticks is still showing... didn't I "override" it? I tried with and without the base.OnPaint() call.

I used the Visual Studio add new custom control and created...

MarkerBarDesigner.cs

    partial class MarkerBar
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
        }

        #endregion
    }

and MarkerBar.cs which I altered to inherit from TrackBar and OnPaint contents

    public partial class MarkerBar : TrackBar
    {
        public MarkerBar()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            //base.OnPaint(pe);
            pe.Graphics.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(0, 0, 50, 50));
        }
    }

and in my Form code I have...

        private MarkerBar mrkTest = new MarkerBar();

        public Form1()
        {
            InitializeComponent();
            mrkTest.BackColor = Color.Red;
            mrkTest.Location = new System.Drawing.Point(400, 300);
            mrkTest.Size = new System.Drawing.Size(50, 25);
            Controls.Add(mrkTest);
        }
  • This may help https://stackoverflow.com/questions/1551889/how-to-make-an-ownerdraw-trackbar-in-winforms. In short, you need `SetStyle(ControlStyles.UserPaint, true);` and only then your custom drawing handler will be called. – Serg Mar 23 '23 at 15:11
  • That did the trick. I since decided to go with a fully Custom Control and the original OnPaint override is (as expected) working in that case. Thanks. – theDickChuck Mar 25 '23 at 14:32

0 Answers0