Learning WPF: Using the Toolbox

I am a recent graduate at the beginning of my software development career. I enjoy documenting my learnings through my blogs
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.

The Button control
Creating a button
Drag a
Buttononto the main window.Rename the property - find the properties of the textbox and change the Name property

To change the text shown in the text box see Content, and change the property to what you want the textbox to show.

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.

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.

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
Drag the
TextBoxonto the main window.Rename the property - find the properties of the textbox and change the Name property
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.




