cant change text label inside events?
why is scorelbl.Text red when I have label named like that in designer?
32 Replies
Can you post the error message?
Also is this WinForms, WPF, MAUI?
winforms
it says it does not exist in current context
i think it was working in net framework when i used it now i use net core
It could be a typo, are you sure it's named exactly
scorelbl
in the designer (no capitals)?yes i checked that already
also i think this was not happening until i updated vs
Are you able to compile it? Sometimes there's red squiggles where there shouldn't be
umm i didnt try i will now
should have i put everything in
public Snek()
{
InitializeComponent();
}
?
or it can be outside
Snek()
is the constructor method for the class. scorelbl
should be accessible anywhere within the class
Can you post the entire class? Afaik it should be workingstill red
i am trying to follow this tutorial https://www.mooict.com/c-tutorial-how-to-make-a-classic-snakes-game-with-windows-form-and-visual-studio/
Anhar Ali
Moo ICT - Project Based Tutorials
C# Tutorial – How to make a Classic Snakes Game with Windows form a...
Hi Welcome to this tutorial. This tutorial is an updated version of the C# Snakes game tutorial we have done before on the website. This video tutorial will explain all of the core components need…
i also change names
his scoretxt is my scorelbl but it is red
and see in tutorial its outside public Form1()
could it be a problem that he may be using net framework and I use net core
Yes it's outside the constructor but still within the
Form1
class. Can you post your code for Form1
?
I doubt it. I've written WinForms applications that work fine once migrated from Framework to Core. For an application like this, it shouldn't matter that it's Framework or Core
Also a screenshot of the properties of the control itself would be usefulone sec
do you want only the part where the name is or?
its identical to his but with Snek instead of Form1
and scorelbl instead of scoretxt
Do you get any other errors or is this the only error you get when you compile?
The information I'm still missing is that, I still don't know if
scorelbl
is in the same class as Snek
. Do you see the file named Snek.designer.cs
?i didnt yet compile cus i need to change other things to meet my names
yes
this is from snek.designer.cs
The
*.designer.cs
file is autogenerated from the controls you add in the design view. In the file there should be a definition like Label scorelbl = new Label();
but that is absent from this file
IIRC it is generated during compileyou mean build?
or what
Fix the names and as many red squiggles as you can until you are ready to compile
Yes when you build the application, you are compiling the code
oh ok i will try
but wont the build fail if it has errors
i am fixing them but they are still red, but it doesnt matter right?
Sometimes the red squiggles you see when editing a code disappears when compiling it. This is especially true with WinForms since some files are autogenerated during compilation
Compiler error messages > editor error messages
also i am not sure which element is he using here where it says gameTimer.Stop that isnt normal label right?
im compiling
i commented the gametimer line until i know what it is
A good practice especially in GUI development is to name controls with the abbreviation of the control type.
For example:
btnPlay
or playBtn
This shows that this control is of type Button
gameTimer
This control can be assumed to be of type Timer
there is no Timer in toolbox
thats why i am asking i already searchedi t
it
also it did not compile
red things still here and build failed
and shouldt it auto generate stuff in designer.cs without compiling
at least it was doing that before
So none of the controls you created are showing up in
Snek.designer.cs
?Yes none
It was working before on other projects
Thats why i am confused
Right click
Snek.cs
> Design View
. Delete scorelbl
and recreate it with the same name. Comment out all the other red squiggles so you can focus on just this issue. Before building run Clean
then build and post the errors you seeI will try to do that tomorrow im gonna go sleep now
Bye
Good luck. I recommend starting out with a smaller project. Creating the forms/controls one by one and frequently compiling would help you understand common errors and how to resolve them. I think copying/pasting a lot of code is creating confusion
something weird happened, i woke up turned my computer on only to see that my form1 design is back to default
@Nikolaaa that kinda makes sense because it sounded like the
desinger.cs
wasn't being generated so none of your changes on the form were being savednow it did get generated and i fixed all errors and added stuff i needed but the game does not work
no errors are shown just that when i click play it does not work