# Learning WPF: Using the Toolbox

This is the next step in my WPF learning journey. I will look at creating `TextBox`, `TextBlock`, and `Button` controls. This follows on from my previous blog titled *Day 1 with WPF: Creating projects*. Making a `TextBox`, `TextBlock` or `Button` is simple using WPF. These options can be found in the **Toolbox** of your project.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675891944675/c1653671-5415-4dbc-b6dd-d6379172efba.png align="center")

## The `Button` control

### Creating a button

1. Drag a `Button` onto the main window.
    
2. Rename the property - find the properties of the textbox and change the **Name** property
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675891908802/d8bdcf86-1291-4923-8951-63b55aa21c3b.png align="center")
    
3. To change the text shown in the text box see **Content**, and change the property to what you want the textbox to show.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675891929177/d40e51e0-26c2-4c1d-8e6c-c8af2e1ae000.png align="center")
    

### Adding button events

To create an event click the lightning bolt icon in the properties for the button. This will then give you a list of events.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675891960791/e1ee91bb-f680-48d6-afc8-a8493b8c1242.png align="center")

Double-click on the event you want to use and this will auto-generate a method using the name of your object and the event type.

By doing this it will bring you to the `MainWindow.xaml.cs` file with a method already started.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675891978844/c32ca375-a091-4464-9886-af678fac8945.png align="center")

Generally, when using a button a `Click` event is used.

## The `TextBox` control

### Creating a text box

Setting up a textbox is very similar to setting up a button

1. Drag the `TextBox` onto the main window.
    
2. Rename the property - find the properties of the textbox and change the **Name** property
    
3. To change the text shown in the text box see **Content**, and change the property to what you want the textbox to show.
    

### Add events to the text box

To create a method for the text box property you need to set up an event. The simplest way to do this is by clicking on the lightning bolt icon in the properties list. This will then give you a list of events.

Double-click on the event you want to use and this will auto-generate a method using the name of your object and the event type.

By doing this it will bring you to the `MainWindow.xaml.cs` file with a method already started.

In the case of using the text box for user input you would generally use the `GotFocus` event.

## The `TextBlock` control

### Creating a text block

A `TextBlock` is very similar to a `TextBox`. A `TextBox` is used to enter data whereas a `TextBlock` is used to display data.

Generally a `TextBlock` does not have an event but is used in events such as a click event. For example, when a button is clicked text or a number is displayed.

## What I learnt…

My learning from this week involved creating different objects from the **Toolbox** and setting these up. There is great importance in naming your objects so that these are clear when you want to use them for an event or within an event, and so that they also make sense. The double click on the event is a useful shortcut to creating an event method instead of manually writing the event method out.
