# Windows Forms

## Creating a Project

You can create a module or script project.

Click File->New->Project

![](https://i0.wp.com/wandering.life/wp-content/uploads/2017/04/newproject.png?resize=581%2C155)

Select the Module or Script project type, name it and then click Ok.

![](https://i1.wp.com/wandering.life/wp-content/uploads/2017/04/newproject2.png?resize=753%2C522)

## Create the Form

After installing the Pro tools, you should now have a Form item template available. Right click on your project and select Add->New Item.

![](https://i1.wp.com/wandering.life/wp-content/uploads/2017/04/newitem.png?resize=481%2C222)

Once the New Item dialog pops up, select the PowerShell Form template, name it and click Ok.

![](https://i0.wp.com/wandering.life/wp-content/uploads/2017/04/additem2.png?resize=696%2C482)

## Working with the Form Designer

### Adding Controls

The form designer works the same way with any language. You can select items from the Toolbox window and drag them onto your form. Properties of the controls can be set using the Properties window.

![](https://i2.wp.com/wandering.life/wp-content/uploads/2017/04/workingwithforms.png?resize=849%2C457)

Adding controls automatically updates the Form.Designer.ps1 file. Do not edit this file by hand as the editor will simply recreate it after changes are made to the form.

![](https://i2.wp.com/wandering.life/wp-content/uploads/2017/04/designer.png?resize=682%2C296)

### Adding Event Handlers

To do anything interesting, you’ll need to add event handles. You can access a control’s events by selecting it in the designer and clicking the Event button in the Properties window.

![](https://i1.wp.com/wandering.life/wp-content/uploads/2017/04/events.png?resize=628%2C255)

Enter the name of your event handler function and click enter.

![](https://i1.wp.com/wandering.life/wp-content/uploads/2017/04/createhandler.png?resize=325%2C149)

After you press enter, you will be moved into the code-behind view where you can wire up your event handler.

![](https://i1.wp.com/wandering.life/wp-content/uploads/2017/04/codebehind.png?resize=628%2C204)

The event handler will automatically be wired up to your control.

![](https://i1.wp.com/wandering.life/wp-content/uploads/2017/04/add_Click.png?resize=515%2C82)

## Debugging A Form

Once you are ready to test out your form, you can click Start or press F5 from either the designer window or the code-behind window. PoshTools will fire off the script and you can set breakpoints and debug like any other PowerShell script.

![](https://i0.wp.com/wandering.life/wp-content/uploads/2017/04/debuger.png?resize=709%2C237)

And just like that you have a working Windows Form.

![](https://i1.wp.com/wandering.life/wp-content/uploads/2017/04/running.png?resize=775%2C400)

## Accessing Controls Added to the Form

In many circumstances, you’ll want to access a control that you’ve add to the form. The $MainForm variable will have parameters for each of it's child items. You can access those through the control's name.

```
$MainForm.lblMyLabel.Value = "Some Text"
```

## Conclusion

The PowerShell scripts generated by PoshProTools can be used in any PowerShell host. The designer and code-behind files can be joined into a single script. You can use the bundling functionality of PoshProTools to do this automatically.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.poshtools.com/powershell-tools-documentation/visual-studio/user-interface-design/building-a-gui-with-windows-forms-in-visual-studio.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
