Join Anton Delsink for an in-depth discussion in this video Windows Forms controls, part of Object Oriented Programming with C#.
- In this video we will create a Windows forms application without the help from the visual designer that we typically use to layout our forms. This will help us understand from the basics which aspects of object oriented design we are actually using. Opening visual studio, we will select file, new project. And in this case we will create a Windows desktop type application. However we will create a console application first.
I will call my project Windows forms basics and I'll name the solution Windows, for Windows in general. Select the location for your solution and then click okay. Now I selected a Windows console application so that we see the console pop up and so that it's not hidden from us how we actually make this into a Windows application. The first thing to note is from the project itself, right click properties.
Here we see the assembly name, the name space, the framework, .NET framework 4.7 in this case, and the output type console application. Now a console application and a Windows application are essentially the same thing. The most significant difference visually is that for a console application the Windows console will actually show up, will visually appear, whereas for the Windows application, there will be no console. But in both cases we're running .NET code and we can bring in the libraries that we need for the various visual effects we would like to create.
And so I will leave this as a console application for now. Close the tab. And I will go to my project again and right click and add a reference. Now I'm going to add a reference to the Windows forms library. So we click on assembly and then search. Search for form and go find system.windows.forms and check the box. Click okay.
And so now we will create the worlds simplest Windows forms application. We'll say variable equals new form and I pressed control and alt. We want to import the system.windows.forms namespace. So we say using Windows forms, close the parenthesis. There we have an object of type form. And that can show up as Windows form. So let's make the form show itself. And then there's a trap. At this point the application will exit and so that will force the form to close.
And so we have a special case, something called application.run, that will continue to process input from the user so that the form can remain active. Let's see it in action. We go to the button start at the top or press F5. And you can see the console application shows up and the one and only thing it does is it creates a Windows form. Notice the form is completely empty, there's nothing on the form and the form does almost nothing. However, even an empty form has basic behaviors, like minimize, maximize, and close.
Notice that the console application does not go away when we close the form. That's part of the behavior of application.run which keeps the application active and continue to process input. I'll close the console and then I'll need the application exit. And so this is truly the simplest Windows forms application ever created. But you can see there is a class, we instantiate an object, FRM in this case, and we call a method on that class and all of the work being done in the background is encapsulated within the class form.
We don't need to know how it works, we don't need to know how to do any of those tasks, but FRM, the object, exposes to us methods, properties, and events that allows us to interact with this object. Through those interactions we can manipulate the visual effects as well as the behavior that the user is able to interact with.
- Abstract classes
- Processing text with StreamReader
- IEnumerable and yield return
- Windows Forms controls
- Windows Forms with and without the designer
- Adding, organizing, and testing forms
- Creating classes