Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Forms have been a part of the Microsoft programming lexicon since the first version of Visual Basic appeared back in 1991. Form is the term used to designate an interactive window. .NET is a replacement for those early com-based systems, but Microsoft continues the notion of form-based development. This framework is called Windows Forms. Windows Forms have been extremely popular in corporate applications, forming the basis of thousands of useful applications. They are easy to develop, and within Visual Studio support a drag-and- drop interface designer.
Despite their popularity, WinForm development is on the wane. WinForm uses GDI as it's rendering engine, which some consider inferior for building modern interfaces. Because of this, Microsoft created a replacement technology for Windows Forms. This new UI/API is called Windows Presentation Foundation, and it is covered in the next movie in this chapter. Even though I'm a diehard WPF fan, I want to show you some of the features of Windows Forms. It is a powerful tool, and is still used by many development shops as their leading Windows client development framework.
I'm inside Visual Studio, and I've opened a solution called CreateWindowsForms. Currently, there are no projects within this solution. I am going to right-click on the solution and choose Add > New Project. For today, I'm going to create a Visual Basic template because it has some interesting Windows Forms templates to use. So I am going to choose Visual Basic, and then Windows Form Application, and then I am going to go ahead and leave at its default name of WindowsApplication1 and click OK.
Here is my main screen of my application. I would like to add a couple of more Windows templates. To do that, I'm going to right-click on my WindowsAplication, choose Add > New Item. If I click on the Windows Forms node here, under Common Items, I can see about 20 different form templates. I am going to use the Splash Screen, and I am going to add one more: the Explorer Form.
As you can see, the Explorer Form has a complex UI in place. It has a menu, a toolbar, a split screen and many other features. That's all I want to show in that designer. The Splash Screen is designed and pop up when the application starts. To tell Visual Studio to launch this first and then my main form, I need to go to My Project, double-click here, scroll down to the bottom of this Application section, and choose Splash Screen. Choose your appropriate form.
While I'm in this dialog, I'll also make sure that my start up form is correct. I want to start with Form1 as my main screen. Now, I'm ready to test. Debug > Start Debugging. On my machine, it prompts me if I want to save my changes. There is the Splash Screen, and there is our main UI. I'll click the Close button to shut down this form, and then I'll close these two windows here: the WindowsAplication properties and the SplashScreen.
So, for the next couple of minutes, I'm going to creating some user interface for this screen. I am going to start by dragging a few items to the designer. First comes the menu strip. So I am going to go to my Toolbox. I am going to open this All Windows Forms section and find the MenuStrip control and drag that over. Notice that Visual Studio automatically docks that to the top. I'll also find the StatusStrip and drag that and drop it.
That gets docked along the bottom edge of my screen. I need a ListBox. Here it is. I'll drag that over and make it a little bit wider and taller. And then the last item I want to add is something called the PropertyGrid. Now, you've seen the PropertyGrid inside Visual Studio. It lives over here; at least on a Windows Form Application, it does. This control is also usable in your applications. So I am going to find it. Here it is.
I'll drag that over, and the next step I want to take is to dock this PropertyGrid to the right edge of the screen. To do that, I'm going to go to the Visual Studio property grid, scroll down to the Dock property, select it, and choose this dropdown. This represents the edges of the window where I can dock the control. If I click on this button right here, it means to dock to the right edge.
And I'll make it about 50% of the width of the screen. Now, I am ready to write some code. I need some menu items up here, so I am going to click at the top of the screen. I currently have no menu text. There is a very simple way to add a lot of menu items in a hurry in Visual Studio, by going to this little special icon over here, the black triangle, clicking once and then choose Insert Standard Items. See what happened? It added a File menu, an Edit menu, and the one I'm interested in--the Tools menu.
Now, I am going to add my own menu item right here, so I just click once and start typing. Double-click on this one, and it switches me over to the Visual Studio editor, and it has stubbed in this Visual Basic code. Now, what I want to do is go out to a folder in my hard drive. I am going to go to my Desktop folder, and I am going to search for text files, files that end with a TXT extension. Now, I have already created two empty text files on my Desktop.
If you're following along, make sure that you point your example to a folder that contains some files with a TXT extension. I am going to start by getting my desktop folder. We'll declare a variable called location, and then I'm going to go to the .NET engine, and I'm going to query the Environment, using this method called GetFolderPath. And watch what happens when I hit the open parenthesis--Visual Basic suggests this enumerated value.
I am going to be looking in my Desktop folder. So this is saying to .NET, "Somewhere on my hard drive is a Desktop folder. I don't know the exact location of it. Would you look that up and find the path of that and store that here?" Now next, I want to write a link query to talk to that file system and query the data that's here. Now rather than hand type that, I have an Assets folder here that contains a text local link code. I am going to open up this text file, and then I am going to copy this text and paste it into this code window right here. And I see I have this blue squiggle.
This happens occasionally. It means I am missing an Import statement at the top of my code window. That should fix it. So what this says is I'd like to start a query and store the query in this variable. Go out to the hard drive and walk through all of the files in this folder--remember that's my Desktop folder--and then here is the query part. Do a Where clause on that and say I'm only interested in files if they have a TXT extension on them.
Once you've done that query, I want you to select out the information and store it in this data type called the FileInfo. Now FileInfo contains information like when the file was last saved and what the file name is and things like that. So I am going to select that out. On line 12, I am going to execute the query by calling ToList, which returns the results of the query as a list. I am going to assign them to the list box as data source, and then I am going to tell the list box which property on the FileInfo class I'd like to show. I want to show the Name of the file.
The next piece of information comes from the ListBox. As the items are loaded in the ListBox I am going to click on them, and then I want to take the data, the details about those files, and load them into this PropertyGrid. So I need to write an event procedure for the ListBox. So I'll switch back to my designer, I clicked in the ListBox. I am going to go over to this property grid and click on the lightning bolt, and then I am going to find this property called SelectedIndexChanged. That's an event that fires whenever the user selects a new item in the ListBox.
I'll double-click right here on the edge, right in this column here, and Visual Studio will stub in this code on my behalf. If I go to this text file up here, SelectedIndexChanged.txt file, I've got the code I am going to write in there. So I'll copy that over and then paste it in here. What this code says is, "Make sure that the user has selected something in the ListBox. If they've selected something, then wherever it is they selected assign it to the PropertyGrid.
The PropertyGrid then interrogates that object and shows me all the details about whatever that type is." I think I am ready to run the application. Debug > Start Debugging. Click Tools > Show Files. It found two files on my desktop. And hen I click on the file, it loads the information into the PropertyGrid about that item. For instance, you can see the file name is abc.txt, and the last time it was accessed was at 2:15 this afternoon.
There is lot more I can show you with Windows Forms, but that's about all the time I have in this movie. Remember this: Windows Forms are still a viable technology for developing Windows applications, and they are common in many corporate environments.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99963 Viewers
56 Video lessons · 113049 Viewers
71 Video lessons · 81894 Viewers
131 Video lessons · 39299 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.