How do I label the minimum, maximum, center ticks in a .net 3.5 trackbar?
I need to set a voltage in an application. I'm used to using sliders in Labview, and would like to replicate that using a C# program.
I've figured out that the track bar only does integer values, so rather than have the range go from -5 to 5 using a double, I need to have the track bar go from -50 to +50 with tick marks every 10 steps to get 0.1v resolution.
How do I label the track bar minimum and maximum values?
|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
-5.0 0.0 &n开发者_运维百科bsp; 5.0I'm using C# with .net 3.5 with Visual Studio.
The easiest way would be to add to 2-3 labels, using a combination of height/width/top/left of the trackbar and labels you should be able to position them relatively without too much hassle?
EDIT: Also, it seems this post shows a custom implementation with the same requirements as you've described?
TrackBar Scale Image Link
What I've done is modify a chart and used its X axis scale as the scale for the TrackBar (See link for image [I would have embedded image but I couldn't get it to work]). The top half of the image is the slider and the bottom half is a regular chart object with it properties changed so the only thing visible is its X axis.
Here's the VS2010 Form Designer code for the TrackBar and Chart. This example will give you a scale of 0 to 100 which can easily be resized to line up with your trackbar. You could even put both controls in a panel so they can be easily resized as one (Code is VB rather than C#):
Dim ChartArea2 As System.Windows.Forms.DataVisualization.Charting.ChartArea = New System.Windows.Forms.DataVisualization.Charting.ChartArea()
Dim Legend2 As System.Windows.Forms.DataVisualization.Charting.Legend = New System.Windows.Forms.DataVisualization.Charting.Legend()
Dim Series2 As System.Windows.Forms.DataVisualization.Charting.Series = New System.Windows.Forms.DataVisualization.Charting.Series()
Dim DataPoint2 As System.Windows.Forms.DataVisualization.Charting.DataPoint = New System.Windows.Forms.DataVisualization.Charting.DataPoint(0.0R, 0.0R)
Me.chart_slider = New System.Windows.Forms.DataVisualization.Charting.Chart()
Me.TrackBar1 = New System.Windows.Forms.TrackBar()
CType(Me.chart_slider, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'chart_slider
'
Me.chart_slider.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.chart_slider.BackColor = System.Drawing.Color.Transparent
ChartArea2.AxisX.LabelAutoFitMaxFontSize = 8
ChartArea2.AxisX.LabelAutoFitMinFontSize = 8
ChartArea2.AxisX.LineColor = System.Drawing.Color.Transparent
ChartArea2.AxisX.MajorGrid.Enabled = False
ChartArea2.AxisX.Maximum = 100.0R
ChartArea2.AxisX.Minimum = 0.0R
ChartArea2.AxisY.Interval = 1.0R
ChartArea2.AxisY.LineWidth = 0
ChartArea2.AxisY.MajorGrid.Enabled = False
ChartArea2.AxisY.MajorTickMark.Enabled = False
ChartArea2.AxisY.Maximum = 0.0R
ChartArea2.AxisY.Minimum = 0.0R
ChartArea2.AxisY.TitleForeColor = System.Drawing.Color.Transparent
ChartArea2.BackColor = System.Drawing.Color.Transparent
ChartArea2.InnerPlotPosition.Auto = False
ChartArea2.InnerPlotPosition.Height = 5.0!
ChartArea2.InnerPlotPosition.Width = 100.0!
ChartArea2.Name = "ChartArea1"
Me.chart_slider.ChartAreas.Add(ChartArea2)
Legend2.Enabled = False
Legend2.Name = "Legend1"
Me.chart_slider.Legends.Add(Legend2)
Me.chart_slider.Location = New System.Drawing.Point(186, 426)
Me.chart_slider.Name = "chart_slider"
Series2.ChartArea = "ChartArea1"
Series2.Legend = "Legend1"
Series2.Name = "Series1"
Series2.Points.Add(DataPoint2)
Me.chart_slider.Series.Add(Series2)
Me.chart_slider.Size = New System.Drawing.Size(441, 31)
Me.chart_slider.TabIndex = 160
Me.chart_slider.Text = "Chart1"
'
'TrackBar1
'
Me.TrackBar1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.TrackBar1.Location = New System.Drawing.Point(191, 398)
Me.TrackBar1.Margin = New System.Windows.Forms.Padding(3, 3, 3, 0)
Me.TrackBar1.Name = "TrackBar1"
Me.TrackBar1.RightToLeftLayout = True
Me.TrackBar1.Size = New System.Drawing.Size(423, 45)
Me.TrackBar1.TabIndex = 1
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(690, 498)
Me.Controls.Add(Me.TrackBar1)
Me.Controls.Add(Me.chart_slider)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.chart_slider, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
精彩评论