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
CommandButtonButton
FrameGroupBox
ImagePictureBox
ListBoxcannot habe multiple columns
MultiPageTabControl
OptionbuttonRadioButton
ScrollBarHScrollBar or VScrollBar
SpinButtonNumericUpDown or DomainUpDown
TabStripTabControl
ToggleButtonCheckBox (with appearance = Button)

Different Properties


VBA.NET
TextBox.AlignmentTextBox.TextAlign
TextBox.LockedTextBox.ReadOnly
TextBox.ChangeTextBox.TextChanged
CommanButton.MoveButton.SetBounds
CommanButton.SetFocusButton.Focus
CommanButton.ValidateButton.Validating
TextBox.SelLengthTextBox.SelectionLength
TextBox.SetStartTextBox.SelectionStart
TextBox.SelTextTextBox.SelectionText
Label.AlignmentLabel.TextAlign
Label.BackStyleLabel.BackColor (set as transparent)
Label.CaptionLabel.Text
Label.WordWrap(automatic)
ComboBox.ListComboBox.Items
ComboBox.ListCountComboBox.Count
ComboBox.ListIndexComboBox.SelectedIndex
ComboBox.LockedComboBox.DropDownStyle = DropDownList
ComboBox.StyleComboBox.DropDownStyle
ComboBox.AddItemComboBox.Items.Add (or AddRange or Insert)
ComboBox.RemoveItemComboBox.Items.Remove
ComboBox.ChangeComboBox.TextChanged
ComboBox.ClickComboBox.SelectedIndexChanged
ListBox.ColumnListBox.ColumnWidth (or MultiColumn)
ListBox.ListListBox.Items
ListBox.ListColumnListBox.Count
ListBox.ListindexListBox.SelectedIndex
ListBox.MultiSelectListBox.SelectionMode
ListBox.SelCountListBox.Count
ListBox.SelectedListBox.GetSelected (or SetSelected)
ListBox.AddItemListBox.Items.Add (or AddRange or Insert)
ListBox.RemoveItemListBox.Items.Remove
ListBox.ItemCheckn/a
CheckBox.AlignmentCheckBox.CheckAlign
CheckBox.StyleCheckBox.Appearance
CheckBox.ValueCheckBox.CheckState
CheckBox.ClickCheckBox.CheckStateChanged
OptionButton.AlignmentOptionButton.TextAlign
OptionButton.AppearanceOptionButton.FlatStyle
OptionButton.ValueOptionButton.Checked
OptionButton.ClickOptionButton.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 BoxThis 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 NavigatorThis control doesn't work with the VSTO control hosting architecture. This control relies on a container model that is not supported by VSTO.
MenuStripThis control can only be added to a windows form
RichTextBoxThis 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