Welcome back.
But now let's look at the rest of the code.
So what we check here is the text not empty and well, then we display.
Please enter a text.
Then we check.
Is the selected value empty or is the index zero?
Which means that is it.
This select here is our combo box still at select, which means we didn't select anything.
If that is the case, then display please select currency form.
And then we also say or we directly focus on the from currency so the user knows where he needs to select
something.
So this is a very neat little way to make the application just a little more user friendly and easier
to use.
Then we have another else if block here which says if the selected value for our combo box to currency
is null or the index is still zero, which means it's still at select here, then in.
Make sure to display this text box here with your message box, which says, Please select currency
two.
All right.
And then we check is the CNB from currency?
A text and two currency texts.
So the combo box texts, are they the same?
If they are the same, well, then it's pretty simple because basically there's nothing to convert,
right?
It will still be the same value.
But the currency, which is our label currency content, will be set to combo box to currency text,
empty space, and then the converted value to string.
And three.
So.
And three means it is used to place three zeros after the dot.
So we want to have a precision of three values after the decimal point, and then we have this else
block.
So we took the converted value where we say parse.
Whatever is the result of this calculation here.
So let me put that into multiple lines so it's a little more readable here.
So like this.
So what we're saying is take the selected value that the user used to string from our combo box from
currency.
Multiply that with the text currency text.
So whatever the user entered in the text field.
So this maybe it's easier from here actually.
So.
Take this text from here.
Take the currency from here.
Take the value from the currency that the user used and take the value from the currency that the user
used for the two and then do the calculation.
So the calculation is the from currency multiplied by the text divided by the two currency.
And of course we need to convert it into a string and then make sure that we parse it into a double.
And then we do all of the calculations in doubles and convert it.
Value, as you might recall, is this double variable that we created here, which was empty at that
point, but then we assign a value to it.
And then finally we use the LBL currency and we set the content to be whatever we just calculated.
Okay, so we say combo box to currency text, empty space, and then the converted value should be M3.
So show the label converted currency and the converted currency name, because we also want to see basically
what the currency is.
That it is converted to.
Now, if we run this and I convert 25 USD to euros, you can see that this actually works already.
Okay, so 25 USD are €22 at the moment of recording, even though it's fluctuating, of course.
Okay.
So using static values for such an application is not a good idea.
You would always want to have the data from the web with the latest currency values.
Okay.
But that's something that we're going to look at, as I said in one of the other videos.
Okay.
So now we just need to make sure that whatever is entered here is actually a number and that this clear
button works.
Right now, the only thing that it does is it clears this text here, this label here at the top to
be empty.
But let's extend that a little bit.
So what I want to happen when the clear button is clicked, I want to clear controls and I'm going to
create an extra method for this, which I'm going to call clear controls.
So what it does is it sets back the text currency and I need to stop the application running.
So I will set back the text currency text to an empty string, and then I check if the currency items
count is greater than zero.
Then I set the selected index to zero.
Why do I do this if statement?
Well, if for some reason it is empty, then I don't want to run this code because that would crash
the application.
So this combo box from currency, if the data source or the item source is empty, which it luckily
isn't because we defined it here and we call this bind currency directly in the main window when the
application starts.
So it shouldn't create an error in our case.
But for some reason if we forgot to give it a data table as the item source, then that could cause
problems.
That's why we make sure that the program doesn't crash in such cases.
That's what these if statements are for.
So we do the same thing here.
If items count is greater zero, then set the selected index to be zero.
And then we reset the currency content to be an empty string as well as we set the focus to be on the
text currency.
So now we can enter the text that we want to.
Half is a value.
Okay.
So this is clear controls and we want to run this clear controls whenever we run clear click.
Now you could of course say, okay, I could have just used this code and put it into clear click alternatively.
But what if I want to reuse this clear control somewhere else?
Well then it would be nice to have a function for it.
And that's why I created this extra function.
Okay.
And now how do I double check if the value that the user has entered is actually going to be the right
value?
So that's what this number validation text box method is for.
Well, there is this regex thing, so you need to make sure that you import regular expressions here
using system.text dot regular expressions.
So let me go all the way up here.
You can see we need this namespace for this to work.
And then I'm using a regular expression here.
If you want to know more about regular expressions, check out my regular Expressions video.
And yeah, you can just also read about regular expressions.
But basically what I say is I only want to accept numbers.
So numbers between 0 and 9 or digits.
Basically I can enter numbers into that field and then I say E, which is this text composition event
arguments dot handled should be regex is match e dot text.
So we get the text from E, then we match it against our regular expression that we have created here.
And then we set it back for e dot handled, which means we only accept numbers now.
Okay.
So this number validation text, we used it inside of our window or main window examle.
Let's look at it.
So number validation text.
Where was it?
Actually we can use it in our text amount or text box here.
So you see we use this property called preview text input, which is super cool, very powerful.
And we just use this event number validation text box, which basically previews the text and only allows
certain entries, which is very powerful because if you want to make sure that the user cannot enter.
Alpha numeric values, but just numbers.
So if I try to enter any character here except for numbers, it doesn't accept them anymore.
Even commas are not allowed.
So I would potentially need to change my regular expression to also allow commas or dots or points for
hex, for decimal values for here, for example.
Well, I could do that, but I'm going to limit it.
Of course, you could also make sure that you limit the amount of entries to the double length that
is accepted.
So if it's a too long variable, this could also crash our program.
So that's something that I didn't take into consideration.
So let's crash our program with a way too big number.
Well, actually, it can handle it well.
So you can see it didn't crash, but it didn't take the whole length here.
So at one point it stopped.
So that's the length that the double can handle.
Longer numbers are not really handled well by doubles, but you can see that's just what is generated
for us.
Now let's click the clear button and you can see it resets everything.
So now our application is done.
And it took us just a little more than an hour to create this and even explain everything.
So now you have this currency converter and it looks pretty nice, I'd say.
You have rounded corners, you have rounded buttons, you have even a gradient in there.
You use images, you have labels and a layout, which is fine.
Now, of course, it is not perfect and you can see that very quickly once you resize the application.
So you can see it really screws up the application.
If I change the size of the application now, you can stop that by of course.
Fixing the min size, for example, you could say the window should never be smaller than size X or
Y.
So here, for example, you could say min height should be.
Let's say 400 and min width should be, let's say 1000.
Okay.
So you can of course limit your application to have these values.
And as it seems, this creates an error.
Let me run it again.
That's because I.
Broke it.
Okay, now let's try it again.
And now I cannot make it.
Oh yeah, you can see more narrow than this value and the height.
I can go to 400, but I cannot do less.
So I can get rid of my logo, which I don't like.
I want my logo to be in there.
Well, yeah, but that's how you can limit the resize of a window, which is a good idea sometimes.
But of course we could optimize this application to adjust accordingly.
But I think it's fine this way.