Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
strauss_d_getting_started_with_visual_studio_2022_learning_a.pdf
Скачиваний:
123
Добавлен:
26.06.2023
Размер:
17.04 Mб
Скачать

Chapter 1 Getting to Know Visual Studio 2022

If you are receiving funny build errors that do not seem to be errors in your code editor, try cleaning your solution and building it again.

Toolbox

When dealing with a UI file such as a web application or a WinForms application, you will notice that you have a Toolbox at your disposal (Figure 1-15).

Figure 1-15.  The WinForms Toolbox in Visual Studio

The Toolbox allows you to add controls to your application such as text boxes, buttons, drop-down lists, and so on. This allows developers to design the UI of the application by dragging and dropping the relevant controls on the design surface.

You can also open the Toolbox by clicking the View menu and selecting the Toolbox menu item. It is worth noting that for some project types, you will not see any items in the Toolbox.

20

Chapter 1 Getting to Know Visual Studio 2022

If you do not like the default layout of the Toolbox, you can right-click the tab or an individual item in the Toolbox and perform one of several actions from the context menu as seen in Figure 1-16.

Figure 1-16.  Toolbox context menu

The context menu allows you to do the following:

•\

Rename an item

•\

Choose additional items

•\

Remove items

•\

Move items up and down

•\

Sort items

•\

Add a new tab

If you have third-party controls installed such as DevExpress or Telerik, you will find the controls specific to the installed components under their own tab in the Toolbox.

21

Chapter 1 Getting to Know Visual Studio 2022

The Code Editor

Let’s add some basic UI components to our WinForms application as seen in Figure 1-17. To this code-behind, we will add some code to our project, just to get the ball rolling.

All that this application will do is take a given waybill number and return some location data for it.

Figure 1-17.  The application design

The location data will be hard-coded in a Location class that was added to the project.

After adding the UI elements to the designer, swing over to the code window for the main WinForms application called Form1.cs. Add the code in Listing 1-1 to the code-behind.

You will notice after adding the code that Visual Studio starts to underline some of the added code as seen in Figure 1-18. This is because Visual Studio is making suggestions to improve the quality of your code.

22

Chapter 1 Getting to Know Visual Studio 2022

Listing 1-1.  The Code-Behind Form1.cs

private void BtnTrack_Click(object sender, EventArgs e)

{

if (!(string.IsNullOrWhiteSpace(txtWaybill.Text)))

{

string waybillNum = txtWaybill.Text; if (WaybillValid())

{

Package package = new Package(waybillNum); Location packLoc = package.TrackPackage(); if (packLoc != null)

{

txtLocationDetails.Text = $"Package location: " + $"{packLoc.LocationName} with coordinates " + $"Long: {packLoc.Long} and " +

$"Lat: {packLoc.Lat}";

}

}

else

MessageBox.Show("You have entered an invalid Waybill number");

}

}

private bool WaybillValid()

{

return txtWaybill.Text.ToLower().Contains("acme-");

}

23

Chapter 1 Getting to Know Visual Studio 2022

The underlined code is code that Visual Studio is making suggestions for improvement on.

Figure 1-18.  Visual Studio code improvement suggestions

To view the details of the suggestion, hover your mouse over one of the underlined lines of code. Visual Studio will now display the details of the suggested change as seen in Figure 1-19.

24

Chapter 1 Getting to Know Visual Studio 2022

Figure 1-19.  Code change suggestion

Here, we can see that Visual Studio is suggesting the use of the var keyword. At the bottom of the code editor, you will also see that Visual Studio displays the count of errors and warnings as seen in Figure 1-20.

Figure 1-20.  Errors and warnings

You are able to navigate between the warnings and errors by clicking the left and right arrows. You can also perform a code cleanup by clicking the little brush icon or by holding down Ctrl+K, Ctrl+E.

After cleaning up the code and adding the code suggestions that Visual Studio suggested, the code looks somewhat different as can be seen in Figure 1-21.

25