Today we are going to be learning about switches in C#!
Switch statements are a way to simplify if statements. Once you get a lot of if statements together the code starts becoming very cumbersome and switches are just a really elegant way of cleaning that up.
Today, We are going to break switches down while creating a sample application!
If you were with us last time, you are probably familiar with this form that we designed previously. This form has a label and a textbox. It also has a picture box that we are going to change the background color of using switch statements. We also have a button to perform calculations.
First, we are going to set the label control up for our new project. We are going to change the label’s text from “age” to “color” and change it’s name to “lblcolor”
The idea of the application is going to be that the user can type in the name of a few different colors and we will update the picture box’s background color to reflect whatever color the user input.
Alright, To do that we’re going to use a switch statement that switches based on the text you type into the textbox. We are going to have a case that’s going to be red and our first build will end there. This is basically just a simple if statement.
Inside the select statement we are going to say
pbResult.BackColor = Color.Red;
This will set the picture box’s background color to red. Now we are going to add a few other colors, such as Blue, Black, and White.
Keep in mind, CSharp is case sensitive, so this has to be a capital “B” for “Black”
pbResult.BackColor = Color.Red;
pbResult.BackColor = Color.Blue;
pbResult.BackColor = Color.Black;
pbResult.BackColor = Color.White;
pbResult.BackColor = Color.Brown;
all right, so once you have recreated the UI and you have the above code in the click event of the button. It is time to run the application and give it a test.
What should happen is, when you type “red” into the textbox and then click the button, it will run the block of code that sets the BackColor to Red. When you type “blue” into the textbox and press the button, it will change the BackColor of the picture box to Blue!
You could create this same logic with an IF statement but it would be a lot longer. The logic here is simply, if text = “red” then set the background color to red, elseif text = “blue” then set the background color to blue, and you would just repeat that logic for every color and use an else in place of default.
I hope this simple explanation of switches in C# helped! I am a hands-on type learner myself, so projects like this is how I prefer to learn things.
If you have any questions please ask in the comments below! Also, if you want me to go over any topics just let me know! I appreciate you guys sticking around and would love to help with topics you have in mind!
Darren here and welcome back to another c-sharp tutorial! So, last time we worked on a hello world tutorial. If you missed it, I highly recommend starting there if you are brand new!
I’m going to assume at this point you know how to make a simple windows form and drag a button on it. Today we are going to be talking about if statements in C#.
alright, so first we are going to make a simple little form that will turn a box green or red based on some conditions. We are going to assume this is for a movie theatre entrance and make 18 the barrier for entry. If the person is below 18, the box will turn red and if they are over 18 the box will be green!
so, we start off with getting a button and placing it on the form. This button is just going to say calculate.
Alright, next we will rename it to button calc. This button is actually going to be what runs our code. (the button’s click event triggers the event handler)
Next, we need to grab a textbox from our toolbox. This textbox will store the movie patreon’s age. After adding this textbox, we need to rename it txtAge.
so after getting the button and textbox set up, we are going to need the actual PictureBox that’s background color will be changed. Drag a PictureBox over from your toolbar and then rename it to pbResult. I’m calling it pbResult because we will be using it to display our results.
The last control we are going to bring over would be a label to make the form look a bit better. Change the text of the label to say “age” and center everything in the middle of the form. It doesn’t have to be the best-looking form ever since this application is just for learning purposes. After that, rename the label to lblAge and that’s it for our controls.
Now that we have our simple little form built out, we need to handle the button’s click event. To do that, you need to go to your button and select it. Now hit f4 to open the properties window on the right. Once you are there at the top right, there will be a lightning bolt symbol that once pressed switches from properties to events.
After clicking on that button and getting to the events view, you should see the “Click” event in the list. Double click on the click events row and Visual Studio will take you to the code window. It will have also written out a click event handler and place you inside of the new function it created.
Once you are inside of the newly created function, it is time to write a simple IF statement. The new if statement should say “if textbox text is greater than 18 then make the background color of the PictureBox green.”
You will most likely see a squiggly line. If you see it or not will be based on your current Visual Studio settings. Visual Studio is showing this warning because the text inside the textbox is actually a string instead of an integer. Strings can hold letters and numbers, so the compiler is worried that the user could cause an error.
The way to stop the warning would be to wrap int.parse() around our string to convert it into an integer(number).
So the resulting code would look like this.
if (int.Parse(txtAge.text) > 18)
pbResult.BackColor = Color.Green;
Ok, now we can run the program and input an age. So first we put in 20 to test and the PictureBox should turn green. After that, stop the application and re-start it.
If you put in 10 this time, the PictureBox won’t turn green. Nothing will happen actually since our if statement’s condition wasn’t true. The value has to be more than 18 for our code to run.
You can put in 19 to see it turn green again before closing it. Now let’s beef the application up a bit. This time we want to include people that are 18 and not just over 18. To do that, we will need a greater than or equal to sign, which is what the operator I used below is.
we can come back and we can say it’s 18 that’s greater than or equal to 18.
So, it turns green but the problem is that if we say 18 and then we change it to 12 it stays green because we don’t have any code setting the color to red.
So, what we can do is drop-down below the if statement and add an else.
This else is actually a part of the if statement. So we want to say “else” set the background color to red instead of green.
Once you add the new else, your if statement now reads “If the text boxes text is greater than or equal to 18 set the PictureBox background color to green otherwise set it to the color red.”
Alright, now run the application again with the else in place. Now we can input “19” and get green, then once we change the text to 10 and click the button, we get a red background!
okay, so next we are going to add in an else if which will be our last thing for today.
While an else is a catch-all for anything that doesn’t satisfy the if statement, else-if is similar but has a specific condition check as well. This allows you to say something like: “if the first thing isn’t true BUT this other specific thing IS true then do this instead.”
So, with the else-if in our example, we are going to say if the age is 17 then we set the background color to blue. Our logic is that the worker needs to make sure the kid looks mature enough or something.
anyways, to do this we are going to say if it’s greater than or equal to 18 it needs to be green else-if they’re 17, set it to blue, otherwise set it to red.
Now we run this and test it by inputting 20. This should set the picture box to green, if we change the text to 17, the picture box should turn blue. Lastly, if we set the number to 16, the picture box should turn red.
So, that is it really! That about sums up if statements in C#!
They are a really nice construct and you are going to use this over and over again.
The cool thing is that once you get a handle on If Statements and learn how to put a simple form together, you will be able to make so many cool things!
That’s it for now,
I really hope you guys learned something from this and join me in the next one!
hey everyone! Darren here and today we’re going to be learning c sharp. We are going to kick this off by creating a hello world application!
This is going to be a simple little application where we have a label that says some text and we’re going to click on a button to change what that text says.
We may change some styles of the label and a couple of other things but this will be great to get you started learning software development!
Hopefully, this will help you get started. This is not going to be the most complicated application ever but let’s get right into it!
Okay, so first we are going to choose windows forms application c-sharp for windows.
We are going to click next and then we are going to call this project “hello world”.
It is going to save in our repos folder, which is where visual studio 2019 saves all of it’s new projects by default. We are also going to use the latest version of the.net framework we have installed. After that we click create and then it’s going to open a new project with our new form.
Just for fun i’m just going to change the text property on our form so i have this whole form selected and i choose the text property and just change the text to “hello world” and it’ll change the text up here.
Next, I’m going to go ahead and run this. When it’s running you can see that the text up top now says is “hello world” in the title. Visual Studio generates a regular form that looks just like every other application, you can drag it around, minimize and maximize it.
That all works but it’s simply a blank form for now.
Next, we are going to add some text to that form. The control to add text in C# would be the label control. So we are going to drag over a label here.
Next, I’m going to bring up the properties window for that label and you can change the text of it right here. I’m gonna change the text to “test”. I’m also going to rename the label to a test name. The name property is what you would reference this label by in code.
Alright, so now that our label has some new custom text and it has a new name. We are going to make our label bigger so we can see it better.
we will start by making it 48 point to make it super big! Something else cool you can do is: go up here to format and center the label in the form horizontally. After this our label is ready to go.
Next, we are going to start our project and now we have this label in the middle of our form that simply says “Test”.
Alright, so now we want to add a button because we’re going to actually make some stuff happen. So we head over to our toolbox on the left and drag this button over to our form.
Let’s head over to Format and center this button horizontally. I’m also going to change the text of the button just to say “click me!” using the text property.
Next, let’s name it “BtnClickMe”, so now we have a button called button click me and we have our test label ready to go.
With any of these controls that are on a form, they’re going to have events that you can access from the properties window. So if you come over here to events, there is a click event for this button and this will allow you to execute custom code when this button is clicked on.
If you select over here on the right side of the click event in the properties window and double click, visual studio will actually generate the click event handler for you.
Once you have the method generated in code, click inside of it and we are going to write one line of code that will change the text of our label.
lblTest.Text = "Hello World!";
First, we call it our lblTest and lblTest has a Text property. The actual text of this label is stored inside of this Text property. We set this text property equal to the text “Hello World!”.
“Hello World!” has to be in quotes because that is how you tell C# it is a string. The last thing is the semi-colon on the end, which is customary in any C-Syntax based language.
The resulting function looks like this:
This will get executed when the button is clicked because this is the click event handler of that button.
So, next, we head back to the designer where we can hit start and run the program.
So, now we’ve got our little label that says “Test” and then when we click our button, the label changes to “Hello World!”. It’s really that easy to get events firing and change simple things on the form.
For fun, we are going to go ahead and change the font color of the label when the button gets clicked, so you kind of get a better idea of what’s going on.
So, to do that, our label actually has a forcolor property that controls the foreground color. We are going to set that to Color.Red to set the foreground color of the label to Red.
LblTest.ForeColor = Color.Red;
Color.Red is just the enumerator for the color red. This literally just means red. so, we run the application again and click our button. now it says “Hello World!” and our text is now red.
alright, so to keep this short and simple, we are going to end off there. I hope you were able to set the color to red on your test form and get the text to say Hello world.
After you get this going, you can definitely play around with other options and for example, set the label’s back color to black.
There are lots and lots of properties you can change and all kinds of other details about the label control you can edit on the fly.
I also want to point out that there are tons and tons of other controls out there. The possibilities are nearly endless. I really recommend just creating a new project and playing around with all of these different controls. Just start learning everything you can!
Alright, that about wraps it up for this one. I really hope that you learned something from this. I know this isn’t the most complicated project every but we will definitely get into more complicated projects going forward.
Well, thanks again and I’ll see you in the next one!