Welcome back.
In this video we're going to be doing some essential fixes to our application so we've made good progress
so far but there are certain things are boats the application that can be changed I can be you know
refined and just list a few of them.
Well we want to do is make sure that only one window at a time comes up meaning when I in my application
say that I want to add a car.
Let me just log in quickly so we can see exactly what I'm talking about when I say I want to launch
a managed vehicle listing I don't want to be able to click that and have so many instances of the same
window open.
So we're going to be modifying or application so that only one window comes up at a time.
That's one.
And another thing that we will want to do is to do our grid auto refresh so we didn't get to do that
but we want to do that where if I add a new car right.
No I don't have to come back and click refresh but the grid should automatically refresh and show me
the new car outside of that.
Anything else would just be refining the messages and the visual cues that we give to our users with
each operation that they complete.
And then we will upload our application to GitHub.
So let's get started.
I'm going to exit the application and the first thing that we're going to tackle is opening one window
at a time.
No they're for too much two ways to do this one.
While one will help us maintain the MDI child the pattern that we have been using where when a window
comes up as an MDI child they cannot be moved outside of the main window.
And the other one is as short as that dialogue where that window will appear.
Nothing else underneath that new window will be active until that new window is exited but then that's
at the expense of it being an MBA child where it will sit on top and explain exactly what I mean.
So let's start off with the manager equally still rather let us go to the main window and then I will
look in one of these I have this option here.
This button click event where I said I'd rent out the record and add another one for the managed vehicle
listing fired.
So I am going to modify the order into the record button click event to show a dialog instead.
Now the problem with short dialogue is that I can't sit the appear and and then show a dialogue.
You'll get an error part so you can show an MDI child.
So after sitting in that appearance it becomes a dead child.
And then I can show the window as I named my child.
However I would have to omit or at least rearrange this line of code.
So I'd have to put Shaw above the MDI Parent Line and I would have to say sure dialogue.
So wanted shows dialogue pretty much that show MBA that MBA child sitting in their parents sitting gets
nullified it's irrelevant at that point.
But let's see what happens when a short dialogue.
So let me start.
All right.
So we modified the man at her entire record.
And so when I click monitor into records that's a click event.
Right.
And I.
Oh sorry.
It's this event at her entire record when I select that notice that it is not an immediate child.
This window launched well outside of the main window.
However I can't do anything outside of it while it is on the screen and you can see it blinking that
each time it blinks that's me clicking something instead of me in windows I got over me in window I
don't interact with anything and as a result only this window will remain active until I exit it and
then I can proceed with anything else and application.
So that's one very simple way of allowing are ensuring that only one instance of the windows open at
any given time.
Like I said though as you can see the window operates independently off the main window which may not
necessarily be a bad thing depending on your objectives.
So I mean in this situation it's not a bad thing.
I can move it are owned and I can't do anything else until I close it.
Right.
So when I close it then it's business as usual.
So that's not necessarily the worst thing that can happen really.
So that is how you can go both ensuring one instance off the window is up.
So if you want you could change out all of these shows to show dialogue.
Once again though it has to happen before the MDI period line or else you get an error.
So you could actually just remove this line where you set the MDA appearance if you're going to be showing
the dialog.
However I'm going to show you another method using the managed vehicle listing menu click event.
So this one no I'm actually it's going to be far more lines of code and a little more goes into the
thought process.
But the fact is that I will retain the fact that this window has an MDI period.
All right.
So for the other window which is monetary closings or move some code I wrote the code already.
So let me just walk you through it.
So what's going to happen is that we're going to be checking all of the open forms in the application.
So on button click right.
When manage VK listing when that button is clicked and we're able to show we're going to initialize
we're going to set that parent.
But then I'm going to also check to see if there is any open form inside the application as I want of
a fact describe it a bit I do all of that first and then if there is none then I initialize a new object
and then sure the window.
So let's do it that way.
All right.
So the first thing I want to do and then you'll see if you research it you'll see a number of ways of
doing it so be be used for each loop.
Some people use different methods but the concept remains the same.
I'm actually going to use the link because link allows us to iterate through any list or collection
just like if for using a for loop or a for each loop.
So a link is not necessarily unique to only database queries but if you have a list that you want to
interrogate just as if it was a list of requests going from the database side list of objects you can
do the same thing using the link anyway.
Right.
So the first thing I want to do is declare some variables I'm going to say var open forms.
All right.
And then what I want to store in here is the number of or all the open forms in the application so I
say application dot and then I can use open forms.
All right.
So this is giving me a little collection all of the forms owned by the applications that application
is what comes up or what starts to run on we click start.
Right.
And then all of the forms are the open forms would be any window that is open at the time off it's running.
All right.
So I say application that open forms but then I want to do and I kind of list of forms.
So while it's open forms as you can see it's a different data type.
So I want to see a dot and then cast and I'm really casting it to the data type of a form.
So I'm basically saying give me a list of the open forms in the data type form which is what I know.
All right so remember that everything is off the base class farm.
So I want all of them at the base class farm.
So it will handle that type casting for me and then give me a list of the open forms of all of the data
type farm.
Now after I get that list off the open forms I'm going to declare another variable where I want to say
is open so var is open there.
So this is really going to be a boolean rights I'm saying via.
But it's really going to be a boolean and I I'm telling you this because of how I named it is open.
So usually it was a Boolean if you have noticed before you can you know take note of it know that whenever
I declare a boolean I say is this or is that right.
And that's a common pattern in programming.
So I would just say VAR is open and then I'm going to say Give me the list of forms so I say open forms
and then say dot no when I say that if you just look through this list you'll see that all of the options
that we had in the link are here and available at first are defaults.
You have the weird of the selectee of all of them.
The one I'm interested in this time is any.
Right.
So any of those to stipulate a condition.
Is there anything that has this condition and it will give you a true or false.
All right.
So I can see any and then blow up my land.
Expression.
So cue arrow and then cue dot.
So I'm checking if there is any farm.
So if I say kudos to you.
Notice that all of these just the options we see when we are creating a new instance of a farm.
Right.
It's the same thing.
So would you say if there is any farm and then I can do this too is I can say by text or by name so
I'll just stick to a name.
So if there is any form with then name right.
That much is or is equivalent to the name of whatever I'm able to bring up so remember that the name
of the farm is pretty much the class name right.
So I can say is there any form with the name manage vehicle listing because I'm a to launch manage vehicle
listing so I want to know if there is any farm that is open that has the name of the farm I'm about
to launch right.
And then if it is not.
Or if not is open.
I remember that this really translates to if is open is equivalent to falls.
So if it is not open already then I will do all of this.
All right.
So this one has a bit more lines of code but once again the point is that we're just checking to see
if there is any open farm currently with the same name as the one I want to launch.
And if there isn't then I can go ahead and lodge it.
I don't need an ETS because it's either true or false so if it's false then would launch it if it's
true then.
Well we skipped over everything and nothing happens.
Right.
So let's try that one.
All right.
So I'm about to launch manage vehicle listing that's the click event that we just modified.
So when I click manage vehicle listing it will launch the window.
Notice it's the launching as an MBA child.
I didn't change anything about the way it launched.
However if I click the button again and I'm clicking right.
No I see the most is moving and I'm clicking you'll only see one instance coming up.
That's because it's is checking all the open farms and seeing that there is an open forum with that
name already so nothing should happen if I close it and then click it again then it will work and then
ever subsequent click does nothing.
So we see that that is one way that we can retain that MDI child behavior but ensure that only one window
comes up at a time.
So you can use either method.
Like I said the short dialogue doesn't allow for the MDA child but it does give the farm that is launching
kind of the autonomy of not being a child but then based on your application flow under your expectations
then you can use either one that you wish.
No.
Moving on we want to start looking at our grid refresh know what we want to do is ensure that after
we do that that I'd edit operation or add delete operation that our grid refreshes automatically.
So what happens right now is that when you add or edit a record it will you know say the success message
but then you still have to go back to your the manage list and then you just launch applications so
we can see.
All right.
So like I was saying we have to when we launch manage vehicle listing.
If I add a new car right then I would have to come back here and then refresh manually for me to see
the changes.
So I want to modify that after the car and I click submit and it has been saved to the database that
the grid refreshes and I don't I will actually be able to delete and remove or remove the need for this
refresh button.
So no we'll have to actually get creative just like we had to kind of get creative with the log in form
where when we were launching our main window remember that we had to modify it to put in an object off
the log in form into our constructor so that we could manipulate the properties of the log in form when
we wanted or which in this case was to close it.
So what I'm going to have to do is whatever window is launching or manage windows so manage vehicle
listing I'm going to have to pass an object off this into the ad edit window.
All right.
Or even if it's the edit whichever one but once I'm caught in that window I need to pass an object of
this into that phone showing called into that constructor call.
Right.
And then from this window when it launches I'll be able to you know execute with a populate grid function
call.
So I have a function called populate grid which runs of course runs the query sorry.
To actually populate its own grid.
And right now it's private.
So we'll just do the out and then we'll seal all of the changes someone to do it with a manager eco
listing and then I'm going to challenge you to get it done with the current records window also.
All right.
So let's get started.
So the first thing that I want to do is modify my constructors and add edit vehicle so I'd edit vehicle
ahead clean constructor and the constructor that took an object off the car.
Now what I'm going to have to do is modify this so that I have one my private property off type manage
vehicle listing so I'm making this global because I don't know when I'm going to need it.
So after make it global just like I had to do with a log in and everything else that's up here and then
I'm going to call it manage vehicle listing and then I'm going to tell this constructor.
So just copy this quickly that it should take this concept to that had no parameters initially it should
take parameter off type money vehicle listing on this what I want to say it does underscore manage vehicle
listing but then the prob the parameter won't have the underscore.
All right.
So either constructor is supposed to take an object of manage vehicle listing.
Now I can modify this parameter and say something like equals NULL which would then say that you may
or may not need to pass of value in.
All right.
So to make it not a requirement I can see equals.
No.
So if you have to call this constructor but you don't want to pass in that window or this object then
this would kind of help to ease the frustration that would happen at that point right.
So I can do that for both of these parameters and C equals no just in case the person wants to call
the ad or edit constructor without passing in the manage vehicle.
Sorry I said use of what's it's really you the programmer I'm catering to this point.
All right.
So after we've gotten our parameters we want to initialize our local or our private object that we just
declared or to initialize it to whatever value comes in to manage vehicle listing.
So if it's not it's not if it's not no then we get an object.
Right.
So manage vehicle listing easy on the square miles you go this thing is equal to that.
And I'll just take this line and repeat it below.
All right and so whenever our window launches it will have some knowledge of the Managed vehicle listing
window that called it.
So it's easy enough to declare an object off one inside off their own time but then each time we declare
an object it's a brand new object.
It's not the object that exists.
All right.
So we want to make sure that we're tracking the one that exists are the one that called it because we
know that the one that called it is the one that is open in the background currently showing the grid.
All right.
So I going to know go back.
Sorry.
Go back to manage equally listing and then I'm going to make sure that I just hold onto control and
click and then I can just double click.
So if you have more than want instances it will financially double click.
Which one you want to jump to.
So then my function calls to add edits vehicle or mine edit vehicle.
Yes I noticed that there is no arrow because I did set the parameter to be knowledgeable.
Right.
So because I said it to be knowledgeable and let me just change it our own and just show you exactly
what I mean.
If I did not say that he can be equal to null then manage vehicle listing.
This constructor would then give me a red line because then it saying that it doesn't have a constructor
that takes all arguments it needs and it needs to pass in an argument.
Because I said it can be equal to no let me find it back because I said it can be equal to no.
Then I can proceed to meet the regular function call it if I want to are the regular constructor call
without passing a parameter.
All right.
So that is one of those things that I'm just showing you just in case that you wanted to be more versatile.
Either way I'm going to pass in this.
So this surplus in some object of the manage vehicle listing window that is making the call to add edit
vehicle.
Right.
So I'll put this in for the ad click and also do the same for the edit.
So they see the edit click is the same thing it's passing in car which is our required parameter but
it's not crying all the time all the money.
Vehicle listing.
However I will say comma this all right.
No that that is done.
What will happen is that when we add or edit the vehicle once we complete our operation in this function
call.
All right.
The expectation is that our grid is going to refresh.
So what I'm going to do is before show in the message box and seeing everything is completed I'm going
to put in
call to our objects to underscore manage vehicle listing and then I would call populates grid.
No notice that I can't call it.
It's not coming up in the this sense.
And even if I type it all out it's not calling it.
All right.
So what happens is that it is private.
So it's private over inside of our managed vehicle listing.
Let me see if I can find it quickly.
It's private here.
Second call it from outside.
So if I make it public then that problem gets solved.
So I say public void populate grid and then we'll be able to call it.
So let's review the expectation here.
When I click save on whether it's an ad for operation or an edit operation the expectation is that our
you know we do everything that we know we do we check if it's edit mode and we populate or object accordingly.
So you see that we kind of repeated code and we kind of reflected that the last time sort of a BBC if
change is here.
However BBC if change is here and I really wouldn't want to repeat those things all the time right.
So what I'll do is remove these lines of code so I'm just refactoring right now and then I'm going to
take the same lines of code that were repeated and then put them outside of the check if it is a mode
or not.
All right.
So check if it is valid.
Else if it is edit mode then we want to do this.
If it is not it it more than we want to do that either way at the end of the day we want to see if the
changes populate the grid and then.
Sure analogy.
Just make a generic message operation completed refresh to see changes and then we close.
All right.
So then what should happen is that when we click save on it does either operation.
It's actually just going to populate the grid call the populate grid function in the manage in the manage
vehicle listings window and a room without populate grid is what runs the query since the data source
and all of the operations in between and then that should trigger the refresh off the grid.
So let's that that's.
All right.
So we launch our monitors vehicle listing window I click add new car and then I'm going to see the ME
is high on a sorry high on day i 20.
This is twin to 19 model sorry the VIN is it just puts in something random the year is 20 18 and the
license plate is that we click save changes it says operation is completed refresh to see changes Okay
fine but then look at that.
It's already refreshed for me so I can change a message.
No I don't need to tell them to click refresh second of the refresh button there.
But the point is that the operation would refresh the grid automatically.
All right.
The same for edit.
If I say I wanted to edit this and I wanted to clean up this Vin a bit because I put in the wrong video.
All right.
And then save changes operation completed.
Now you see the changed Vin.
Going in and everything is happening real time right.
So that's all you can do that grid refresh operation.
So I did it with a manage vehicle listing.
Well I didn't do it with the lead car.
Let me make sure I did it with the lead car so I no longer have this car in my fleet.
In fact delete.
You see that nothing happened so delete.
Nothing happens.
No visual cue of anything but if I refresh you'll see that it disappears.
All right.
If I tried to delete again I would have gotten an error.
So what we want to do then is make sure that our delete function tells us that the operation was successful.
And it refreshes at the same time.
You mean you want to give them a yes or no.
Are you sure you want to delete kind of prompt.
So I'm back in the mileage vehicle listing so initially I'd try it.
Ah re fresh using this function but that kind of does something different from what we want to do.
Someone to remove this and then what we're going to do is say 1 if the person clicks the delete button
we get the the details you know we get the object and everything and then what I'm going to do is write
some code and I wrote it quickly so I'll just explain it to you.
So what we're doing is doing a message box that allows us to prompt the user.
Are you sure you want to delete so when you click the delete button.
It's going to retrieve the record is going to retrieve the idea and the record associated with the I.T.
sorry.
And then we're launching a dialog box.
Now remember that the dialog box is a box that comes up that prevents you from interacting with any
other open window right.
So this dialog box our dialog result is the data type and object is D.R..
That's what we're calling it and we're seeing I want a dialog results that has the value off our message
box.
So and then this the message we're showing are you sure you want to delete this record.
You can put in even more details in this if you want because you already retrieved the car object sake
and say Are you sure you want to delete the car name so to speak or the car model car make if you want
and then this is the title of the dialog box which is delete.
Then we see we want to show message box buttons.
Yes no or console so when it comes up it will come up with that.
Yes I know and I cancel button and then we're showing an icon.
That's a warning call we're warning that we're letting them know that they're just entered the danger
zone right.
And then after we've prompted them we're seeing if the result is yes.
Meaning they click the yes button then we want to proceed to remove the car.
All right.
And then having removed the car we can call populate grid all right.
And a spelling error there.
So we can call populates grid and then that should refresh our grid.
So let's try that lead operation.
All right.
So we're back and our goal is to try to delete Toyota.
I don't think Toyota has any rental records associated with it.
I can always check so I can always just go over view the archive and I don't see anything with Toyota.
So I shouldn't have any difficulty deleting Toyota.
I click delete and then it's giving me that dialog box.
Are you sure you want to delete this record.
Yes no maybe so I click.
Yes.
And then boom it removes it and then it refreshes the grid automatically for me.
All right let's try.
Subaru also if I.
No.
Then nothing happens.
Exactly.
Cancel then nothing happens.
But then once I click yes then it is removed.
All right.
So we just cleaned up our application a bit.
We made sure that's only one window opens.
And we also put in some grid refresh capabilities into our windows.
So I did it with managed vehicle listing.
I'm challenging you to try and repeat those steps with the managed rental records either way in the
next video we're going to be checking in our code into source control I would love all the code ready
and available and commented so you can check the repository for the completed code in case you need.