C
C#9mo ago
Tsjech

❔ Need help adding a label from a child class to the screen that exists in the parent class

So im coding a board game and the board is its own class, now when the game ends I would like to show that by adding a label with some text to the screen, yet this label exists in the parent class because it takes up more of the screen than just the gameboard (which isnt the entire screen). Is there any way I can show this label only when the game ends (which is checked in the board class)? (please ask me more questions if needed this explaination is far from good :) )
151 Replies
Pobiega
Pobiega9mo ago
is this using winforms? WPF?
Tsjech
Tsjech9mo ago
windows forms yes
Tsjech
Tsjech9mo ago
all im using
No description
Pobiega
Pobiega9mo ago
I didn't mean your using statements, I meant what GUI framework are you using 😛 but thats winforms, clearly
Tsjech
Tsjech9mo ago
yeah, This is for a uni project and we get this kinda "preset" So i dont have to deal with most of the stuff like that :)
Pobiega
Pobiega9mo ago
and the answer to your question is... yes! Yes you can show it based on a value from a different class. You just need to get a reference from the right thing to the other right thing somehow.
Tsjech
Tsjech9mo ago
so as i have it now a method gets called if the game is finished i would like to be able to make the method show a label thats inside of the other class
Pobiega
Pobiega9mo ago
Do you understand the basics of classes and variables?
Tsjech
Tsjech9mo ago
yes, classes im a bit worse at but methods and variables i get i have some1 who could explain it to me if i dont understand tho, its just that he doesnt know how to do this either haha
Pobiega
Pobiega9mo ago
so each "screen" or window you have is an instance of a class
Tsjech
Tsjech9mo ago
The parent class has a list that goes through a foreach to set all UI elements to visible, If I could add this label to this list from the method in my child class I would know how to make everything work
Pobiega
Pobiega9mo ago
you need to pass a reference to one of these instances to the other probably the easiest way is if the one with the "game ends" check has a reference to the one with the label, but the other way works too
Tsjech
Tsjech9mo ago
so the board is its own class and everything around it is a parent class
No description
Pobiega
Pobiega9mo ago
right, the board is a UserControl or similar?
Tsjech
Tsjech9mo ago
ye i have this check but its in the child class
Pobiega
Pobiega9mo ago
dont worry about that
Tsjech
Tsjech9mo ago
its a label
Pobiega
Pobiega9mo ago
the board? the entire board is a label?
Tsjech
Tsjech9mo ago
yes
Pobiega
Pobiega9mo ago
wtf?
Tsjech
Tsjech9mo ago
made with paint event args etc ye its how we get thaught dw its a seperate file
Pobiega
Pobiega9mo ago
ok its weird as hell, but we can work with that
Tsjech
Tsjech9mo ago
ye XD
Pobiega
Pobiega9mo ago
so that class has the check for if the game is over
Tsjech
Tsjech9mo ago
yes and it calls the method GameFinished
Pobiega
Pobiega9mo ago
and the "parent", being the form itself, thats the one with the label you want to update when the game ends?
Tsjech
Tsjech9mo ago
yes!
Pobiega
Pobiega9mo ago
okay so, there are two ways we can do this we can do it quick and dirty or using an event which do you prefer
Tsjech
Tsjech9mo ago
ye the event is what i had in mind but i didnt know how to make a custom event
Pobiega
Pobiega9mo ago
easy what .NET version is this btw? just to be sure
Tsjech
Tsjech9mo ago
i had a similar idea by just using like simulate keypress with a key like f20 and then make that subscribe to an event in the parent class lemme check
Pobiega
Pobiega9mo ago
thats not quick and dirty, thats "crime against humanity"
Tsjech
Tsjech9mo ago
how do i check.. LMAO true
Pobiega
Pobiega9mo ago
open your .csproj
Pobiega
Pobiega9mo ago
No description
Pobiega
Pobiega9mo ago
this is a .net 7 project
Tsjech
Tsjech9mo ago
ah ok i see 7.0 wait
Tsjech
Tsjech9mo ago
No description
Pobiega
Pobiega9mo ago
waiting.
Tsjech
Tsjech9mo ago
7.0 windows
Pobiega
Pobiega9mo ago
yeah thats fine
Tsjech
Tsjech9mo ago
idk if its dsifferent
Pobiega
Pobiega9mo ago
its just .net 7 with a "windows-only" restriction because its winforms
Tsjech
Tsjech9mo ago
oh fair
Pobiega
Pobiega9mo ago
its fine so, when the game is finished, do you want to send any info up the chain too like, who won?
Tsjech
Tsjech9mo ago
wait ye i prob should do that i have some stats i want to send
Pobiega
Pobiega9mo ago
okay, we can do that at the same time
Tsjech
Tsjech9mo ago
cool
Pobiega
Pobiega9mo ago
so first we make a class that represents that info thats step 1 a class that should have a property for each value we want to send as part of the event
Tsjech
Tsjech9mo ago
wait like an entire new class?
Pobiega
Pobiega9mo ago
yes.
Tsjech
Tsjech9mo ago
but that does not mean a new file too right? different form a new .class
Pobiega
Pobiega9mo ago
it doesnt have to be, no jsut a new class
Tsjech
Tsjech9mo ago
ok
Pobiega
Pobiega9mo ago
public class GameFinishedEventArgs
{
public int Score { get; init; }
public string Winner { get; init; }
}
public class GameFinishedEventArgs
{
public int Score { get; init; }
public string Winner { get; init; }
}
something like this
Tsjech
Tsjech9mo ago
ye i see
Pobiega
Pobiega9mo ago
I dont know what you want to send, so I just made a string and an int you put whatever you want there. tell me when you have something. it doesnt have to be done now, we can add new stuff to it later
Tsjech
Tsjech9mo ago
ok i see but i can use variables from my board class in this new class?
Pobiega
Pobiega9mo ago
not directly
Tsjech
Tsjech9mo ago
cuz that board class for example holds the amount of pieces per player and who won
Pobiega
Pobiega9mo ago
but we will pass in information from there when we make an instance of this class so you can create properties for that info here we will "copy" the data from the gameboard into this class then send (an instance of) this class to anyone listening on the event
Tsjech
Tsjech9mo ago
ok and the get; init; is so i can use values there?
Pobiega
Pobiega9mo ago
kinda
Tsjech
Tsjech9mo ago
ok ive never used this part of classes so thats cool
Pobiega
Pobiega9mo ago
init means its only settable when we are creating the class, after thats its immutable
Tsjech
Tsjech9mo ago
ok i just have one variable for now thats fine
Pobiega
Pobiega9mo ago
thats fine can you show me the class?
Tsjech
Tsjech9mo ago
isnt that the opposite of what we want?
Pobiega
Pobiega9mo ago
no thats exactly what we want
Tsjech
Tsjech9mo ago
No description
Pobiega
Pobiega9mo ago
only the gameboard can say who won everyone else can read it
Tsjech
Tsjech9mo ago
hmm i see
Pobiega
Pobiega9mo ago
okay, lets follow C# naming conventions so we call it GameFinishedEventArgs
Tsjech
Tsjech9mo ago
ight
Pobiega
Pobiega9mo ago
ok great now, lets go to the board class. ReversiBoard I guess?
Tsjech
Tsjech9mo ago
yep
Tsjech
Tsjech9mo ago
No description
Pobiega
Pobiega9mo ago
we need to make an event in that class
Tsjech
Tsjech9mo ago
thats the method in the board class
Pobiega
Pobiega9mo ago
we will use that soon
Tsjech
Tsjech9mo ago
ah ok
Pobiega
Pobiega9mo ago
first we need an event holder
public event EventHandler<GameFinishedEventArgs>? OnGameFinished;
public event EventHandler<GameFinishedEventArgs>? OnGameFinished;
should do the trick
Tsjech
Tsjech9mo ago
it doesnt like the ? but its ok with it
Pobiega
Pobiega9mo ago
did you disable nullable annotations, you bad person? thats very naughty of you but okay
Tsjech
Tsjech9mo ago
not to my knownledge
Pobiega
Pobiega9mo ago
its "optional" so dont worry too much
Tsjech
Tsjech9mo ago
ah ok i remove it?
Pobiega
Pobiega9mo ago
well, can you tell me the error its giving you with it there?
Tsjech
Tsjech9mo ago
ye sure
Tsjech
Tsjech9mo ago
No description
Pobiega
Pobiega9mo ago
yeah okay thats what I expected that tells me you did indeed disable nullable annotations 🙂 thats okay, so remove the ?
Tsjech
Tsjech9mo ago
could it be with the preset from uni? cuz I deff didnt on purpose i did
Pobiega
Pobiega9mo ago
so, what that line of code does is declare an event that other things can now subscribe to cool but lets wait with that, and fire the event! so when the game ends, you run the GameFinished method, right? or FinishGame
Tsjech
Tsjech9mo ago
yep
Pobiega
Pobiega9mo ago
cool so go in that method
Tsjech
Tsjech9mo ago
i am
Pobiega
Pobiega9mo ago
and fire the event!
Tsjech
Tsjech9mo ago
just like the += thing?
Pobiega
Pobiega9mo ago
nah thats a subscription
Pobiega
Pobiega9mo ago
Handling and Raising Events - .NET
Learn to handle and raise .NET events, which are based on the delegate model. This model lets subscribers register with or receive notifications from providers.
Pobiega
Pobiega9mo ago
I just realized I didnt link this before
Tsjech
Tsjech9mo ago
ah ok ty? so i just call it just like a method?
Pobiega
Pobiega9mo ago
yeah kinda OnGameFinished?.Invoke(this, ...); where ... is a new instance of your event args so we first gotta make one of those and set the winner when we do so
Tsjech
Tsjech9mo ago
No description
Pobiega
Pobiega9mo ago
yeah you missed one of the arguments read my message again
Tsjech
Tsjech9mo ago
oh bruh i read the dots as a pause ok so how2
Pobiega
Pobiega9mo ago
you know how to make an instance of a class, no?
Tsjech
Tsjech9mo ago
well since this conversation my definition of class changed alot so maybe?
Pobiega
Pobiega9mo ago
a class is just anything declared with the class keyword
Tsjech
Tsjech9mo ago
i need to declare it first? with = new() right
Pobiega
Pobiega9mo ago
thats not a declaration thats creating an instance which is what we want to do, but I just want to be clear, that is not a declaration
Tsjech
Tsjech9mo ago
oh so its like a variable?
Pobiega
Pobiega9mo ago
??? what are you smoking? can I have some?
Tsjech
Tsjech9mo ago
as in a variable of time class? type*
Pobiega
Pobiega9mo ago
?
Tsjech
Tsjech9mo ago
nvm
Pobiega
Pobiega9mo ago
no, a class is a class its not a variable public class Steve {} is a class called Steve
Tsjech
Tsjech9mo ago
ye
Pobiega
Pobiega9mo ago
note how classes can exist at top level, inside a namespace methods, variables etc can't
Tsjech
Tsjech9mo ago
mhm
Pobiega
Pobiega9mo ago
only type declarations can be at the top level
Tsjech
Tsjech9mo ago
ah i see
Pobiega
Pobiega9mo ago
a variable is either local (inside a method) or a member (inside a class) so now, we want to make a local variable inside our FinishGame method
Tsjech
Tsjech9mo ago
ight of what type?
Pobiega
Pobiega9mo ago
our event args class
Tsjech
Tsjech9mo ago
ohhh wait i do think i had this all the way back in highschool u mean like Class class = new (Class)?
Pobiega
Pobiega9mo ago
kiiinda [Type] [variableName] = new [Type]();
Tsjech
Tsjech9mo ago
No description
Tsjech
Tsjech9mo ago
so like this
Pobiega
Pobiega9mo ago
yeee
Tsjech
Tsjech9mo ago
ok ye thats what i meant with new ()
Pobiega
Pobiega9mo ago
its almost perfect we just need to tell it who won
Tsjech
Tsjech9mo ago
and i put that info in gfea?
Pobiega
Pobiega9mo ago
so change it to be ... new GameFinishedEventArgs() { Winner = 2;}
Tsjech
Tsjech9mo ago
wait doesnt the ; have to be behind }? especially if im giving it multiple variables also i did
Pobiega
Pobiega9mo ago
ok well, now the event is done and being triggered all we need to do now is make your window actually subscribe to it
Tsjech
Tsjech9mo ago
yeah but on what condition does it get called cuz it should be when FinishGame is being ran or after*
Pobiega
Pobiega9mo ago
it runs when you invoke it I thought that was obvious
Tsjech
Tsjech9mo ago
so the parent class should have an method with object o and GameFinishedEventArgs gfea right?
Pobiega
Pobiega9mo ago
kinda you could do that, and wire it up actually, VS will help us do that so leave it alone for now
Tsjech
Tsjech9mo ago
i thought it only set the variables in the class for every1 to read
Pobiega
Pobiega9mo ago
thats what new GameFinishedEventArgs()... does
Tsjech
Tsjech9mo ago
oh ye right
Pobiega
Pobiega9mo ago
we then pass that to the event with invoke invoking all the event handlers go to the parent class, specially the constructor
Tsjech
Tsjech9mo ago
the constructor is in the board class..
Pobiega
Pobiega9mo ago
you dont know what a constructor is, do you? 🙂
Tsjech
Tsjech9mo ago
uhm.. maybe ok im in the parent class
Pobiega
Pobiega9mo ago
this is getting hard without seeing code. can you screenshare perhaps?
Tsjech
Tsjech9mo ago
i could
Pobiega
Pobiega9mo ago
#dev-vc-1
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.