如何在.net 3.5轨道栏中标记最小,最大,中心刻度线?

时间:2011-05-24 14:20:55

标签: c# .net-3.5 trackbar

我需要在应用程序中设置电压。我习惯在Labview中使用滑块,并希望使用C#程序复制它。

我已经发现轨迹栏只有整数值,所以我不需要使用双倍的范围从-5到5,我需要让轨迹栏从-50到+50带刻度线每10个步骤获得0.1v分辨率。

如何标记轨迹栏的最小值和最大值?

| ----- | ----- | ----- | ----- | ----- | ----- | ----- | ---- - | ----- | ----- |
-5.0 0.0 5.0

我在Visual Studio中使用带有.net 3.5的C#。

2 个答案:

答案 0 :(得分:3)

最简单的方法是添加2-3个标签,使用轨迹栏的高度/宽度/顶部/左侧和标签的组合,您应该能够相对定位它们而不会有太多麻烦?

编辑:此外,似乎this post显示的自定义实现具有与您描述的相同的要求?

答案 1 :(得分:2)

TrackBar Scale Image Link

我所做的是修改图表并使用其X轴刻度作为TrackBar的刻度(请参阅图像链接[我会嵌入图像,但我无法使其工作])。图像的上半部分是滑块,下半部分是常规图表对象,其属性已更改,因此唯一可见的是其X轴。

这是TrackBar和Chart的VS2010表单设计器代码。此示例将为您提供0到100的比例,可以轻松调整其大小以与您的轨迹栏对齐。您甚至可以将两个控件放在一个面板中,这样它们就可以很容易地调整为一个(代码是VB而不是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()