In this video we're going to be looking at designing additional forms in our current application.
Now the reality of the situation is that we're writing an application for our car into a business.
So far we have one form and then we just bring it up quickly and this form is really just collecting
data that goes towards creating our rental record over I'm sure if you sit down and think about it there's
much more to a car into a business than just you know logging the fact that somebody the name the cost
the day is returned and rented that type of car and clicking So I mean there's more to the business
than that there are other things that need to get done.
No the problem is that you don't want to try and do everything on one form because this is one form
we are using this one form to call it our car into the record.
So windfarms allows us to build multiple forms on each form can have its own functionality and pretty
much they're all autonomous meaning they're all able to act on their own so in this video we're just
going to explore what goes into creating our brand new form and how we would go about launching it from
another form.
So back in our project we're going to right click on our project file in our solution explorer.
Now remember we have the solution and then we have this project right.
So I'm right click on the project and then I'm going to click add and then I can see a new item Mark
and see a form either way I'm going to get the same dialog box but ultimately I'm going for to farm
someone to start off with new item and then in new item I can look for a form.
But then I can go to the subcategories to the left and see Windows Forms and then what I'm going to
do is select the form templates which is a blank.
Windows forms and forms form rides it so seeing it gets a blank form a blank template.
It's also asking for a name.
So our very first form that we would have started the project with what's called form one that doesn't
really tell me what's going to be on that form does it.
So if I have up to 10 different forms doing 10 different things I do on form one form to form three
I treat naming my forms the same way I treats naming my controls.
So I'm going to build this new form and I'm going to call it its mean window and that's when I explain
why I'm calling it mean window because I'm actually going to be using this new form for something very
special.
So we call it mean window and then we click add and then a low visual studio to create that new form
and give us this brand new canvas.
It looks just like all form one look before we started dragging over new controls and such.
Right.
No the fact that we named it I mean we know it comes deep by default it's with the word meaning to open
the title bar.
You'll also notice that the new file is called mean window and every other file associated with it is
also called mean window.
Now I'm just going to add a quick label to it so I'm just going to go back to my form one design and
I'm just going to copy this label.
I'm just bored in this label putting it on the main window and then I wanted to change the takes that's
in it so I just click it go to properties and then I'm just going to change it takes to see me in windows
so that when it launches I know exactly where I am.
So main window.
Sorry main window.
There we go.
And also I don't want mean window to just be printed like that's in Camilla case in the in the title
bar maybe I want something else I'm just going to click the form itself right click go to properties
and then I'm going to change a text that is being displayed up there so I'm just going to see mean space
window press enter and then that looks a bit better on the eyes right now.
Let's see how all of this mean window look.
So when I click start it is still going to launch form one.
No this is form one we've been working with form one No I have a new form and I would like to see this
new form on the window however I don't necessarily know haul it to get to that form so I need to put
something in place that allows me to launch this form when I need it.
Right.
So copying a lot of debug mode just clicking stop what I'm going to do on Form One is bringing over
another button so I'm going to show you how you can go about launching another form from what from one
form launching another form.
So we have form one it's always going to come up with form one by default.
But I want that when I click this button I see another form and in this case I have main window as my
next form or my next window.
That can be displayed right.
So I'm just going to go to tool box quickly go to my controls get a button and I'm just putting a random
button there.
Nothing spectacular.
This is just purely experimental.
It's only temporary but I just wanted to get the point across that you can launch a window from another
window so I'm just going to change one or two properties on this button and I want to see a launch mean
window launch mean window.
That's the text in this button.
All right.
So this button is simply supposed to launch the main window right.
No.
It would have to do that on click.
So that means if I'm going to expect the button to do something when it is clicked then I need a click
event.
Right.
So if you finish that sentence just no kudos to you.
So I right click on my button and go back to properties click the lightning bolt and double click in
the click area.
So there are two ways though you can just double click the button that will automatically generates
a click event or you could do all of what I just did by going to properties getting the lightning bolt
and then creating the Click event.
As I said before always try to be very deliberate.
All right.
No I have my button I noticed that button one click here and then I have button on underscore click
on the square one.
So that's where the conflicting names come in.
Because I didn't rename this button and this button is also a button one I know it's two buttons with
similar names with similar click events.
So once again it is best if you rename your controls.
All right.
So I have the button click event for my new button and then I'm just going to write two lines of code
that are going to tell me or tell the button that when you are clicked launch this new window or launch
this new form.
Right.
So I'm going to see a main window so remember that everything in C sharp is a class.
So form one is a class.
If we look at it form one is a class that is inheriting from a base class called farm right.
If I go into mean window it also has its own code files so I can right click me in windows go to view
code and then I'm going to see that main window is also a class of itself.
It's called mean window because that's what we call the file.
And it is also inheriting from the base class form.
All right.
So what I need to do to launch an instance of I mean window is to declare an object of type.
Mean window.
So this is me declaring the object and initializing it's a new mean window.
Right.
So remember in C sharp this is so we initialize an object of a class and then I am going to call a function
inside this object called Show so similar to how we show our message boxes are always said Mrs. box
not sure.
Right.
And then that's what pops up the message box of the screen.
Well all I'm saying here is that I want an instance of mean window and then I want to show this window
once it's created and then we discuss using VARs I could just say a var there instead of seeing mean
window.
The object is equal to right.
So you have the two ways you can write it whichever one you feel more comfortable with.
That's fine.
So no the expectation is that when I click this new button this experimental button in this car into
a system window it will launch window for the mean window or launch of the main window form.
So let's try.
So let's start.
All right.
So I have my car in so form up and then I click launch main window and there we go.
All right.
So when I click launch main window it does appear it just a prism and other monitor noticed that each
time I click it is going to pop up this mean window so you can bake in some intelligence at some point
to see if any instance of it is already open then don't open anymore.
But for now we see that we can launch another window anytime we want.
Once we have the proper code in place now that's pretty much it for how we go about launching an additional
form from an existing form.
So most since we close all the windows our agendas press stop the exit debug mode altogether I'm going
to do one more thing in keeping with the best practices that I'm trying to promote in my videos so far
and that is to rename form one from just form one that's Yes because yes it was the first form yes it
was the first thing that was there.
So by default it's form 1.
However when we start building in other forms you don't want to have to look at the systems and explore
and guess that form one is the car into a window the same way we can just look at this and deduce that
mean we know represents a mean window isn't saying we should we should be able to look at the file name
for the car and I know that it is the car into the record window.
So I'm going to rename this form and by renaming this form visual studio is going to allow me to change
out every reference to this farm one class through all of my solution.
So the new name name I can read the file and see read name and then I'm going to call it for record
or let's be more specific.
I'd bring into a record right.
So remember that this window is all about allowing us to insert our entire record so I'd entire record.
Press enter then visual studio is asking me or telling me I'm renaming a file.
No it's asking me would I like to rename all references to form one in this entire file and I'm going
to say yes I do want to do that.
And notice how a visual studio does reflect does all the references for me.
So that when I build control shift and b there are no arrows in my code.
Also the bigger advantage is that when I look in my solution Explorer I can directly or immediately
detect all the forms and what they are for.
All right.
So the last thing that I'm going to do is look in my program not see us file no in our program not see
us file we have all the code that runs as at the time the program is started.
So when I click start what really happens is that visual studio or what the engine looks still programmed
us this file which executes this main function so if you were doing the console applications and you
would be familiar with the fact that there is a main function at all times.
Right.
And then what it does is initialize some visuals as some takes rendering stuff.
I don't know I'm not going to sit there and teach all these two lines do I would just advise you that
if you're going to move them be sure you know what you're doing.
Otherwise you can leave them alone.
But the one that you can modify is application run where it is running and starting a new instance of
well we just change the name from form one to add our entire records.
So what happens is that when you click Start it does this line.
Does this line and then it runs a new window of order into our record.
Which is why that's the first form that comes up.
So now that we have a main window form that we want to make the landing window for our entire application
we can change this from our entire record to mean window.
Now what's going to happen is that when the program runs it's going to launch the mean window so let's
try that.
So I click starts and what comes up first is the mean window.
All right.
So no the next steps that we have is to transform our entire application into what we call a multi document
interface or an MDI application where our main window with the host menu strip that's allows us to go
between all the features and the respective forms that will be needed to support a functionality for
this application.
Other functionality would include maybe like an archive of all our entire records.
All right.
We're only adding but we don't view them on lists we go to the database.
You don't want to send people to a database.
You want people to be able to view them in the application.
Likewise we need to be able to manage the list of cars.
And there are other things that we maybe want to do in our car into application that we cannot do right
now.
So our main we know will now be the landing window or the first window that launches to show us all
of four options thereafter.
And then by clicking each option we would launch the respective form for that feature.