I have a trackBar in my form1 designer where i can click twice. First time i select a number of a file for example file number 5052 and another click for exmaple on 5473.
Then i assign the selections to two int variables Start and End: But i see using a breakpoint that Start is: 5051 and End is: 5472 In my new form i did:
mtp1Start = myTrackPanelss1.Start; mtp1End = myTrackPanelss1.End;
Now mtp1Start = 5051 And mtp1End = 5472
myTrackPanelss1 is a UserControl of a trackPanel.
The question is where is the problem when i make the selection on the trackPanel why it's showing different selected numbers from what i click on and what i get in the variables ?
This is the trackPanel control code:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace mws { public partial class MyTrackPanelss : UserControl { public int Start { get; set; } public int End { get; set; } private int _tbPadding = 14; private float _penWidth = 15F; public int TBPadding { get { return _tbPadding; } } public float PenWidth { get { return _penWidth; } } public MyTrackPanelss() { InitializeComponent(); Start = 0; End = this.trackBar1.Maximum; } private void panel1_Paint(object sender, PaintEventArgs e) { using (SolidBrush b = new SolidBrush(SystemColors.ControlDark)) e.Graphics.FillRectangle(b, this.ClientRectangle); using (GraphicsPath gPath = new GraphicsPath()) { gPath.AddLine(new Point(_tbPadding, 0), new Point(_tbPadding, panel1.ClientSize.Height - 2)); gPath.AddLine(new Point(this.trackBar1.ClientSize.Width - _tbPadding, panel1.ClientSize.Height - 2), new Point(this.trackBar1.ClientSize.Width - _tbPadding, 0)); using (SolidBrush sb = new SolidBrush(SystemColors.ActiveCaption)) e.Graphics.FillPath(sb, gPath); e.Graphics.DrawPath(Pens.Black, gPath); } int maxRange = this.trackBar1.Maximum - this.trackBar1.Minimum; //_tbPadding for end and start of ticks of trackbar double rangeOne = ((this.panel1.ClientSize.Width - _tbPadding * 2.0) / (double)maxRange); if (End < Start) { int tmp = End; End = Start; Start = tmp; } if (End == Start) { MessageBox.Show("You cannot select start point and end point on the same place"); } float xVal = (float)(_tbPadding + (Start * rangeOne)); float xVal2 = (float)(_tbPadding + (End * rangeOne)); if (Start >= this.trackBar1.Minimum) { using (Pen greenPen = new Pen(Color.Green, _penWidth)) { e.Graphics.DrawLine(greenPen, new PointF(xVal, 1), new PointF(xVal, this.panel1.ClientSize.Height - 2)); using (Pen p = new Pen(Color.Black)) { p.StartCap = LineCap.ArrowAnchor; e.Graphics.DrawLine(p, new PointF(xVal, 1), new PointF(xVal, this.panel1.ClientSize.Height - 2)); } e.Graphics.DrawString((Start+1).ToString(), this.Font, Brushes.Black, new PointF(xVal, this.panel1.ClientSize.Height / 2F)); } } if (End <= this.trackBar1.Maximum) { using (Pen redPen = new Pen(Color.Red, _penWidth)) { SizeF s = e.Graphics.MeasureString(End.ToString(), this.Font); e.Graphics.DrawLine(redPen, new PointF(xVal2, 1), new PointF(xVal2, this.panel1.ClientSize.Height - 2)); using (Pen p = new Pen(Color.Black)) { p.StartCap = LineCap.ArrowAnchor; e.Graphics.DrawLine(p, new PointF(xVal2, 1), new PointF(xVal2, this.panel1.ClientSize.Height - 2)); } if (xVal2 - xVal > s.Width + redPen.Width) e.Graphics.DrawString((End+1).ToString(), this.Font, Brushes.Black, new PointF(xVal2 - s.Width, this.panel1.ClientSize.Height / 2F)); else e.Graphics.DrawString((End+1).ToString(), this.Font, Brushes.Black, new PointF(xVal2, this.panel1.ClientSize.Height / 2F)); } } string t = "click left to specify start, right to specify end"; SizeF sz = e.Graphics.MeasureString(t, this.Font); e.Graphics.DrawString(t, this.Font, Brushes.Black, new PointF((this.panel1.ClientSize.Width - sz.Width) / 2F, 0)); } private void panel1_MouseClick(object sender, MouseEventArgs e) { int maxRange = this.trackBar1.Maximum - this.trackBar1.Minimum; //_tbPadding for end and start of ticks of trackbar double rangeOne = ((this.panel1.ClientSize.Width - _tbPadding * 2.0) / (double)maxRange); if (e.Button == MouseButtons.Left && e.X >= _tbPadding && e.X <= panel1.ClientSize.Width - _tbPadding) { int i = (int)((e.X - _tbPadding) / rangeOne); Start = i; trackBar1.Value = i; this.panel1.Invalidate(); } if (e.Button == MouseButtons.Right && e.X >= _tbPadding && e.X <= panel1.ClientSize.Width - _tbPadding + rangeOne) { int i = (int)((e.X - _tbPadding) / rangeOne); End = i; trackBar1.Value = i; this.panel1.Invalidate(); } } } }
And this is a screenshot of the trackPanel after i did a selection:
You can see the selections i did the Green color and the Red color on the bottom. I'm doing the selections by clicking with the mouse on the light blue color of the trackPanel.
For some reason somewhere in the trackPanel UserControl code when i click to make the selections the results are less 1 number. If i clicked on 6321 then it will be 6320. I can't figure out what make the problem.