4b. Adding form fields : Add FormObject - CodeBlueG/PSForms GitHub Wiki
Having added a new row, now add the fields. Each field that is added should be created as a variable so that it can be referenced or altered later.
Field types that can be added are;
- CheckBox
- CheckedListBox
- ComboBox
- DateTimePicker
- GroupBox
- Label
- LinkLabel
- NumericUpDown
- Panel
- ProgressBar
- RadioButton
- RichTextBox
- TextBox
To add a field to the form, the "Add-FormObject" cmdlet is run and the following parameters are available;
- FormObject
- objectType
- cellSize
- cellFont
- cellFontSize
- cellText
- textAlign
- dropDown
- hidden
- disabled
- readOnly
- border
- mandatory
- previousColumn
- bold
- italic
- underline
- checked
- showCheckBox
formObject
This is a mandatory field
Enter the name of the form that you are creating, this is the same as the name used in the "Initialize-Form" cmdlet.
objectType
The type of form object to be added, taken from the list above.
cellSize
The width sizing of the object, this can take the values "Full", "HalfLeft", "HalfRight", "1QLeft", "3QRight" and"Double". The default value is "Full" and can be left blank for a 'normal' sized cell. The options "HalfLeft" and "HalfRight" would normally be used together so that two cells fill a normal cell area. The same is true for "1QLeft" and "3QRight".
cellfont and cellfontsize
Used to change the font style and size for a cell. These are cosmetic changes and won't change any sizing calculations for the cell or form, therefore adding a cell with a larger font might result in the text not showing correctly if it is too large for the cell size. Note that the font must be available on the target machine.
cellText
The text to be shown in the cell at the time of loading. This is especially useful for labels, but can also be used in other fields.
textAlign
To change the default text alignment of a cell. Note that this entry must follow the options available for that kind of field, for instance, a label uses "content alignment" values (from "topleft" through "middleCentre" to "bottomright") while a textbox uses "horizontal alignment" values ("left", "centre" and "right"). Check MSDN for which values are required for which field type.
dropDown
Applies only to the "comboBox" field type
Changes the behaviour of a comboBox to act as a dropDown comboBox. A normal comboBox will only allow fields in the list to be selected whilst a dropDown box will allow other values to be entered.
hidden and disabled
These switches will change the visibility of the field when the form is shown.
readOnly
Will make a field read only. Will only apply to fields which allow this setting.
border
Adds a border around an object. For some field types, such as a textbox, this won't be visible due to the styling, but for others it can change the visibility - a label is a good example of that.
mandatory
Makes a field mandatory and this can be checked later in the script to ensure that fields have been completed.
bold, italic, underline and strikeout
Change the appearance of the text in the field. These can be combined in any order to change how the text is displayed.
checked
This only applies to checkboxes and allows the checkbox to be checked when the form is opened. For a date and time picker, this can be used if the 'checked' switch has been applied.
For all other objects, this setting is ignored.
showCheckBox
This only applies to a date and time picker and will show the check box in the picker. With this, the check box will enable and disable the date picker section. If this switch is used, the 'checked' switch can also be applied to the field.
previousColumn
This allows a cell to take a place in the previous column. An example of this would be a double sized field that is added to the second column so that the tab order is correct. With the previousColumn switch used, the field will appear in the first and second columns, not the second and third. This is shown in the example below.
Examples
The following lines of code create the form as shown and illustrates some of the field types and sizes.
Note that the large text box at the bottom is "double" size and created in the second column, but fills column 1 and 2 which means the tab order will work as expected. This is achieved using the "previousColumn" switch.
#Column 1
$TestForm = Add-Row -formObject $TestForm -rowsHigh 1 -newColumn
$lblLabel1 = Add-FormObject -formObject $TestForm -objectType label -cellText "Enter text:" -cellFontSize 12
$TestForm = Add-Row -formObject $TestForm
$lblLabel2 = Add-FormObject -formObject $TestForm -objectType label -cellText "Enter more text:" -bold
$TestForm = Add-Row -formObject $TestForm
$lblLabel3 = Add-FormObject -formObject $TestForm -objectType label -cellText "Select item:"
$TestForm = Add-Row -formObject $TestForm
$lblLabel4 = Add-FormObject -formObject $TestForm -objectType label -cellText "Start and End date:" -underline
# Column 2
$TestForm = Add-Row -formObject $TestForm -rowsHigh 1 -newColumn
$txtTextBox1 = Add-FormObject -formObject $TestForm -objectType TextBox
$TestForm = Add-Row -formObject $TestForm
$txtTextBox2 = Add-FormObject -formObject $TestForm -objectType TextBox
$TestForm = Add-Row -formObject $TestForm
$txtTextBox2 = Add-FormObject -formObject $TestForm -objectType ComboBox
$TestForm = Add-Row -formObject $TestForm
$datDatePicker1 = Add-FormObject -formObject $TestForm -objectType DateTimePicker -cellSize HalfLeft
$datDatePicker2 = Add-FormObject -formObject $TestForm -objectType DateTimePicker -cellSize HalfRight
$TestForm = Add-Row -formObject $TestForm -rowsHigh 4
$txtTextBox2 = Add-FormObject -formObject $TestForm -objectType TextBox -cellSize Double -previousColumn