VB.Net Tutorial/GUI/TrackBar

Материал из VB Эксперт
Перейти к: навигация, поиск

Get TrackBar value and LargeChange value

<source lang="vbnet">imports System imports System.Drawing imports System.Windows.Forms public class TrackBars : inherits Form

 dim htbar as TrackBar
 dim vtbar as TrackBar
 public sub New()
   Size = new Size(500,520)
   htbar = new TrackBar()
   htbar.Parent = me
   htbar.Orientation = Orientation.Horizontal
   htbar.Size = new Size(200, 10)
   htbar.Location = new Point(0, 25)
   htbar.TickStyle = TickStyle.BottomRight
   htbar.TickFrequency = 25
   htbar.Minimum = 25
   htbar.Maximum = 400
   htbar.SmallChange = 10
   htbar.LargeChange = 25
   htbar.BackColor = Color.Yellow
   htbar.Value = 100
   AddHandler htbar.ValueChanged, AddressOf htbar_OnValueChanged
   vtbar = new TrackBar()
   vtbar.Parent = me
   vtbar.Orientation = Orientation.Vertical
   vtbar.Size = new Size(25, 300)
   vtbar.Location = new Point(25, 25)
   vtbar.TickStyle = TickStyle.BottomRight
   vtbar.SetRange(25,400)
   vtbar.SmallChange = 10
   vtbar.LargeChange = 50
   vtbar.TickFrequency = CInt(vtbar.Maximum / 20)
   vtbar.BackColor = Color.Pink
   vtbar.Value = 200
   AddHandler vtbar.ValueChanged, AddressOf vtbar_OnValueChanged
 end sub  "  close for constructor
 private sub htbar_OnValueChanged(ByVal sender as object, _
             ByVal e as EventArgs)
   Console.WriteLine(htbar.Value)
 end sub
 private sub vtbar_OnValueChanged(ByVal sender as object, _
             ByVal e as EventArgs)
   Console.WriteLine(vtbar.Value)
   Console.WriteLine(vtbar.LargeChange)
 end sub
 public shared sub Main() 
   Application.Run(new TrackBars())
 end sub

end class</source>

Link ProgressBar with a TrackBar

<source lang="vbnet">Imports System.Windows.Forms public class TrackBarProgressBarLink

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class

Public Class Form1

   Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
       ProgressBar1.Value = TrackBar1.Value
       Label1.Text = ProgressBar1.Value.ToString + "/100"
   End Sub

End Class <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
       Me.Label1 = New System.Windows.Forms.Label
       Me.TrackBar1 = New System.Windows.Forms.TrackBar
       CType(Me.TrackBar1, System.ruponentModel.ISupportInitialize).BeginInit()
       Me.SuspendLayout()
       "
       "ProgressBar1
       "
       Me.ProgressBar1.Location = New System.Drawing.Point(12, 50)
       Me.ProgressBar1.Name = "ProgressBar1"
       Me.ProgressBar1.Size = New System.Drawing.Size(358, 34)
       Me.ProgressBar1.TabIndex = 0
       "
       "Label1
       "
       Me.Label1.AutoSize = True
       Me.Label1.Location = New System.Drawing.Point(160, 23)
       Me.Label1.Name = "Label1"
       Me.Label1.Size = New System.Drawing.Size(35, 12)
       Me.Label1.TabIndex = 1
       Me.Label1.Text = "0/100"
       "
       "TrackBar1
       "
       Me.TrackBar1.Location = New System.Drawing.Point(12, 110)
       Me.TrackBar1.Maximum = 100
       Me.TrackBar1.Name = "TrackBar1"
       Me.TrackBar1.Size = New System.Drawing.Size(358, 45)
       Me.TrackBar1.TabIndex = 2
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(382, 166)
       Me.Controls.Add(Me.TrackBar1)
       Me.Controls.Add(Me.Label1)
       Me.Controls.Add(Me.ProgressBar1)
       Me.Name = "Form1"
       Me.Text = "Form1"
       CType(Me.TrackBar1, System.ruponentModel.ISupportInitialize).EndInit()
       Me.ResumeLayout(False)
       Me.PerformLayout()
   End Sub
   Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
   Friend WithEvents Label1 As System.Windows.Forms.Label
   Friend WithEvents TrackBar1 As System.Windows.Forms.TrackBar

End Class</source>

Use TrackBar to control the Scale

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class TrackBarScale

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisplayScale.Scroll
       DrawingArea.Invalidate()
   End Sub
   Private Sub DrawingArea_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DrawingArea.Paint
       Dim titleFont As Font
       Dim mainFont As Font
       Dim titleArea As Rectangle
       Dim textArea As Rectangle
       Dim titleFormat As StringFormat
       Const MainTitle As String = "www.vbex.ru"
       titleFont = New Font("Arial", 16, FontStyle.Bold)
       mainFont = New Font("Arial", 12, FontStyle.Regular)
       titleArea = New Rectangle(0, 0, DrawingArea.ClientRectangle.Width, titleFont.Height)
       textArea = New Rectangle(0, titleFont.Height * 1.4, DrawingArea.ClientRectangle.Width, DrawingArea.ClientRectangle.Height - (titleFont.Height * 1.4))
       e.Graphics.ScaleTransform(DisplayScale.Value, DisplayScale.Value)
       titleFormat = New StringFormat()
       titleFormat.Alignment = StringAlignment.Center
       e.Graphics.DrawString(MainTitle, titleFont, Brushes.Black, titleArea, titleFormat)
       titleFormat.Dispose()
       mainFont.Dispose()
       titleFont.Dispose()
   End Sub

End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.LabelText = New System.Windows.Forms.Label
       Me.DisplayText = New System.Windows.Forms.TextBox
       Me.LabelScale = New System.Windows.Forms.Label
       Me.DisplayScale = New System.Windows.Forms.TrackBar
       Me.ActDisplay = New System.Windows.Forms.Button
       Me.DrawingArea = New System.Windows.Forms.PictureBox
       CType(Me.DisplayScale, System.ruponentModel.ISupportInitialize).BeginInit()
       CType(Me.DrawingArea, System.ruponentModel.ISupportInitialize).BeginInit()
       Me.SuspendLayout()
       "
       "LabelText
       "
       Me.LabelText.AutoSize = True
       Me.LabelText.Location = New System.Drawing.Point(8, 8)
       Me.LabelText.Name = "LabelText"
       Me.LabelText.Size = New System.Drawing.Size(31, 13)
       Me.LabelText.TabIndex = 0
       Me.LabelText.Text = "&Text:"
       "
       "LabelScale
       "
       Me.LabelScale.AutoSize = True
       Me.LabelScale.Location = New System.Drawing.Point(8, 144)
       Me.LabelScale.Name = "LabelScale"
       Me.LabelScale.Size = New System.Drawing.Size(37, 13)
       Me.LabelScale.TabIndex = 2
       Me.LabelScale.Text = "&Scale:"
       "
       "DisplayScale
       "
       Me.DisplayScale.Location = New System.Drawing.Point(48, 136)
       Me.DisplayScale.Maximum = 5
       Me.DisplayScale.Minimum = 1
       Me.DisplayScale.Name = "DisplayScale"
       Me.DisplayScale.Size = New System.Drawing.Size(104, 45)
       Me.DisplayScale.TabIndex = 3
       Me.DisplayScale.Value = 1
       "
       "DrawingArea
       "
       Me.DrawingArea.BackColor = System.Drawing.Color.White
       Me.DrawingArea.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
       Me.DrawingArea.Location = New System.Drawing.Point(192, 8)
       Me.DrawingArea.Name = "DrawingArea"
       Me.DrawingArea.Size = New System.Drawing.Size(328, 208)
       Me.DrawingArea.TabIndex = 5
       Me.DrawingArea.TabStop = False
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(532, 227)
       Me.Controls.Add(Me.DrawingArea)
       Me.Controls.Add(Me.DisplayScale)
       Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
       Me.MaximizeBox = False
       Me.Name = "Form1"
       Me.Text = "Displaying Scaled Content"
       CType(Me.DisplayScale, System.ruponentModel.ISupportInitialize).EndInit()
       CType(Me.DrawingArea, System.ruponentModel.ISupportInitialize).EndInit()
       Me.ResumeLayout(False)
       Me.PerformLayout()
   End Sub
   Friend WithEvents LabelText As System.Windows.Forms.Label
   Friend WithEvents DisplayText As System.Windows.Forms.TextBox
   Friend WithEvents LabelScale As System.Windows.Forms.Label
   Friend WithEvents DisplayScale As System.Windows.Forms.TrackBar
   Friend WithEvents ActDisplay As System.Windows.Forms.Button
   Friend WithEvents DrawingArea As System.Windows.Forms.PictureBox

End Class</source>