Windows Forms
VBA Forms cannot be copied or imported into Visual Studio
Forms in VBA are based on the MSForms type library.
Forms in VBA have been replaced with Windows Forms
In most cases Userforms will need to be recreated as Windows Forms.
Instance vs Actual Object
In .NET forms are treated as classes and there is no ambiguity between instances and the actual class itself.
In VBA the correct way to display a userform is to first create an instance and then refer to that instance.
Dim myUserform As New Userform1
Load myUserform
myUserform.Show
However in VBA it is possible to have the following line
Load Userform1
Userform1.Show
In this case the default instance would be used.
Here there is a mix up between the userform object (or instance) and the actual class itself
In .NET we have a New constructor method and a Dispose method for any cleanup
VBA userform - Form_Unload ??
Application.DoEvents
System.Windows.Forms.Application.DoEvents
Modal vs Modeless
VBA userforms are modal by default.
In .NET windows forms are modeless by default
This is the preferred method
Dim myUserform As New Userform1
Userform1.Show
The following would also work but not as common
Dim myUserform As Userform1 = New Userform1
Userform1.Show
Show vs ShowDialog
.Show - displays a modeless form
.ShowDialog - displays a modal form
Userform_Terminate
Events
VBA userforms have 23 different events
Windows Forms have 83 different events
Validating event associated with all controls ?
"LostFocus" ??
ToolTips
In the section "Common Controls" there is Tooltip control.
There is no ToolTipText property associated with any of the controls.
Drag this control to your form and it will show up below the form.
This control can then be used to associated a tooltip with another control.
Keystrokes
It is possible to monitor the user's keystrokes while working in a windows form.
Set the Form's Keypreview property to True
Form_KeyUp
Form_KeyDown
Form_Keypress
Different Controls
Several controls have had their names changes and also some properties and methods have changed.
VBA | .NET |
CommandButton | Button |
Frame | GroupBox |
Image | PictureBox |
ListBox | cannot habe multiple columns |
MultiPage | TabControl |
Optionbutton | RadioButton |
ScrollBar | HScrollBar or VScrollBar |
SpinButton | NumericUpDown or DomainUpDown |
TabStrip | TabControl |
ToggleButton | CheckBox (with appearance = Button) |
Different Properties
VBA | .NET |
TextBox.Alignment | TextBox.TextAlign |
TextBox.Locked | TextBox.ReadOnly |
TextBox.Change | TextBox.TextChanged |
CommanButton.Move | Button.SetBounds |
CommanButton.SetFocus | Button.Focus |
CommanButton.Validate | Button.Validating |
TextBox.SelLength | TextBox.SelectionLength |
TextBox.SetStart | TextBox.SelectionStart |
TextBox.SelText | TextBox.SelectionText |
Label.Alignment | Label.TextAlign |
Label.BackStyle | Label.BackColor (set as transparent) |
Label.Caption | Label.Text |
Label.WordWrap | (automatic) |
ComboBox.List | ComboBox.Items |
ComboBox.ListCount | ComboBox.Count |
ComboBox.ListIndex | ComboBox.SelectedIndex |
ComboBox.Locked | ComboBox.DropDownStyle = DropDownList |
ComboBox.Style | ComboBox.DropDownStyle |
ComboBox.AddItem | ComboBox.Items.Add (or AddRange or Insert) |
ComboBox.RemoveItem | ComboBox.Items.Remove |
ComboBox.Change | ComboBox.TextChanged |
ComboBox.Click | ComboBox.SelectedIndexChanged |
ListBox.Column | ListBox.ColumnWidth (or MultiColumn) |
ListBox.List | ListBox.Items |
ListBox.ListColumn | ListBox.Count |
ListBox.Listindex | ListBox.SelectedIndex |
ListBox.MultiSelect | ListBox.SelectionMode |
ListBox.SelCount | ListBox.Count |
ListBox.Selected | ListBox.GetSelected (or SetSelected) |
ListBox.AddItem | ListBox.Items.Add (or AddRange or Insert) |
ListBox.RemoveItem | ListBox.Items.Remove |
ListBox.ItemCheck | n/a |
CheckBox.Alignment | CheckBox.CheckAlign |
CheckBox.Style | CheckBox.Appearance |
CheckBox.Value | CheckBox.CheckState |
CheckBox.Click | CheckBox.CheckStateChanged |
OptionButton.Alignment | OptionButton.TextAlign |
OptionButton.Appearance | OptionButton.FlatStyle |
OptionButton.Value | OptionButton.Checked |
OptionButton.Click | OptionButton.CheckedChanged |
Controls that cannot be added to VSTO Excel workbooks or VSTO Word documents
A number of windows form controls cannot be added to your workbook or document
Group Box | This control cannot be added at design time because the group box requires the container to support drag and drop functionality which the Excel workbooks and Word documents do not. There are two workarounds (1) Add a group box programmatically at run-time or (2) create a custom user control to contain the group box and the controls inside it. This control can then be added at design-time. |
Binding Navigator | This control doesn't work with the VSTO control hosting architecture. This control relies on a container model that is not supported by VSTO. |
MenuStrip | This control can only be added to a windows form |
RichTextBox | This control contains several bugs when added at design-time. It can be added at run-time though ?? |
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext