组合框控件、AddItem 方法、Picture 和 PicturePosition 属性示例

下例用组合框来显示控件的图片位置选项。每次用户单击列表选项,命令按钮上的图片和题注都将被更新。这个代码示例还使用了 AddItem 方法来填充组合框选项。

窗体包含:

  • 名为 Label1 的标签。
  • 名为 CommandButton1 的命令按钮。 -名为 ComboBox1 的组合框。
示例
SubUserForm_Initialize() 
UserForm_Label1.Left = 18 
UserForm_Label1.Top = 12 
UserForm_Label1.Height = 12 
UserForm_Label1.Width = 190 
UserForm_Label1.Caption = "选择相对于标题的图片位置" 
 
 '将列表项添加到组合框。每个条目的值与组合框中相应的ListIndex值匹配。
 
UserForm_ComboBox1.AddItem "左上角" 'ListIndex = 0 
UserForm_ComboBox1.AddItem "左中" 'ListIndex = 1 
UserForm_ComboBox1.AddItem "左下角" 'ListIndex = 2 
UserForm_ComboBox1.AddItem "右上方" 'ListIndex = 3 
UserForm_ComboBox1.AddItem "右中" 'ListIndex = 4 
UserForm_ComboBox1.AddItem "右下角" 'ListIndex = 5 
UserForm_ComboBox1.AddItem "左上方" 'ListIndex = 6 
UserForm_ComboBox1.AddItem "居中之上" 'ListIndex = 7 
UserForm_ComboBox1.AddItem "右上方" 'ListIndex = 8 
UserForm_ComboBox1.AddItem "左下角" 'ListIndex = 9 
UserForm_ComboBox1.AddItem "居中以下" 'ListIndex = 10 
UserForm_ComboBox1.AddItem "右下" 'ListIndex = 11 
UserForm_ComboBox1.AddItem "居中" 'ListIndex = 12 
 
'使用下拉列表
UserForm_ComboBox1.Style = fmStyleDropDownList 
 
 '组合框值是列表索引值 
UserForm_ComboBox1.BoundColumn = 0 
 
 '将组合框设置为第一个条目
UserForm_ComboBox1.ListIndex = 0 
 
 
UserForm_ComboBox1.Left = 18 
UserForm_ComboBox1.Top = 36 
UserForm_ComboBox1.Width = 90 
UserForm_ComboBox1.ListWidth = 90 
 
 '初始化CommandButton1 
UserForm_CommandButton1.Left = 18 
UserForm_CommandButton1.Top = 56 
UserForm_CommandButton1.Height = 180 
UserForm_CommandButton1.Width = 200 
 
 '注意:请确保引用系统中存在的位图文件,并在文件名中包含路径
UserForm_CommandButton1.Picture = LoadPicture("E:\Weisoft Stock(x64)6230\AddinSkin\Background\黑绿.bmp") 
UserForm_CommandButton1.PicturePosition =UserForm_ComboBox1.Value 
End Sub 
 
SubUserForm_ComboBox1_Click() 
 Select CaseUserForm_ComboBox1.Value 
 Case 0 'Left Top 
UserForm_CommandButton1.Caption = "左上角" 
UserForm_CommandButton1.PicturePosition =UserForm_fmPicturePositionLeftTop 
 
 Case 1 'Left Center 
UserForm_CommandButton1.Caption = "左中" 
UserForm_CommandButton1.PicturePosition =UserForm_fmPicturePositionLeftCenter 
 
 Case 2 'Left Bottom 
UserForm_CommandButton1.Caption = "左下角" 
UserForm_CommandButton1.PicturePosition =UserForm_fmPicturePositionLeftBottom 
 
 Case 3 'Right Top 
UserForm_CommandButton1.Caption = "右上方" 
UserForm_CommandButton1.PicturePosition =UserForm_fmPicturePositionRightTop 
 
 Case 4 'Right Center 
UserForm_CommandButton1.Caption = "右中" 
UserForm_CommandButton1.PicturePosition =UserForm_fmPicturePositionRightCenter 
 
 Case 5 'Right Bottom 
UserForm_CommandButton1.Caption = "右下角" 
UserForm_CommandButton1.PicturePosition =UserForm_fmPicturePositionRightBottom 
 
 Case 6 'Above Left 
UserForm_CommandButton1.Caption = "左上方" 
UserForm_CommandButton1.PicturePosition =UserForm_fmPicturePositionAboveLeft 
 
 Case 7 'Above Center 
UserForm_CommandButton1.Caption = "居中之上" 
UserForm_CommandButton1.PicturePosition =UserForm_fmPicturePositionAboveCenter 
 
 Case 8 'Above Right 
UserForm_CommandButton1.Caption = "右上方" 
UserForm_CommandButton1.PicturePosition =UserForm_fmPicturePositionAboveRight 
 
 Case 9 'Below Left 
UserForm_CommandButton1.Caption = "左下角" 
UserForm_CommandButton1.PicturePosition =UserForm_fmPicturePositionBelowLeft 
 
 Case 10 'Below Center 
UserForm_CommandButton1.Caption = "居中以下" 
UserForm_CommandButton1.PicturePosition =UserForm_fmPicturePositionBelowCenter 
 
 Case 11 'Below Right 
UserForm_CommandButton1.Caption = "右下" 
UserForm_CommandButton1.PicturePosition =UserForm_fmPicturePositionBelowRight 
 
 Case 12 'Centered 
UserForm_CommandButton1.Caption = "居中" 
UserForm_CommandButton1.PicturePosition =UserForm_fmPicturePositionCenter 
 
 End Select 
 
End Sub
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106