VB.Net Tutorial/GUI/ScrollBar

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

Scroll Bar scrolls and get its current value

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ScrollBarControl
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Inherits System.Windows.Forms.Form
    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Private components As System.ruponentModel.IContainer
    Friend WithEvents HScrollBar1 As System.Windows.Forms.HScrollBar
    Friend WithEvents VScrollBar1 As System.Windows.Forms.VScrollBar
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.HScrollBar1 = New System.Windows.Forms.HScrollBar
        Me.VScrollBar1 = New System.Windows.Forms.VScrollBar
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.Label5 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        "
        "HScrollBar1
        "
        Me.HScrollBar1.Location = New System.Drawing.Point(24, 208)
        Me.HScrollBar1.Name = "HScrollBar1"
        Me.HScrollBar1.Size = New System.Drawing.Size(176, 16)
        Me.HScrollBar1.TabIndex = 2
        Me.HScrollBar1.Value = 50
        "
        "VScrollBar1
        "
        Me.VScrollBar1.LargeChange = 1
        Me.VScrollBar1.Location = New System.Drawing.Point(208, 32)
        Me.VScrollBar1.Maximum = 3
        Me.VScrollBar1.Name = "VScrollBar1"
        Me.VScrollBar1.Size = New System.Drawing.Size(16, 168)
        Me.VScrollBar1.TabIndex = 3
        "
        "Label1
        "
        Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.Label1.Location = New System.Drawing.Point(24, 32)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(176, 168)
        Me.Label1.TabIndex = 4
        Me.Label1.Text = "Visual Basic"
        "
        "Label2
        "
        Me.Label2.Location = New System.Drawing.Point(88, 240)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(56, 16)
        Me.Label2.TabIndex = 5
        Me.Label2.Text = "Font Size"
        "
        "Label3
        "
        Me.Label3.Location = New System.Drawing.Point(232, 48)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(48, 24)
        Me.Label3.TabIndex = 6
        Me.Label3.Text = "Red"
        "
        "Label4
        "
        Me.Label4.Location = New System.Drawing.Point(232, 96)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(48, 24)
        Me.Label4.TabIndex = 7
        Me.Label4.Text = "Yellow"
        "
        "Label5
        "
        Me.Label5.Location = New System.Drawing.Point(232, 152)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(40, 16)
        Me.Label5.TabIndex = 8
        Me.Label5.Text = "Green"
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.VScrollBar1)
        Me.Controls.Add(Me.HScrollBar1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
        Label1.Font = New Font(Label1.Font.FontFamily, HScrollBar1.Value)
    End Sub
    Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
        Select Case VScrollBar1.Value
            Case 1
                Label1.ForeColor = Color.Red
            Case 2
                Label1.ForeColor = Color.Yellow
            Case 3
                Label1.ForeColor = Color.Green
        End Select
    End Sub
End Class

Use ScollBar to control font size

Imports System.Windows.Forms
public class ScrollBarControlFontSize
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    
    Private Sub HScrollBar1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HScrollBar1.ValueChanged
        Dim mysize As Integer
        mysize = HScrollBar1.Value
        TextBox1.Font = New System.Drawing.Font("", mysize)
    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.HScrollBar1 = New System.Windows.Forms.HScrollBar
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        "
        "HScrollBar1
        "
        Me.HScrollBar1.LargeChange = 5
        Me.HScrollBar1.Location = New System.Drawing.Point(23, 145)
        Me.HScrollBar1.Maximum = 55
        Me.HScrollBar1.Minimum = 5
        Me.HScrollBar1.Name = "HScrollBar1"
        Me.HScrollBar1.Size = New System.Drawing.Size(330, 21)
        Me.HScrollBar1.TabIndex = 1
        Me.HScrollBar1.Value = 5
        "
        "TextBox1
        "
        Me.TextBox1.Location = New System.Drawing.Point(18, 14)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(334, 101)
        Me.TextBox1.TabIndex = 2
        Me.TextBox1.Text = "www.vbex.ru"
        "
        "Label1
        "
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(25, 125)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(53, 12)
        Me.Label1.TabIndex = 3
        Me.Label1.Text = "Font Size"
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(378, 175)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.HScrollBar1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
        Me.PerformLayout()
    End Sub
    Friend WithEvents HScrollBar1 As System.Windows.Forms.HScrollBar
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
End Class

Use ScrollBars to control the scroll of an Image

imports System
imports System.Drawing
imports System.Windows.Forms
public class ScrollBars : inherits Form
  dim pnl as Panel
  dim pb as PictureBox
  dim hbar as HScrollBar
  dim vbar as VScrollBar
  dim img as Image
  public sub New()
    Size = new Size(480,300)
    img = Image.FromFile("yourfile.jpg")
    pnl = new Panel()
    pnl.Parent = me
    pnl.Size = new Size(400,200)
    pnl.Location = new Point(10,10)
    pnl.BorderStyle = BorderStyle.FixedSingle
    pb = new PictureBox()
    pb.Parent = pnl
    pb.Size = new Size(img.Size.Width, img.Size.Height)
    pb.Location = new Point(0, 0)
    pb.SizeMode = PictureBoxSizeMode.CenterImage
    pb.Image = img
    hbar = new HScrollBar()
    hbar.Parent = me
    hbar.Location = new Point(pnl.Left, pnl.Bottom + 25)
    hbar.Size = new Size(pnl.Width, 25)
    hbar.Minimum = 0
    hbar.Maximum = 100
    hbar.SmallChange = 1
    hbar.LargeChange = 10
    hbar.Value = 10
    AddHandler hbar.ValueChanged, AddressOf hbar_OnValueChanged
    vbar = new VScrollBar()
    vbar.Parent = me
    vbar.Location = new Point(pnl.Right + 25, pnl.Top)
    vbar.Size = new Size(25, pnl.Height)
    vbar.Minimum = 0
    vbar.Maximum = 100
    vbar.SmallChange = 1
    vbar.LargeChange = 10
    vbar.Value = CType((vbar.Maximum - vbar.Minimum) / 2, integer)
    AddHandler vbar.ValueChanged, AddressOf vbar_OnValueChanged
  end sub  "  close for constructor
  private sub hbar_OnValueChanged(ByVal sender as object,ByVal e as EventArgs)
    pb.Location = new Point(CType((pnl.Size.Width - img.Size.Width) * _
      hbar.Value /(hbar.Maximum - hbar.LargeChange + 1), integer), _
      pb.Top)
  end sub
  private sub vbar_OnValueChanged(ByVal sender as object,ByVal e as EventArgs)
    pb.Location = new Point(pb.Left, _
      CType((pnl.Size.Height - img.Size.Height) * _
      vbar.Value / (vbar.Maximum - vbar.LargeChange + 1), integer))
  end sub
  public shared sub Main() 
    Application.Run(new ScrollBars())
  end sub
end class

Use ScrollBar to control the image scroll in a PictureBox

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class UseScrollBarToControlTheScrollingOfAnImage
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        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.
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents HScrollBar1 As System.Windows.Forms.HScrollBar
    Friend WithEvents VScrollBar1 As System.Windows.Forms.VScrollBar
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.HScrollBar1 = New System.Windows.Forms.HScrollBar
        Me.VScrollBar1 = New System.Windows.Forms.VScrollBar
        Me.Button1 = New System.Windows.Forms.Button
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.SuspendLayout()
        "
        "PictureBox1
        "
        Me.PictureBox1.Location = New System.Drawing.Point(64, 72)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(168, 144)
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        "
        "Label1
        "
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(0, 0)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(264, 48)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Handling Images"
        "
        "HScrollBar1
        "
        Me.HScrollBar1.Location = New System.Drawing.Point(64, 200)
        Me.HScrollBar1.Name = "HScrollBar1"
        Me.HScrollBar1.Size = New System.Drawing.Size(168, 16)
        Me.HScrollBar1.TabIndex = 2
        Me.HScrollBar1.Visible = False
        "
        "VScrollBar1
        "
        Me.VScrollBar1.Location = New System.Drawing.Point(216, 72)
        Me.VScrollBar1.Name = "VScrollBar1"
        Me.VScrollBar1.Size = New System.Drawing.Size(16, 128)
        Me.VScrollBar1.TabIndex = 3
        Me.VScrollBar1.Visible = False
        "
        "Button1
        "
        Me.Button1.Location = New System.Drawing.Point(112, 248)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 4
        Me.Button1.Text = "Load Image"
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.VScrollBar1)
        Me.Controls.Add(Me.HScrollBar1)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.PictureBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
         PictureBox1.Image = Image.FromFile("yourfile.jpg")
         HScrollBar1.Maximum = PictureBox1.Image.Width - PictureBox1.Width + HScrollBar1.Height
         VScrollBar1.Maximum = PictureBox1.Image.Height - PictureBox1.Height + VScrollBar1.Width
         VScrollBar1.Visible = True
         HScrollBar1.Visible = True
         If PictureBox1.Height > PictureBox1.Image.Height Then
             VScrollBar1.Visible = False
         End If
         If PictureBox1.Width > PictureBox1.Image.Width Then
             HScrollBar1.Visible = False
         End If
    End Sub
    Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
        Dim gphPictureBox As Graphics = PictureBox1.CreateGraphics()
        gphPictureBox.DrawImage(PictureBox1.Image, New Rectangle(0, 0, _
            PictureBox1.Width - HScrollBar1.Height, _
            PictureBox1.Height - VScrollBar1.Width), _
            New Rectangle(HScrollBar1.Value, VScrollBar1.Value, _
            PictureBox1.Width - HScrollBar1.Height, _
            PictureBox1.Height - VScrollBar1.Width), GraphicsUnit.Pixel)
    End Sub
    Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
        Dim gphPictureBox As Graphics = PictureBox1.CreateGraphics()
        gphPictureBox.DrawImage(PictureBox1.Image, New Rectangle(0, 0, _
            PictureBox1.Width - HScrollBar1.Height, _
            PictureBox1.Height - VScrollBar1.Width), _
            New Rectangle(HScrollBar1.Value, VScrollBar1.Value, _
            PictureBox1.Width - HScrollBar1.Height, _
            PictureBox1.Height - VScrollBar1.Width), GraphicsUnit.Pixel)
    End Sub
End Class

Value change event: Verical/Horizontal scroll bar

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class PictureMouseDownEvent
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        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.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents HScrollBar1 As System.Windows.Forms.HScrollBar
    Friend WithEvents VScrollBar1 As System.Windows.Forms.VScrollBar
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.HScrollBar1 = New System.Windows.Forms.HScrollBar
        Me.VScrollBar1 = New System.Windows.Forms.VScrollBar
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        "
        "TextBox1
        "
        Me.TextBox1.Location = New System.Drawing.Point(24, 88)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(200, 20)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = ""
        "
        "HScrollBar1
        "
        Me.HScrollBar1.Location = New System.Drawing.Point(24, 152)
        Me.HScrollBar1.Name = "HScrollBar1"
        Me.HScrollBar1.Size = New System.Drawing.Size(200, 16)
        Me.HScrollBar1.TabIndex = 2
        "
        "VScrollBar1
        "
        Me.VScrollBar1.Location = New System.Drawing.Point(240, 88)
        Me.VScrollBar1.Name = "VScrollBar1"
        Me.VScrollBar1.TabIndex = 3
        "
        "TextBox2
        "
        Me.TextBox2.Location = New System.Drawing.Point(24, 120)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(200, 20)
        Me.TextBox2.TabIndex = 4
        Me.TextBox2.Text = ""
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.VScrollBar1)
        Me.Controls.Add(Me.HScrollBar1)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        HScrollBar1.Minimum = 0
        HScrollBar1.Maximum = 100
        HScrollBar1.LargeChange = 20
        HScrollBar1.SmallChange = 5
        HScrollBar1.Value = 25
        VScrollBar1.Minimum = 0
        VScrollBar1.Maximum = 100
        VScrollBar1.LargeChange = 20
        VScrollBar1.SmallChange = 5
        VScrollBar1.Value = 25
        TextBox1.Text = "Horizontal scroll value: " & HScrollBar1.Value
        TextBox2.Text = "Vertical scroll value: " & VScrollBar1.Value
    End Sub
    Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
        TextBox1.Text = "Horizontal scroll value: " & HScrollBar1.Value
        TextBox2.Text = "Vertical scroll value: " & e.NewValue
    End Sub
    Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
        TextBox1.Text = "Horizontal scroll value: " & e.NewValue
        TextBox2.Text = "Vertical scroll value: " & VScrollBar1.Value
    End Sub
End Class