I created a slider bar user control but at run time when I move the slider to the left or right why it's not getting to the end or swallow?

rhodanny 166 Reputation points
2021-12-23T20:38:31.857+00:00

In the user control designer I added a pictureBox control :

160201-control1.jpg

Then in the code I did :

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
  
namespace Extract  
{  
    public partial class Slider : UserControl  
    {  
        public float Height;  
        public float Min = 0.0f;  
        public float Max = 1.0f;  
  
        private float defaultValue = 0.1f;  
  
        public Slider()  
        {  
            InitializeComponent();              
        }  
  
        private void sliderControl_Paint(object sender, PaintEventArgs e)  
        {  
            float bar_size = 0.45f;  
            float x = Bar(defaultValue);  
            int y = (int)(sliderControl.Height * bar_size);  
  
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;  
            e.Graphics.FillRectangle(Brushes.DimGray, 0, y, sliderControl.Width, y / 2);  
            e.Graphics.FillRectangle(Brushes.Red, 0, y, x, sliderControl.Height - 2 * y);  
  
            using (Pen pen = new Pen(Color.Black, 8))  
            {  
                e.Graphics.FillRectangle(Brushes.Red, 0, y, x, y / 2);  
                FillCircle(e.Graphics, Brushes.Red, x, y + y / 4, y / 2);  
            }  
  
            using (Pen pen = new Pen(Color.White, 5))  
            {  
                DrawCircle(e.Graphics, pen, x, y + y / 4, y/ 2);  
            }  
        }  
  
        public static void DrawCircle(Graphics g, Pen pen,  
                                  float centerX, float centerY, float radius)  
        {  
            g.DrawEllipse(pen, centerX - radius, centerY - radius,  
                          radius + radius, radius + radius);  
        }  
  
        public static void FillCircle(Graphics g, Brush brush,  
                                      float centerX, float centerY, float radius)  
        {  
            g.FillEllipse(brush, centerX - radius, centerY - radius,  
                          radius + radius, radius + radius);  
        }  
  
        private float Bar(float value)  
        {  
            return (sliderControl.Width - 24) * (value - Min) / (float)(Max - Min);  
        }  
  
        private void Thumb(float value)  
        {  
            if (value < Min) value = Min;  
            if (value > Max) value = Max;  
            defaultValue = value;  
  
            sliderControl.Refresh();  
        }  
  
        private float SliderWidth(int x)  
        {  
            return Min + (Max - Min) * x / (float)(sliderControl.Width);  
        }  
  
        protected override void OnResize(EventArgs e)  
        {  
            base.OnResize(e);  
  
            MaintainPictureBoxSize();  
        }  
  
        private void MaintainPictureBoxSize()  
        {  
            sliderControl.SizeMode = PictureBoxSizeMode.Normal;  
  
            sliderControl.Location = new Point();  
            sliderControl.Size = new Size();  
  
            var clientSize = this.ClientSize;  
  
            if (sliderControl.Image == null)  
                sliderControl.Size = clientSize;  
            else  
            {  
                Size s = sliderControl.Image.Size;  
                sliderControl.Size = new Size(  
                    clientSize.Width > s.Width ? clientSize.Width : s.Width,  
                    clientSize.Height > s.Height ? clientSize.Height : s.Height);  
            }  
        }  
  
        bool mouse = false;  
        private void sliderControl_MouseDown(object sender, MouseEventArgs e)  
        {  
            mouse = true;  
            Thumb(SliderWidth(e.X));  
        }  
  
        private void sliderControl_MouseMove(object sender, MouseEventArgs e)  
        {  
            if (!mouse) return;  
  
            Thumb(SliderWidth(e.X));  
        }  
  
        private void sliderControl_MouseUp(object sender, MouseEventArgs e)  
        {  
            mouse = false;  
        }  
    }  
}  

When I drag the control to the form1 designer and then running the application then when I drag the slider for example to the left or to the right the circle of the slider is partly swallow.

and if I resize the control in form1 designer to be smaller and then running the application to left it swallow as before but to the right it's not getting to the end at all.

160060-sliderisnotworking1.jpg

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,830 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,265 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ken Tucker 5,846 Reputation points
    2021-12-24T00:40:18.687+00:00

    The missing part of the circle is drawing outside of the control. You need to indent the rectangle a little for the circles.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful