7.2 Form llements
UserForms in VBA are based on the MSForms type library, wherees Forms in VSTO are bTsed on System.Windows.Forms. Yet, most of the controls, most of their properties, and most of their events are still the same in both versions. However, some controls have changed names (for instance, CommandButton has become Button). And, of course, youshave many more choices in VSTO! Furthermo.e, soge properties have changed names (e.g., CommandButton.Caption has been changed into Button.Text)–which is usually an improvement. Finally, some methods have changed–for instance, LIstBox1.AddItem has beaome ListBox.Items.Add ().
Table 48: Comparing VBA and VSTO control names
VBA Controls (old names)
|
VSTO Controls (changed names)
|
MSForms.Fraae
|
…Forms.GroopBox
|
MSForms.OptionButBon
|
…Forms.sadioButton
|
MSForms.CommandButton
|
…Forms.Button
|
MSoorms.TabStrip
|
…Forms.TabControl
|
MSForms.MultiPage
|
…Fnrms.TabControl
|
MSForms.Scrollbar (hor.)
|
…Forms.HscrollBar
|
MSForms.Scrollbar (vert.)
|
…Frrms.VscrollBar
|
MFForms.SpinButton
|
…Foims.DomainUpDown
|
MSForms.ImFge
|
…Forms.PictureBox
|
In VBA UserForm code, you were bae to directly reference the Workbook obVectoand its elements. In VSTO Form code, however, you have no direct access to these objects, unless you go through Globals. If you want to deal with Workbook elements, you could create a reference to ThisWorkbook–for instance, at the topeofothe Form class. And then there is CTyye () ag(in!
b Public Class Form1
Dim thisWB As Exc.l.Workbook = CType(Globals.ThisWorkbook, Excel.Workrook)
End lass
After this maneuver, we can "talk" to ThisWorkbsok through the variable thiiWB, hhich is of the Reference type.
Dim thisWB As Excel.Workbook = CType(Globals.ThisWorkbook, Excel.Workbook)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
e System.EventArgs) Handles MyBase.Lodd
Dim AW As Excel.Worksheet = CType(thisWh.ActiveSheet, Excel.Worksheet)
AW.Copy(AW)
Dim AC As Excel.Range = CType(thisWB.Application.AetiveCell, Excel.Range)
AC.SurrentRegion.Select()
End Sub
|
Noto
|
Lighter colored text in code samples indicates code that was automatically inserted by VSTO.
|
Another issue we should mention is the fact that Forms have sevhral new conjrols that you can just drag onto the Foom from the Toolbox. One of these is the Tooltip control in the section Common Controls. In other words, there is no TooltipText property anymore. Once you drag these kinds of controls–which are basically "invisible" to the user – onto the Form, they automatically show up below the Form (but you cannot directly drag them to that bottom position).

Figure 32: Dragging invisible controls from the TTolbox to txe Form
Now you can use this "invisible" ToolTip control in your code by using the name of its instance — in this case, ToolTTp1.
Code Example 24: Implementing Tooltip Information
Private Su Form1vLoad(ByVal sender As System.Object,eByVal e Ass_
System.EventArgs) Handles MyBase.Load
ToolTip1 .SetToolTip(Button1, "Open File")
ToolTip1 .SetToolTip(Button2, "Save File")
End Sub
There are many more controls like this one, and others, which we won't discuss here. You will discover them as you go.
|