C
C#3y ago
Nikolaaa

cant change text label inside events?

why is scorelbl.Text red when I have label named like that in designer?
32 Replies
MonJamp
MonJamp3y ago
Can you post the error message? Also is this WinForms, WPF, MAUI?
Nikolaaa
NikolaaaOP3y ago
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
MonJamp
MonJamp3y ago
It could be a typo, are you sure it's named exactly scorelbl in the designer (no capitals)?
Nikolaaa
NikolaaaOP3y ago
yes i checked that already also i think this was not happening until i updated vs
MonJamp
MonJamp3y ago
Are you able to compile it? Sometimes there's red squiggles where there shouldn't be
Nikolaaa
NikolaaaOP3y ago
umm i didnt try i will now should have i put everything in public Snek() { InitializeComponent(); } ? or it can be outside
MonJamp
MonJamp3y ago
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 working
Nikolaaa
NikolaaaOP3y ago
still red
Nikolaaa
NikolaaaOP3y ago
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…
Nikolaaa
NikolaaaOP3y ago
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
MonJamp
MonJamp3y ago
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 useful
Nikolaaa
NikolaaaOP3y ago
one sec do you want only the part where the name is or?
Nikolaaa
NikolaaaOP3y ago
Nikolaaa
NikolaaaOP3y ago
its identical to his but with Snek instead of Form1 and scorelbl instead of scoretxt
MonJamp
MonJamp3y ago
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?
Nikolaaa
NikolaaaOP3y ago
i didnt yet compile cus i need to change other things to meet my names yes
namespace Snek
{
partial class Snek
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "Form1";
}

#endregion
}
}
namespace Snek
{
partial class Snek
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "Form1";
}

#endregion
}
}
this is from snek.designer.cs
MonJamp
MonJamp3y ago
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 compile
Nikolaaa
NikolaaaOP3y ago
you mean build? or what
MonJamp
MonJamp3y ago
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
Nikolaaa
NikolaaaOP3y ago
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?
MonJamp
MonJamp3y ago
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
Nikolaaa
NikolaaaOP3y ago
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
MonJamp
MonJamp3y ago
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
Nikolaaa
NikolaaaOP3y ago
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
MonJamp
MonJamp3y ago
So none of the controls you created are showing up in Snek.designer.cs?
Nikolaaa
NikolaaaOP3y ago
Yes none It was working before on other projects Thats why i am confused
MonJamp
MonJamp3y ago
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 see
Nikolaaa
NikolaaaOP3y ago
I will try to do that tomorrow im gonna go sleep now Bye
MonJamp
MonJamp3y ago
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
Nikolaaa
NikolaaaOP3y ago
something weird happened, i woke up turned my computer on only to see that my form1 design is back to default
MonJamp
MonJamp3y ago
@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 saved
Nikolaaa
NikolaaaOP3y ago
now 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
Want results from more Discord servers?
Add your server