@@michaelartsmedia uisng the newest version of visual studio also using c# windows form. For me, on random, icons, firstclicked and secondclicked It says that "A namespace cannot directly contain members such as fields or methods" And because of this the program does not work Do you maybe know how to fix these errors?
@@saltypepper3340if u use WinForms your code should be nested like this : namespace YourProject { class Form1 : Form { public Form1(){initializeCompontets();}}} "initializeComponts" is the method to initalize the controls on your win Form genereted by the designer . public Form is the constructor ,is the first method called when u create an istance of ur class. The class is a model of an object istance in your specific case a "WinForm" the namespace is the place where all ur classes exist u can also join multiple namespaces with this line "using NameOfYourNameSpace;"
thanks mate, great tutorial i had to do this for my school with extra reset, start, stop and exit buttons and this here pretty much made it possible without any problems
Excellent tutorial, however I'm getting an error in the AssignIconsToSquares method. The error occurs on the line that reads: "label.Text = icons[randomnumber];" The error says, "Use of unassigned local variable 'label'. I don't understand why this error occurs. I typed in the same code you showed here. I'm using VS 2019, so I don't know if that makes a difference or not. I don't see how it would, but you never know with Microsoft. Thanks. :)
That error means you're trying to use a variable that hasn't been assigned. A lot of times just assigning the variable to an empty value where you declare it initially will fix this. So instead of ClassName class; ClassName class = null; would fix the error. Try comparing your declaration with what's in the video and look for things like that. Thanks for watching!
@Michael Hicks I'm kind replicating this exercice with visual basic code. I'm having trouble with hiding the images (as I'm using pictureboxes) and creating an event for when the images clicked match. Any tips?
Thank you so much Nice and clear. That was great. step by step. You are a great teacher I have learned all of them. especially "oops!" between codes were great ;)!!!
It tells me error on the label.Text= icons[randomNumber]; It says "index was out of range",Must be non-nevative and less than the size of the collection... Help pleaseee Michael
Hello, tableLayoutPanel.Controls is an empty collection for me? Am I supposed to intialise that collection somewhere, I can't see you do it at any point in your video?
Thanks for watching! You'll need to reinitialize all of the variables and form info to their initial state. That means resetting the draw colors to the invisible color, and also resetting the variables we declare at the top. Good luck!
Nice video! Very well explained I am trying to add a new feature to it. I am trying to connect a trackbar to the game. The trackbar minimum value is 4 and max is 10. If the track bar is value 4 it should be 4*4 if it’s 5 it should be 5*5 but i am not having success.
Thanks for watching! What exactly is the issue? The track bar is supposed to change the grid of the game but it's not somehow? The more details you give the better!
Hi Micheal Hicks. I following the skip but on the line 32 if(firstClicked ! = null && secondClicked ! = null) return; , it is still error. Can i know what happens?
Hi Michael. This is the best tutorial that I've ever seen. I do my memo game with pictureboxes. I did almost everything as you did. But I have List of the pictures, not the labels ... and at the last step when I need to compare 2 "icons" I dont know how to compare them. Could you help me please?
Haha thanks a lot, you're too kind! There is likely an Image variable in the picture box that you can compare. If the images are equal then you know they are matching!
@@Tim_Linkel You should be able to just compare that variable. If not, you can create a string variable that stores the name or path of the image. When you set the image make sure you set this variable appropriately and you can compare the name of the images later on.
Great game and thanks much for teaching the code as well..... hope you still monitor the site because another feature for this would be... should someone guess 3 times and NOT match, then the game penalizes them and resets the entire game, regardless of how far along you are. So if you can, help me with the code that would do this...... i'm real new to c...... Also for me personally, I will play with it and add more cols and rows and make it really tough!!
Thank you! It helped me a lot! But I have one problem, I have no “tablelayout” in toolbox. What should I do? I would love an answer quickly because I need it for a project for this week.
Unfortunately I probably can't be much help with that. Possibly it didn't come with your install of visual studio? You'll probably have better luck googling that... sorry!
Damn... I gotta be stupid or something... I don't understand null and return in the code. When the form initialized all labels are null right? So there is a code: if (clickedLabel == null) return; return to what? click function is a void so it does not return value! And there is this thing: if (firstClicked.Text == secondClicked.Text) { firstClicked = null; secondClicked = null; } What is that suppose to mean? So you successfully matched 2 labels so... What does it mean? You set them both to null... Why and what does it do? Great video by the way! We all need more of these and you need much more likes and subs!
Hey, sorry I just saw this comment. When a function is void and you want to immedietely exit the function you simply call return. Like you said void means nothing so you don't type anything after return. Null basically means "no value" or empty. You could think of it as setting those variables to 0. Hope that helps and sorry again.
is someone here who knows how to show images in Wpf with style "Webbedigns"? I finished the game, but i don't know how to show images instead of letters.. P.S.: Thank you for your great video. Very nice :)
Hey you're very welcome! Unfortunately I'm super busy right now trying to ship my new video game, but I can lead you in the right direction. You could create a list of ints and each int in the list can correspond to one of the grids. For example... List timesClicked = new List (); Then in initialize or somewhere at the beginning of the program you could call timesClicked.Add (0); to add a number for each grid. You could put that line in a for loop that iterates for as many grids there are. Then once one of the grids are clicked you could say timesClicked[0]++ to add one to the count. Replace 0 with whatever grid is clicked, you would only use 0 if the very first grid was clicked. Hopefully that helps! Good luck!
Hi, am failing to do this because every time is 'int' to the grids its not compatible to the System.Windows.forms . Do you have a simpler way of doing it? or maybe you can write a simple code in the comments
+Peter susan The code I posted is the simplest way I can think of. Don't set anything to the form, it sounds like you're trying to set something that's not an int to the int. Maybe start with only one int that counts how many total times all the grids are clicked. Then once you get that working you can expand to having a counter for each grid. To do this just make an int called counter at the top and then increase it each time a grid's color is set to the visible color!
Hi. This video helped me a lot. I want to ask you a question, is possible when 2 of them matched another image to show up in the 2 icons? For example: when the spiders match, they will be replaced with "x" image. Can you help me to fix this issue? Thank you!
Very glad to hear the video was useful. In regards to your question: do you know the function where we randomly set the panel text to various letters from the Webdings font? You would do something similar... find the character in the font that is the X you want, then set the text of the matching panels to that character once you detect the match! This should be pretty easy as we've already detected when two images match.... good luck!
Nice video! I don't have any errors but my program doesn't functions after I have clicked 3 images. So, when I click on two images and if they are not the same they disappear after 750 ms but after that, I click again and the program freezes and does not respond to my clicks :/
It's like an issue with the timer freezing or not being reset maybe. Maybe take a look at that section of the video and double check everything matches up. Good luck!
Hello, Michael, your video help me a lot and I think your one of the greatest when I doing the right match between two labels I get an error message which says: System.NullReferenceException: Reference to object not set to an instance of an object. it's happened in this line: firstClick.ForeColor = firstClick.BackColor; secondClick.ForeColor = secondClick.BackColor; I hope you could help me
If you hover over those variables you'll see that one of them is null... anything that you try to access that is null will throw that error. If something is null that means it's an empty variable that has nothing inside. Usually this means you're missing a line of code where you call "new" on a class. Good luck and thanks for watching!
The same code as here, or on with the original Microsoft tutorial, and always System.NullReferenceException; ps: Ok, works fine. The issue: Time enabled was true, and it works only with false.
I must have missed something in the video because I'm getting an error of "Object reference not set to an instance of an object" for the following code in the timer1_Tick function firstClicked.ForeColor = firstClicked.BackColor; Please help!!
When it breaks, hover over the line it highlight and see which variable is set to null. This is the variable that needs set... at this point you can reference the video to find the line that sets the variable you missed. Good luck!
Somewhere that variable isn't being set, try looking through the code in the video and see if you're missing any lines related to that variable. Also, if that doesnt help try creating a whole new table Layout Panel and using the variable it gives you. Sometimes the variables don't get linked up to the form stuff properly. Good luck!
You could change fonts and just use the + - x / signs... or you could implement a picture box (see the comments below for links on this) to use whatever pictures you want. Depending on what symbols you're using, I'd just use a different font! Good luck!
There could be many different types of controls in that list, so we use the if check to confirm the current item we're looking at in the list is a Label.
You can display a message box that asks that question and then respond to it accordingly. Google C# message box yes or no and there should be code samples. If they say yes you just need to reset all of the game variables by setting them to the default values for the new round. Good luck!
This is part of a series of videos. We cover making empty projects in earlier videos! Basically you just need to go to New Project and choose Windows Form project.
Can you be more specific? What is the variable you want to look at the index of and what type of variable is it? I haven't watched this video in a while so if you can tell me that I can help you quicker. Thanks!
@@michaelartsmedia thanks for the answer, basically I want to pair labels even though they have different text (for like matching definitions and stuff)
@@kerfuritogonzales Maybe I'm dumb but I'm not sure I understand Haha. We check for matches in the video. You can use that method of looking at the labels for any type of checks you want to make! It sounds like you're wanting to alter the rules of the game a bit, you can do that and check the data the same way. Let me know if I'm misunderstanding!
@@kerfuritogonzales at 30:15 we check the text in the two labels that were clicked. You can use firstClicked.Text and secondClicked.Text to check for whatever logic you want the game to have. Hopefully that helps!
Michael Hicks (local variable) int I A local variable or function named “i” is already defined in this scope the variable “i” is declared but never used
Somewhere you've defined i two times. Is this part of the code in a double for loop where I is defined in the first loop? I'd try and find the other place it's defined. You could also reference the code in the video line for line in this function and see where things don't add up. Hope that helps!
@@michaelartsmedia Thanks man,i have exam today,exam was similar to this game,only we can use some pictures that we must use on game,i didnt know how can i use it,and make program.Ur tutorial is so good.How can i use images form resources,and make array from it and do similar game like this??Sorry for my bad englsh
@@StefanKo1224 That's no problem! Unfortunately, I haven't made this using actual pictures but it shouldn't be too hard. You would use a PictureBox, which is a form you can drag and drop from the Toolbox. Here's an article that shows the basics: www.c-sharpcorner.com/uploadfile/mahesh/picturebox-in-C-Sharp/ Good luck!
DialogResult di = MessageBox.Show("You won! Another Game?", "Another?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (di == DialogResult.Yes) { assign(); } I'm getting errors like index was out of range must be non negative and less than the size of the collection It is good for one game only? How can I play another game without restarting the program?
Where exactly is it breaking? The code you posted doesn't have a place where the index can be out of range. I'd recommend setting a break point and debugging your code line by line to figure out where the issue is stemming from. I have a video in this series where I show how to debug like this. You should be able to re start this game and play another round if the variables are reset correctly. Good luck!
How are you assigning the random number? Most likely the range of that number is outside of the limit of the variable. For example if we have 9 items in the list and your random number is picking a random number between 0 and 15, anything over 8 will crash the code.
Sorry I just saw the full function you posted, I'm on mobile so it wasn't showing up right. The issue is you're removing an icon from the list then using the random number as an index into the list after removing something. Think about it like this... there are 8 items in the list and we get a random number of 7 (the last item in the list). After you remove the icon from the list the number of the items in the list is now 7. Your original random number of 7 is no longer valid and will cause the code to crash.
Yes, that's what happened when I called again the method of assign that code crashes because the icon index is 0. My problem how can I restore the number of icons?
Thank you for your content. Im looking forward to more videos. I have subbed :) I really want to get a job as junior unity dev. I find myself going back and forth between unity and wpf/wpa(games such as above). I've been coding for like 7-8 months now man. But i feel like i cant write code like the abov(yet)e, but i can read it, and understand it. I was told once, just stick to game or app dev, rarely developers do both. What can i do bro? p.s. I not used to seeing null used so often. if (firstClicked.Text == secondClicked.Text) { firstClicked = null; secondClicked = null; } //i tired, but it didnt work. icons.Remove(firstClicked.Text); icons.Remove(secondClicked.Text); by making it null are we removing it from the icons? null means empty. yea.
Hey thanks for the sub, hopefully you find the channel useful. I'm currently in the middle of the next programming series... hoping to get it out early next year. null means "nothing". When the data you're working with is a class, you can set the variable to nothing by setting it to null. null doesn't work on data types such as string or int, only on classes. If you have a null item in a list C# will not automatically remove it from the list... you'll have to manually remove it. It's possible for lists to contain null items. There are some scenarios where you actually would want this... but not in this video! Hope that helps!
Thanks Micheal. You explained null very well. I learned list can contain nulls, yet keep there length. And we can only set class objects to null and not data types. I'm back on TH-cam. Watching your videos, and learning this morning. I look forward to your future content. And you have my full support. Regards, Lais
Hey I don't speak Russian so I can't really help you much. I used Google Translate and I think you're asking about expanding the array to hold more than 20 pictures? Yes, you'll need to expand the array to hold 20 (or however many you want). In programming you need to define how many items are stored in an array. If the index goes over how many items the array can store you'll get a crash. Same thing with Lists... if you try to access the 21st item in a list and there's only 20 items, it will crash. Hopefully that helps!
@@michaelartsmedia спасибо. Я сделал вот так List icons = new List() { "я", "я", "N", "N", "O", "O", "k", "k","S", "S","U","U","Y","Y","e","e","J","J", "b", "b", "v", "v", "ц", "ц", "I", "I", "Z", "Z","!","!","ы","ы","т","т","j","j" }; 36 items. it will be more difficult :)
haha true! Ok so, whenever you iterate through this array you need to make sure the index never goes over 35... that may be the issue. In programming we start counting at 0, so if you try to access the array at index 36 it will crash.
An unhandled exception of type 'System.NullReferenceException' occurred in project.exe Additional information: Object reference not set to an instance of an object. getting this error after first match.Please,help
When your game crashes, hover over the variables on the line it highlights. One of them will be null, this variable is not being set correctly. Once you know which variable is null, look back on the video to find the line of code that is setting that variable. For example if the null variable is "pizza", find the line in the video that says "pizza =". Either this line is not in your program or something else is not causing it to be hit. Good luck!
I'd prefer to do it here in case other people have the same problem. Also, I'm super busy this month so it may be hard for me to reply. But I'll try my best to help!
@@michaelartsmedia i'll explain my issue. I can't link the timer from the game into the code and I've tried turning it into an object which was unsuccessful
Try clicking the timer icon at the bottom of the form view. Next look at the properties window and look for the "Name" property. Whatever is listed here is what you'll type in the code view. Hope that helps!
I didn't upload it for that exact reason :p Learning how to debug and problem solve makes people better coders and human beings! Too many people just copy and paste these things lol. If you're having problems I'll try my best to walk you through debugging. Just let me know what your issue is!
hello there ! At 17:50 when you run the code, i too checked the code as i was going along with you , and i got an error which i wasnt able to spot. Can youi please help me out on that??namespace Memory_Match.cs { public partial class Form1 : Form { Random random = new Random(); List icons = new List() { "!","!","N","N","k","k","@","@", ",",",","v","v","~","~",""",""" }; Label firstClicked, secondClicked; public Form1() { InitializeComponent(); AssignIconsToSquares(); } private void AssignIconsToSquares() { Label label; int randomNumber; for(int i = 0; i< tableLayoutPanel1.Controls.Count; i++) { if (tableLayoutPanel1.Controls[i] is Label) label = (Label)tableLayoutPanel1.Controls[i]; else continue; randomNumber = random.Next(0,icons.Count); label.Text = icons[randomNumber]; icons.RemoveAt(randomNumber); } } } } At the line " label.Text = icons[randomNumber];" i got the error : System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')' can you please help me out???
6years later and you are still helping people! Thank you so much for this very helpful video!
That's great to hear. Thanks for watching!
Thank you for saving my grade in programming at school :)
Haha that's awesome to hear! You're welcome!
@@michaelartsmedia uisng the newest version of visual studio also using c# windows form.
For me, on random, icons, firstclicked and secondclicked
It says that "A namespace cannot directly contain members such as fields or methods"
And because of this the program does not work
Do you maybe know how to fix these errors?
@@saltypepper3340if u use WinForms your code should be nested like this : namespace YourProject { class Form1 : Form { public Form1(){initializeCompontets();}}}
"initializeComponts" is the method to initalize the controls on your win Form genereted by the designer .
public Form is the constructor ,is the first method called when u create an istance of ur class. The class is a model of an object istance in your specific case a "WinForm" the namespace is the place where all ur classes exist u can also join multiple namespaces with this line "using NameOfYourNameSpace;"
Excellent! My first C# experience plus Forms too! You are an amazing teacher. Thank you so much.
Thanks for watching!
Great tutorial, I understood everything you said. It was all nice and clear, great job dude.
That's great to hear! Thanks for watching!
Thank you it's a big help for my case study this midterm for my subject "Game Development" ! Make more game using C# in VB please!
One of the best tutorials I've ever done. Thank you so much
Thanks for watching!!
dude in 2021 u saved my coursework on c#))) Thx buddy)))
Dude, this was fantastic. Definitely going to incorporate some of your ideas into my project.
I actually tried racing you at the end. You won, cant beat the creator I guess. Thanks for the help!
Thank you my man. This Video helped me and my friend to safe our grade in IT Class.
thanks mate, great tutorial i had to do this for my school with extra reset, start, stop and exit buttons and this here pretty much made it possible without any problems
Hey, i have the same exercise with those buttons, can you write how did you manage to do them?
Excellent tutorial, however I'm getting an error in the AssignIconsToSquares method. The error occurs on the line that reads: "label.Text = icons[randomnumber];" The error says, "Use of unassigned local variable 'label'.
I don't understand why this error occurs. I typed in the same code you showed here. I'm using VS 2019, so I don't know if that makes a difference or not. I don't see how it would, but you never know with Microsoft.
Thanks. :)
That error means you're trying to use a variable that hasn't been assigned. A lot of times just assigning the variable to an empty value where you declare it initially will fix this.
So instead of
ClassName class;
ClassName class = null; would fix the error.
Try comparing your declaration with what's in the video and look for things like that. Thanks for watching!
I was getting the same error, but saw that I forgot to put a "N" in the list. After adding that, it fixed it.
Nice and easy tutorial, thanks for the help, it was useful and easy to understand.
Amazing game Michael! Thank you very much!
No problem, thanks for watching!
You deserve a thousand likes!
Thanks! Hopefully someday I'll get there haha
@Michael Hicks I'm kind replicating this exercice with visual basic code. I'm having trouble with hiding the images (as I'm using pictureboxes) and creating an event for when the images clicked match. Any tips?
Thank you so much Nice and clear. That was great. step by step. You are a great teacher I have learned all of them. especially "oops!" between codes were great ;)!!!
haha so glad to hear you enjoyed it! Thanks for watching!
I like it very much. Thanks Michael!
Awesome to hear, thanks for watching!
nice and very explanatory. thanks for your tutorial
Thanks for watching!
Great tutorial! Thank you!
It tells me error on the label.Text= icons[randomNumber];
It says "index was out of range",Must be non-nevative and less than the size of the collection...
Help pleaseee Michael
K,
Hello, tableLayoutPanel.Controls is an empty collection for me? Am I supposed to intialise that collection somewhere, I can't see you do it at any point in your video?
Great Tutorial! I've got a question, if at the end of the game you want to restart it instead of the "close();" at the end, what would we run instead?
Thanks for watching! You'll need to reinitialize all of the variables and form info to their initial state. That means resetting the draw colors to the invisible color, and also resetting the variables we declare at the top. Good luck!
Nice video! Very well explained I am trying to add a new feature to it. I am trying to connect a trackbar to the game. The trackbar minimum value is 4 and max is 10. If the track bar is value 4 it should be 4*4 if it’s 5 it should be 5*5 but i am not having success.
Thanks for watching! What exactly is the issue? The track bar is supposed to change the grid of the game but it's not somehow? The more details you give the better!
Hello, I'm having trouble at 17:50. For me, just one image shows up and the rest are just the squares, does anyone know what's wrong please?
Hi Micheal Hicks. I following the skip but on the line 32 if(firstClicked ! = null && secondClicked ! = null) return; , it is still error. Can i know what happens?
Hi Michael. This is the best tutorial that I've ever seen.
I do my memo game with pictureboxes. I did almost everything as you did. But I have List of the pictures, not the labels ... and at the last step when I need to compare 2 "icons" I dont know how to compare them. Could you help me please?
Haha thanks a lot, you're too kind! There is likely an Image variable in the picture box that you can compare. If the images are equal then you know they are matching!
@@michaelartsmedia sure there is. The question is how to refer to that image variable . And what is the variable it is.
@@Tim_Linkel You should be able to just compare that variable. If not, you can create a string variable that stores the name or path of the image. When you set the image make sure you set this variable appropriately and you can compare the name of the images later on.
thank you so much. I've just now found a solution to make it works.
Great game and thanks much for teaching the code as well..... hope you still monitor the site because another feature for this would be... should someone guess 3 times and NOT match, then the game penalizes them and resets the entire game, regardless of how far along you are. So if you can, help me with the code that would do this...... i'm real new to c...... Also for me personally, I will play with it and add more cols and rows and make it really tough!!
Thank you! It helped me a lot! But I have one problem, I have no “tablelayout” in toolbox. What should I do? I would love an answer quickly because I need it for a project for this week.
Unfortunately I probably can't be much help with that. Possibly it didn't come with your install of visual studio? You'll probably have better luck googling that... sorry!
@@michaelartsmedia oh I understand. Ty!
Damn... I gotta be stupid or something... I don't understand null and return in the code. When the form initialized all labels are null right?
So there is a code:
if (clickedLabel == null)
return;
return to what? click function is a void so it does not return value!
And there is this thing:
if (firstClicked.Text == secondClicked.Text)
{
firstClicked = null;
secondClicked = null;
}
What is that suppose to mean? So you successfully matched 2 labels so... What does it mean? You set them both to null... Why and what does it do?
Great video by the way! We all need more of these and you need much more likes and subs!
Hey, sorry I just saw this comment. When a function is void and you want to immedietely exit the function you simply call return. Like you said void means nothing so you don't type anything after return.
Null basically means "no value" or empty. You could think of it as setting those variables to 0.
Hope that helps and sorry again.
Thank you for your teaching
can I use picture box if I want to set the picture instead of label???
Dude, AMAZING thankyou!
is someone here who knows how to show images in Wpf with style "Webbedigns"? I finished the game, but i don't know how to show images instead of letters..
P.S.: Thank you for your great video. Very nice :)
thank you for the big help!
if i want to use pictures instead of letters, can i use picture box?
I have to do the same thing but instead of labels in each of the boxes, I need to use picture boxes, any suggestions?
nice tutorial thank you you helped me out a lot!
Great to hear! Thanks for watching!
Thanks so much for the video.
Can you please write a function for counting how many times each label clicked at the end of the game.
Hey you're very welcome! Unfortunately I'm super busy right now trying to ship my new video game, but I can lead you in the right direction. You could create a list of ints and each int in the list can correspond to one of the grids. For example...
List timesClicked = new List ();
Then in initialize or somewhere at the beginning of the program you could call timesClicked.Add (0); to add a number for each grid. You could put that line in a for loop that iterates for as many grids there are.
Then once one of the grids are clicked you could say timesClicked[0]++ to add one to the count. Replace 0 with whatever grid is clicked, you would only use 0 if the very first grid was clicked.
Hopefully that helps! Good luck!
Thanks so much for responding quickly!!!
this is a great idea am getting on it right away.
Cheers to you sir
Hi, am failing to do this because every time is 'int' to the grids its not compatible to the System.Windows.forms .
Do you have a simpler way of doing it?
or maybe you can write a simple code in the comments
+Peter susan The code I posted is the simplest way I can think of. Don't set anything to the form, it sounds like you're trying to set something that's not an int to the int. Maybe start with only one int that counts how many total times all the grids are clicked. Then once you get that working you can expand to having a counter for each grid. To do this just make an int called counter at the top and then increase it each time a grid's color is set to the visible color!
Thanks Sir. I'll start from here and do it.
I appreciate you responding even on your old videos
How long did it take you to get to this point? In terms of time how did you personally learn?
I taught myself by making small games. If you have a passion for coding you will learn the more you dive in!
@@michaelartsmedia Thanks Michael.
Hi. This video helped me a lot. I want to ask you a question, is possible when 2 of them matched another image to show up in the 2 icons? For example: when the spiders match, they will be replaced with "x" image. Can you help me to fix this issue?
Thank you!
Very glad to hear the video was useful. In regards to your question: do you know the function where we randomly set the panel text to various letters from the Webdings font? You would do something similar... find the character in the font that is the X you want, then set the text of the matching panels to that character once you detect the match! This should be pretty easy as we've already detected when two images match.... good luck!
Michael Hicks thank you a lot!
Dude seriosly thank you !
Thank you a lot !! great tutorial
Nice video!
I don't have any errors but my program doesn't functions after I have clicked 3 images. So, when I click on two images and if they are not the same they disappear after 750 ms but after that, I click again and the program freezes and does not respond to my clicks :/
It's like an issue with the timer freezing or not being reset maybe. Maybe take a look at that section of the video and double check everything matches up. Good luck!
Hey what version of visual studio did you use?
Hello, Michael, your video help me a lot and I think your one of the greatest when I doing the right match between two labels I get an error message which says:
System.NullReferenceException: Reference to object not set to an instance of an object. it's happened in this line:
firstClick.ForeColor = firstClick.BackColor;
secondClick.ForeColor = secondClick.BackColor;
I hope you could help me
If you hover over those variables you'll see that one of them is null... anything that you try to access that is null will throw that error. If something is null that means it's an empty variable that has nothing inside. Usually this means you're missing a line of code where you call "new" on a class. Good luck and thanks for watching!
The same code as here, or on with the original Microsoft tutorial, and always System.NullReferenceException;
ps: Ok, works fine. The issue: Time enabled was true, and it works only with false.
Druze, volim te
Thank you bro 🤙
is it possible to use images or pictures instead of icons?
Thanks!
Great video!
Thanks you pro your are great
I couldn’t help my daughter, but this tutorial DID 😊😊😊😊🍾🤓🤓🙏🙏🙏🙏🙏🙏🙏🎖🎖🎖🎖🎖🎖🎖
This is the video that saved my grad, but the pictures won’t random they are just next to each outer. What am I doing wrong?
It's hard to say without looking at code, but I would make sure the line that uses the "random" function is written correctly. Good luck!
Thanks man
I must have missed something in the video because I'm getting an error of "Object reference not set to an instance of an object" for the following code in the timer1_Tick function
firstClicked.ForeColor = firstClicked.BackColor;
Please help!!
When it breaks, hover over the line it highlight and see which variable is set to null. This is the variable that needs set... at this point you can reference the video to find the line that sets the variable you missed. Good luck!
i was getting the same thing but i checked a lot times somehow my code "timer1.Start(); " wrote two times, just delete one and it works fine.
I am getting an error message for this. for (int i = 0; i
Somewhere that variable isn't being set, try looking through the code in the video and see if you're missing any lines related to that variable. Also, if that doesnt help try creating a whole new table Layout Panel and using the variable it gives you. Sometimes the variables don't get linked up to the form stuff properly. Good luck!
@@michaelartsmedia thanks.
i was getting the same thing but i checked a lot times somehow my code "timer1.Start(); " wrote two times, just delete one and it works fine.
hi man ,do you have tutorial how to add timer to the game like 1 min to win?
Ty dude!
how do I insert picture files instead of icons?
did you figured out how?? i would like to know
How can I add levels into this game please give some ideas
*ty nice tut ! :)*
No problem thanks for watching!
Is mutiplayer?
Nice
Each time i try always match first 2. all cards match and shows only SQUARE.
How can I change it into mathematical icons?
You could change fonts and just use the + - x / signs... or you could implement a picture box (see the comments below for links on this) to use whatever pictures you want. Depending on what symbols you're using, I'd just use a different font! Good luck!
@@michaelartsmedia thank you for your answer :>
didnt understand the line 38 in the video. Why the Labels of tableLayoutPanel.Controls are stored in the local variable label?
Why did you use the if condition on line 38? Kindly explain it explicitly.
There could be many different types of controls in that list, so we use the if check to confirm the current item we're looking at in the list is a Label.
it gives me the error property not found when ever i want to type c in gridcharset
Iam getting warning like the field'form1.firstclicked'is never used and also the field 'form1.second clicked is never used
Can you do this in Visual Basic ?
it is visual basic lol
How do you edit it so it will ask you if you want to start a new game instead of closing it?
You can display a message box that asks that question and then respond to it accordingly. Google C# message box yes or no and there should be code samples. If they say yes you just need to reset all of the game variables by setting them to the default values for the new round. Good luck!
@@michaelartsmedia Thank you.
What if I wanted to program a bot to play against me? E.g. it would just pick random cards (no intelligence)
You didn’t tell us how you got that empty form set up. Doesn’t help beginners like me
This is part of a series of videos. We cover making empty projects in earlier videos! Basically you just need to go to New Project and choose Windows Form project.
I used Community Version of VS (2015) I can't find TablePanel is there any alternative toolbox?
2015 should have a TablePanel, try looking under the "All" category of the toolbox!
Sorry but I cant find it 😔
Is there a guide to find it
???
Try this: docs.microsoft.com/en-us/visualstudio/ide/step-4-lay-out-your-form-with-a-tablelayoutpanel-control?view=vs-2019
I probably won't get an answer, but how do I check the pairs by index of the elements in the list?
Can you be more specific? What is the variable you want to look at the index of and what type of variable is it? I haven't watched this video in a while so if you can tell me that I can help you quicker. Thanks!
@@michaelartsmedia thanks for the answer, basically I want to pair labels even though they have different text (for like matching definitions and stuff)
@@kerfuritogonzales Maybe I'm dumb but I'm not sure I understand Haha. We check for matches in the video. You can use that method of looking at the labels for any type of checks you want to make! It sounds like you're wanting to alter the rules of the game a bit, you can do that and check the data the same way. Let me know if I'm misunderstanding!
@@michaelartsmedia you understand it quite well, that's basically what I'm trying to do. I just don't exactly know how
@@kerfuritogonzales at 30:15 we check the text in the two labels that were clicked. You can use firstClicked.Text and secondClicked.Text to check for whatever logic you want the game to have. Hopefully that helps!
when im putting the code for (int i = 0, i < tableLayoutPanel1.Controls.Count; i++) i'm getting errors,):
What is the error you're getting? If you can provide some more details I'll see if I can help!
Michael Hicks (local variable) int I A local variable or function named “i” is already defined in this scope the variable “i” is declared but never used
Somewhere you've defined i two times. Is this part of the code in a double for loop where I is defined in the first loop? I'd try and find the other place it's defined. You could also reference the code in the video line for line in this function and see where things don't add up. Hope that helps!
i fixed it man, it was autocorrecting me , instead of putting Private Void AssignIconstoSquares() it was Putting Private Void AssignIconstpSquares()
thank you man,
sir very good lesson
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 카드짝_맞추기_게임
{
public partial class Form1 : Form
{
Random random = new Random();
List icons = new List()
{
"!", "!", "N", "N", ",", ",", "k", "k",
"b", "b", "v", "v", "w", "w", "z", "z"
};
Label firstclicked, secondclicked;
public Form1()
{
InitializeComponent();
AssignIconsToSquares();
}
private void AssignIconsToSquares()
{
Label label;
int randomnumder;
for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
{
if (tableLayoutPanel1.Controls[i] is Label)
label = (Label)tableLayoutPanel1.Controls[i];
else
continue;
randomnumder = random.Next(0, icons.Count);
label.Text = icons[randomnumder];
icons.RemoveAt(randomnumder);
}
}
private void label_Click(object sender, EventArgs e)
{
if (firstclicked! = null && secondClicked ! = null)
return;
Label clickedLabel = sender as Label;
if (clickedLabel == null)
return;
if (clickedLabel.ForeColor == Color.Black)
return;
if (firstclicked == null)
{
firstclicked = clickedLabel;
firstclicked.ForeColor = Color.Black;
return;
}
secondclicked = clickedLabel;
secondclicked.ForeColor = Color.Black;
CheckForwinner();
if (firstclicked.Text == secondclicked.Text)
{
firstclicked = null;
secondclicked = null;
}
else
timer1.Start();
}
private void CheckForwinner()
{
Label label;
for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
{
Label = tableLayoutPanel1.Controls[i] as Label;
if (Label! = null && Label.DefaultForeColor == Label.backcolor)
return;
}
MessageBox.show("You matched all the icons! ");
close();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
firstclicked.ForeColor = firstclicked.BackColor;
secondclicked.ForeColor = secondclicked.BackColor;
firstclicked = null;
secondclicked = null;
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
}
}
i've some issues
is there anything wrong i typed??
I dont understant how when program run,it shows pictures,not letters that are on list
The Webdings font uses pictures not letters. If you open up Wordpad and start typing with the Webding font you'll see the same images.
@@michaelartsmedia Thanks man,i have exam today,exam was similar to this game,only we can use some pictures that we must use on game,i didnt know how can i use it,and make program.Ur tutorial is so good.How can i use images form resources,and make array from it and do similar game like this??Sorry for my bad englsh
@@StefanKo1224 That's no problem! Unfortunately, I haven't made this using actual pictures but it shouldn't be too hard. You would use a PictureBox, which is a form you can drag and drop from the Toolbox. Here's an article that shows the basics: www.c-sharpcorner.com/uploadfile/mahesh/picturebox-in-C-Sharp/ Good luck!
DialogResult di = MessageBox.Show("You won!
Another Game?", "Another?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (di == DialogResult.Yes)
{
assign();
}
I'm getting errors like index was out of range must be non negative and less than the size of the collection
It is good for one game only? How can I play another game without restarting the program?
Where exactly is it breaking? The code you posted doesn't have a place where the index can be out of range. I'd recommend setting a break point and debugging your code line by line to figure out where the issue is stemming from. I have a video in this series where I show how to debug like this. You should be able to re start this game and play another round if the variables are reset correctly. Good luck!
How are you assigning the random number? Most likely the range of that number is outside of the limit of the variable. For example if we have 9 items in the list and your random number is picking a random number between 0 and 15, anything over 8 will crash the code.
Sorry I just saw the full function you posted, I'm on mobile so it wasn't showing up right. The issue is you're removing an icon from the list then using the random number as an index into the list after removing something. Think about it like this... there are 8 items in the list and we get a random number of 7 (the last item in the list). After you remove the icon from the list the number of the items in the list is now 7. Your original random number of 7 is no longer valid and will cause the code to crash.
I assign the random number like this
List icons = new List()
{
"a","a","b","b","c","c","d","d","e","e","f","f","h","h","i","i"
};
Yes, that's what happened when I called again the method of assign that code crashes because the icon index is 0. My problem how can I restore the number of icons?
Thank you for your content. Im looking forward to more videos. I have subbed :)
I really want to get a job as junior unity dev. I find myself going back and forth between unity and wpf/wpa(games such as above).
I've been coding for like 7-8 months now man. But i feel like i cant write code like the abov(yet)e, but i can read it, and understand it. I was told once, just stick to game or app dev, rarely developers do both. What can i do bro?
p.s. I not used to seeing null used so often.
if (firstClicked.Text == secondClicked.Text)
{
firstClicked = null;
secondClicked = null;
}
//i tired, but it didnt work.
icons.Remove(firstClicked.Text);
icons.Remove(secondClicked.Text);
by making it null are we removing it from the icons?
null means empty. yea.
Hey thanks for the sub, hopefully you find the channel useful. I'm currently in the middle of the next programming series... hoping to get it out early next year.
null means "nothing". When the data you're working with is a class, you can set the variable to nothing by setting it to null. null doesn't work on data types such as string or int, only on classes.
If you have a null item in a list C# will not automatically remove it from the list... you'll have to manually remove it. It's possible for lists to contain null items. There are some scenarios where you actually would want this... but not in this video!
Hope that helps!
Thanks Micheal. You explained null very well. I learned list can contain nulls, yet keep there length. And we can only set class objects to null and not data types.
I'm back on TH-cam. Watching your videos, and learning this morning.
I look forward to your future content. And you have my full support.
Regards,
Lais
i ly
I did not like this video, my code just does not work.
17:25
Больше 20 картинок пишет нельзя. Индекс не может быть больше 20. Почему? Как сделать больше 20?
Hey I don't speak Russian so I can't really help you much. I used Google Translate and I think you're asking about expanding the array to hold more than 20 pictures? Yes, you'll need to expand the array to hold 20 (or however many you want). In programming you need to define how many items are stored in an array. If the index goes over how many items the array can store you'll get a crash. Same thing with Lists... if you try to access the 21st item in a list and there's only 20 items, it will crash. Hopefully that helps!
@@michaelartsmedia спасибо. Я сделал вот так
List icons = new List()
{
"я", "я", "N", "N", "O", "O", "k", "k","S", "S","U","U","Y","Y","e","e","J","J",
"b", "b", "v", "v", "ц", "ц", "I", "I", "Z", "Z","!","!","ы","ы","т","т","j","j"
};
36 items. it will be more difficult :)
haha true! Ok so, whenever you iterate through this array you need to make sure the index never goes over 35... that may be the issue. In programming we start counting at 0, so if you try to access the array at index 36 it will crash.
CAN YOU GIVE ME SOURCE CODE , PRORGAM , I WANT IT, PLEASE !!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
Random random = new Random();
List icons = new List()
{
"!", "!", "N", "N", ",", ",", "k", "k",
"b", "b", "v", "v", "w", "w", "z", "z"
};
Label firstClicked, secondClicked;
public Form1()
{
InitializeComponent();
AssignIconsToSquares();
}
private void AssignIconsToSquares()
{
Label label;
int randomNumber;
for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
{
if (tableLayoutPanel1.Controls[i] is Label)
label = (Label)tableLayoutPanel1.Controls[i];
else
continue;
randomNumber = random.Next(0, icons.Count);
string v = icons[randomNumber];
label.Text = v;
icons.RemoveAt(randomNumber);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
firstClicked.ForeColor = firstClicked.BackColor;
secondClicked.ForeColor = secondClicked.BackColor;
firstClicked = null;
secondClicked = null;
}
private void label1_Click(object sender, EventArgs e)
{
if (firstClicked != null && secondClicked != null)
return;
Label clickedLabel = sender as Label;
if (clickedLabel == null)
return;
if (clickedLabel.ForeColor == Color.Black)
return;
if (firstClicked == null)
{
firstClicked = clickedLabel;
firstClicked.ForeColor = Color.Black;
return;
}
secondClicked = clickedLabel;
secondClicked.ForeColor = Color.Black;
CheckForWinner();
if (firstClicked.Text == secondClicked.Text)
{
firstClicked = null;
secondClicked = null;
}
else
timer1.Start();
}
private void CheckForWinner()
{
Label label;
for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
{
label = tableLayoutPanel1.Controls[i] as Label;
if (label != null && label.ForeColor == label.BackColor)
return;
}
MessageBox.Show("You matches the icons!");
Close();
}
}
}
An unhandled exception of type 'System.NullReferenceException' occurred in project.exe
Additional information: Object reference not set to an instance of an object.
getting this error after first match.Please,help
When your game crashes, hover over the variables on the line it highlights. One of them will be null, this variable is not being set correctly. Once you know which variable is null, look back on the video to find the line of code that is setting that variable. For example if the null variable is "pizza", find the line in the video that says "pizza =". Either this line is not in your program or something else is not causing it to be hit. Good luck!
I’ve been following along with the tutorial and I’ve been having a few issues at the end. Can I dm you on Twitter to show you the issues?
I'd prefer to do it here in case other people have the same problem. Also, I'm super busy this month so it may be hard for me to reply. But I'll try my best to help!
@@michaelartsmedia i'll explain my issue. I can't link the timer from the game into the code and I've tried turning it into an object which was unsuccessful
@@luckyjim37 What exactly do you mean by that? You can't find the timer variable when you type it in the code?
@@michaelartsmedia exactly
Try clicking the timer icon at the bottom of the form view. Next look at the properties window and look for the "Name" property. Whatever is listed here is what you'll type in the code view. Hope that helps!
Heya boss.. Could you upload somewhere the main code please ? Thank youu
Where can I download the source code? I'm lazy :D
I didn't upload it for that exact reason :p Learning how to debug and problem solve makes people better coders and human beings! Too many people just copy and paste these things lol. If you're having problems I'll try my best to walk you through debugging. Just let me know what your issue is!
hello there ! At 17:50 when you run the code, i too checked the code as i was going along with you , and i got an error which i wasnt able to spot. Can youi please help me out on that??namespace Memory_Match.cs
{
public partial class Form1 : Form
{
Random random = new Random();
List icons = new List()
{
"!","!","N","N","k","k","@","@",
",",",","v","v","~","~",""","""
};
Label firstClicked, secondClicked;
public Form1()
{
InitializeComponent();
AssignIconsToSquares();
}
private void AssignIconsToSquares()
{
Label label;
int randomNumber;
for(int i = 0; i< tableLayoutPanel1.Controls.Count; i++)
{
if (tableLayoutPanel1.Controls[i] is Label)
label = (Label)tableLayoutPanel1.Controls[i];
else
continue;
randomNumber = random.Next(0,icons.Count);
label.Text = icons[randomNumber];
icons.RemoveAt(randomNumber);
}
}
}
}
At the line " label.Text = icons[randomNumber];"
i got the error : System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')'
can you please help me out???
(i use microsoft visual studio community version 2022)
thanks dude