ScrollableControl の派生クラスじゃないもの(おもに Panel)をスクロールさせたい時に使うと便利。
ScrollableControl だったら、普通に AutoScroll とかありますからね~。
ちょっと悩んだのはスクロールさせたいもの(Panel)の Dock を Fill にしてたらうまくスクロールしなかった事。
よく考えれば当たり前ですね...。
■参考文献
HScrollBar コントロールおよび VScrollBar コントロール (Windows フォーム)
HScrollBar クラス
ScrollableControl.AutoScroll プロパティ
■実行画像
起動したとこ
スクロールしたとこ
おまけ
Public Class HScrollBarVScrollBarTest
Private m_scrollPanel As Panel
Private m_nakasan As PictureBox
Private Sub HScrollBarVScrollBarTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Clear()
Dim tabCtrl As TabControl = New TabControl()
tabCtrl.Dock = DockStyle.Fill
Me.Controls.Add(tabCtrl)
tabCtrl.TabPages.Clear()
Dim panelScrollPage As TabPage = New TabPage("ScrollTest")
tabCtrl.TabPages.Add(panelScrollPage)
Me.m_scrollPanel = New Panel()
Me.m_scrollPanel.Size = New Size(300, 300)
'Me.m_scrollPanel.Dock = DockStyle.Fill ' ← これはだめです。
' VScrollVar と HScrollBar の配置
Dim vScroll As VScrollBar = New VScrollBar()
vScroll.Dock = DockStyle.Right
vScroll.Minimum = 1
vScroll.Maximum = Me.m_scrollPanel.Height + panelScrollPage.ClientSize.Height
Dim hScroll As HScrollBar = New HScrollBar()
hScroll.Dock = DockStyle.Bottom
hScroll.Minimum = 1
hScroll.Maximum = Me.m_scrollPanel.Width + panelScrollPage.ClientSize.Width
panelScrollPage.Controls.Add(vScroll)
panelScrollPage.Controls.Add(hScroll)
panelScrollPage.Controls.Add(Me.m_scrollPanel)
'Panel 内にコントロールを詰め込む
For hIndex As Integer = 10 To 150 Step 140
For vIndex As Integer = 0 To 9
Dim tBox As TextBox = New TextBox()
tBox.Text = hIndex.ToString() & "-" & (vIndex + 1).ToString()
tBox.Location = New Point(hIndex, 10 + vIndex * 20)
Me.m_scrollPanel.Controls.Add(tBox)
Next
Next
'スクロールされた時のイベントを登録
AddHandler vScroll.ValueChanged, AddressOf Me.VScrollBar_ValueChanged
AddHandler hScroll.ValueChanged, AddressOf Me.HScrollBar_ValueChanged
'おまけ
Dim nakaSanTabPage As TabPage = New TabPage("中さん")
tabCtrl.TabPages.Add(nakaSanTabPage)
Me.m_nakasan = New PictureBox()
Me.m_nakasan.Image = My.Resources.中さん
Me.m_nakasan.SizeMode = PictureBoxSizeMode.Zoom
Me.m_nakasan.Size = New Size(300, 300)
Me.m_nakasan.Location = New Point(0, 0)
' VScrollVar と HScrollBar の配置
Dim nvScroll As VScrollBar = New VScrollBar()
nvScroll.Dock = DockStyle.Left
nvScroll.Minimum = 1
nvScroll.Maximum = Me.m_nakasan.Height
nvScroll.Value = nvScroll.Maximum
Dim nhScroll As HScrollBar = New HScrollBar()
nhScroll.Dock = DockStyle.Bottom
nhScroll.Minimum = 1
nhScroll.Maximum = Me.m_nakasan.Width
nhScroll.Value = nhScroll.Maximum
nakaSanTabPage.Controls.Add(nvScroll)
nakaSanTabPage.Controls.Add(nhScroll)
nakaSanTabPage.Controls.Add(Me.m_nakasan)
'移動情報の取得
AddHandler nvScroll.Scroll, AddressOf NakasanScrollBar_Scroll
AddHandler nhScroll.Scroll, AddressOf NakasanScrollBar_Scroll
'スクロールされた時のイベントを登録
AddHandler nvScroll.ValueChanged, AddressOf NVScrollBar_ValueChanged
AddHandler nhScroll.ValueChanged, AddressOf NHScrollBar_ValueChanged
End Sub
Private Sub VScrollBar_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim vScroll As VScrollBar = DirectCast(sender, VScrollBar)
Me.m_scrollPanel.Top = -vScroll.Value
End Sub
Private Sub HScrollBar_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim hScroll As HScrollBar = DirectCast(sender, HScrollBar)
Me.m_scrollPanel.Left = -hScroll.Value
End Sub
' 中さん関連
Private Sub NVScrollBar_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Me.m_nakasan.Size = New Size(Me.m_nakasan.Size.Width, DirectCast(sender, VScrollBar).Value)
End Sub
Private Sub NHScrollBar_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Me.m_nakasan.Size = New Size(DirectCast(sender, HScrollBar).Value, Me.m_nakasan.Size.Height)
End Sub
Private Sub NakasanScrollBar_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs)
System.Diagnostics.Debug.WriteLine("移動方向:" & e.ScrollOrientation.ToString())
System.Diagnostics.Debug.WriteLine("移動種類:" & e.Type.ToString())
System.Diagnostics.Debug.WriteLine("前の値:" & e.OldValue.ToString())
System.Diagnostics.Debug.WriteLine("新しい値:" & e.NewValue.ToString())
End Sub
End Class