CodeItBetter Programming Another VB Programming Blog

How to add and remove buttons from a scrolled button area

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'Controls - How to add and remove buttons from a scrolled button area
'When the user clicks the Add button, the program uses Load to add a new button
'to the cmdButton control array. It sets the new button's Container so it is in
'the same PictureBox as the other buttons. It then calls subroutine SetScrollBar
'to adjust the buttons' scroll bar.

Private Sub cmdAdd_Click()
    Dim new_index As Integer
 
    new_index = cmdButton.UBound + 1
    Load cmdButton(new_index)
    cmdButton(new_index).Move cmdButton(new_index - 1).Left, cmdButton(new_index - 1).Top + _
        cmdButton(new_index - 1).Height + 120
    cmdButton(new_index).Container = cmdButton(new_index - 1).Container
    cmdButton(new_index).Visible = True
    cmdButton(new_index).Caption = "Button " & Format$(new_index)
 
    SetScrollBar
End Sub
 
Private Sub SetScrollBar()
    ' Size the inner PictureBox.
    InnerPict.Height = cmdButton(cmdButton.UBound).Top + cmdButton(cmdButton.UBound).Height + 120
 
    ' See if we need the scroll bar.
    If InnerPict.Height <= OuterPict.ScaleHeight Then
        InnerPict.Move 0, 0
        VBar.Visible = False
    Else
        ' Set scroll bar properties.
        VBar.Min = 0
        VBar.Max = OuterPict.ScaleHeight - InnerPict.Height
        VBar.LargeChange = OuterPict.ScaleHeight
        VBar.SmallChange = OuterPict.ScaleHeight / 5
        VBar.Visible = True
    End If
End Sub
 
'When the user clicks the Remove button, the program unloads the most recently
'created button from the control array. It then calls subroutine SetScrollBar to
'adjust the buttons' scroll bar.

Private Sub cmdRemove_Click()
    If cmdButton.UBound < 1 Then Exit Sub
 
    Unload cmdButton(cmdButton.UBound)
    SetScrollBar
End Sub
 
'Letting the user add and remove an arbitrary number of buttons
Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.