下列範例示範搭配獨立ScrollBar使用時的 Max和Min屬性。 使用者可以將 Max 和 Min 值設定為 -1000 到 1000 範圍內的任何整數。 此範例也會使用 TextBox.MaxLength 屬性來限制為 Max 和 Min 值輸入的字元數。
若要使用本範例,請將此範例程式碼複製到表單的指令碼編輯器中。 若要執行該程式碼,您需要開啟該表單以啟動 Open 事件。 請確定表單包含:
一個名為 Label1 的 Label 。
一個名為 TextBox1 的 TextBox ,它結合到名為 ScrollBarMin 的自訂數字欄位。
一個名為 Label2 的 Label 。
一個名為 TextBox2 的 TextBox ,它結合到名為 ScrollBarMax 的自訂數字欄位。
一個名為 ScrollBar1 的 ScrollBar ,它結合到名為 ScrollBarValue 的自訂數字欄位。
一個名為 Label3 的 Label 。
Sub Item_Open()
Set Label1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("Label1")
Set ScrollBar1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ScrollBar1")
Set TextBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox1")
Set Label2 = Item.GetInspector.ModifiedFormPages("P.2").Controls("Label2")
Set TextBox2 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox2")
Set Label3 = Item.GetInspector.ModifiedFormPages("P.2").Controls("Label3")
Label1.Caption = "Min -1000 to 1000"
ScrollBar1.Min = -1000
TextBox1.Text = ScrollBar1.Min
TextBox1.MaxLength = 5
Label2.Caption = "Max -1000 to 1000"
ScrollBar1.Max = 1000
TextBox2.Text = ScrollBar1.Max
TextBox2.MaxLength = 5
ScrollBar1.SmallChange = 1
ScrollBar1.LargeChange = 100
ScrollBar1.Value = 0
Label3.Caption = ScrollBar1.Value
End Sub
Sub Item_CustomPropertyChange(byval pname)
Set ScrollBar1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ScrollBar1")
Set TextBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox1")
Set TextBox2 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox2")
Set Label3 = Item.GetInspector.ModifiedFormPages("P.2").Controls("Label3")
If pname = "ScrollBarMin" Then
If IsNumeric(TextBox1.Text) Then
TempNum = CInt(TextBox1.Text)
If TempNum >= -1000 And TempNum <= 1000 Then
ScrollBar1.Min = TempNum
Else
TextBox1.Text = ScrollBar1.Min
End If
Else
TextBox1.Text = ScrollBar1.Min
End If
ElseIf pname = "ScrollBarMax" Then
If IsNumeric(TextBox2.Text) Then
TempNum = CInt(TextBox2.Text)
If TempNum >= -1000 And TempNum <= 1000 Then
ScrollBar1.Max = TempNum
Else
TextBox2.Text = ScrollBar1.Max
End If
Else
TextBox2.Text = ScrollBar1.Max
End If
ElseIf pname = "ScrollBarValue" Then
Label3.Caption = ScrollBar1.Value
End If
End Sub
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。