C
C#14mo 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
Pobiega14mo ago
is this using winforms? WPF?
Tsjech
TsjechOP14mo ago
windows forms yes
Tsjech
TsjechOP14mo ago
all im using
No description
Pobiega
Pobiega14mo ago
I didn't mean your using statements, I meant what GUI framework are you using 😛 but thats winforms, clearly
Tsjech
TsjechOP14mo 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
Pobiega14mo 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
TsjechOP14mo 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
Pobiega14mo ago
Do you understand the basics of classes and variables?
Tsjech
TsjechOP14mo 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
Pobiega14mo ago
so each "screen" or window you have is an instance of a class
Tsjech
TsjechOP14mo 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
Pobiega14mo 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
TsjechOP14mo ago
so the board is its own class and everything around it is a parent class
No description
Pobiega
Pobiega14mo ago
right, the board is a UserControl or similar?
Tsjech
TsjechOP14mo ago
ye i have this check but its in the child class
Pobiega
Pobiega14mo ago
dont worry about that
Tsjech
TsjechOP14mo ago
its a label
Pobiega
Pobiega14mo ago
the board? the entire board is a label?
Tsjech
TsjechOP14mo ago
yes
Pobiega
Pobiega14mo ago
wtf?
Tsjech
TsjechOP14mo ago
made with paint event args etc ye its how we get thaught dw its a seperate file
Pobiega
Pobiega14mo ago
ok its weird as hell, but we can work with that
Tsjech
TsjechOP14mo ago
ye XD
Pobiega
Pobiega14mo ago
so that class has the check for if the game is over
Tsjech
TsjechOP14mo ago
yes and it calls the method GameFinished
Pobiega
Pobiega14mo ago
and the "parent", being the form itself, thats the one with the label you want to update when the game ends?
Tsjech
TsjechOP14mo ago
yes!
Pobiega
Pobiega14mo 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
TsjechOP14mo ago
ye the event is what i had in mind but i didnt know how to make a custom event
Pobiega
Pobiega14mo ago
easy what .NET version is this btw? just to be sure
Tsjech
TsjechOP14mo 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
Pobiega14mo ago
thats not quick and dirty, thats "crime against humanity"
Tsjech
TsjechOP14mo ago
how do i check.. LMAO true
Pobiega
Pobiega14mo ago
open your .csproj
Pobiega
Pobiega14mo ago
No description
Pobiega
Pobiega14mo ago
this is a .net 7 project
Tsjech
TsjechOP14mo ago
ah ok i see 7.0 wait
Tsjech
TsjechOP14mo ago
No description
Pobiega
Pobiega14mo ago
waiting.
Tsjech
TsjechOP14mo ago
7.0 windows
Pobiega
Pobiega14mo ago
yeah thats fine
Tsjech
TsjechOP14mo ago
idk if its dsifferent
Pobiega
Pobiega14mo ago
its just .net 7 with a "windows-only" restriction because its winforms
Tsjech
TsjechOP14mo ago
oh fair
Pobiega
Pobiega14mo ago
its fine so, when the game is finished, do you want to send any info up the chain too like, who won?
Tsjech
TsjechOP14mo ago
wait ye i prob should do that i have some stats i want to send
Pobiega
Pobiega14mo ago
okay, we can do that at the same time
Tsjech
TsjechOP14mo ago
cool
Pobiega
Pobiega14mo 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
TsjechOP14mo ago
wait like an entire new class?
Pobiega
Pobiega14mo ago
yes.
Tsjech
TsjechOP14mo ago
but that does not mean a new file too right? different form a new .class
Pobiega
Pobiega14mo ago
it doesnt have to be, no jsut a new class
Tsjech
TsjechOP14mo ago
ok
Pobiega
Pobiega14mo 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
TsjechOP14mo ago
ye i see
Pobiega
Pobiega14mo 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
TsjechOP14mo ago
ok i see but i can use variables from my board class in this new class?
Pobiega
Pobiega14mo ago
not directly
Tsjech
TsjechOP14mo ago
cuz that board class for example holds the amount of pieces per player and who won
Pobiega
Pobiega14mo 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
TsjechOP14mo ago
ok and the get; init; is so i can use values there?
Pobiega
Pobiega14mo ago
kinda
Tsjech
TsjechOP14mo ago
ok ive never used this part of classes so thats cool
Pobiega
Pobiega14mo ago
init means its only settable when we are creating the class, after thats its immutable
Tsjech
TsjechOP14mo ago
ok i just have one variable for now thats fine
Pobiega
Pobiega14mo ago
thats fine can you show me the class?
Tsjech
TsjechOP14mo ago
isnt that the opposite of what we want?
Pobiega
Pobiega14mo ago
no thats exactly what we want
Tsjech
TsjechOP14mo ago
No description
Pobiega
Pobiega14mo ago
only the gameboard can say who won everyone else can read it
Tsjech
TsjechOP14mo ago
hmm i see
Pobiega
Pobiega14mo ago
okay, lets follow C# naming conventions so we call it GameFinishedEventArgs
Tsjech
TsjechOP14mo ago
ight
Pobiega
Pobiega14mo ago
ok great now, lets go to the board class. ReversiBoard I guess?
Tsjech
TsjechOP14mo ago
yep
Tsjech
TsjechOP14mo ago
No description
Pobiega
Pobiega14mo ago
we need to make an event in that class
Tsjech
TsjechOP14mo ago
thats the method in the board class
Pobiega
Pobiega14mo ago
we will use that soon
Tsjech
TsjechOP14mo ago
ah ok
Pobiega
Pobiega14mo ago
first we need an event holder
public event EventHandler<GameFinishedEventArgs>? OnGameFinished;
public event EventHandler<GameFinishedEventArgs>? OnGameFinished;
should do the trick
Tsjech
TsjechOP14mo ago
it doesnt like the ? but its ok with it
Pobiega
Pobiega14mo ago
did you disable nullable annotations, you bad person? thats very naughty of you but okay
Tsjech
TsjechOP14mo ago
not to my knownledge
Pobiega
Pobiega14mo ago
its "optional" so dont worry too much
Tsjech
TsjechOP14mo ago
ah ok i remove it?
Pobiega
Pobiega14mo ago
well, can you tell me the error its giving you with it there?
Tsjech
TsjechOP14mo ago
ye sure
Tsjech
TsjechOP14mo ago
No description
Pobiega
Pobiega14mo ago
yeah okay thats what I expected that tells me you did indeed disable nullable annotations 🙂 thats okay, so remove the ?
Tsjech
TsjechOP14mo ago
could it be with the preset from uni? cuz I deff didnt on purpose i did
Pobiega
Pobiega14mo 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
TsjechOP14mo ago
yep
Pobiega
Pobiega14mo ago
cool so go in that method
Tsjech
TsjechOP14mo ago
i am
Pobiega
Pobiega14mo ago
and fire the event!
Tsjech
TsjechOP14mo ago
just like the += thing?
Pobiega
Pobiega14mo ago
nah thats a subscription
Pobiega
Pobiega14mo 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
Pobiega14mo ago
I just realized I didnt link this before
Tsjech
TsjechOP14mo ago
ah ok ty? so i just call it just like a method?
Pobiega
Pobiega14mo 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
TsjechOP14mo ago
No description
Pobiega
Pobiega14mo ago
yeah you missed one of the arguments read my message again
Tsjech
TsjechOP14mo ago
oh bruh i read the dots as a pause ok so how2
Pobiega
Pobiega14mo ago
you know how to make an instance of a class, no?
Tsjech
TsjechOP14mo ago
well since this conversation my definition of class changed alot so maybe?
Pobiega
Pobiega14mo ago
a class is just anything declared with the class keyword
Tsjech
TsjechOP14mo ago
i need to declare it first? with = new() right
Pobiega
Pobiega14mo 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
TsjechOP14mo ago
oh so its like a variable?
Pobiega
Pobiega14mo ago
??? what are you smoking? can I have some?
Tsjech
TsjechOP14mo ago
as in a variable of time class? type*
Pobiega
Pobiega14mo ago
?
Tsjech
TsjechOP14mo ago
nvm
Pobiega
Pobiega14mo ago
no, a class is a class its not a variable public class Steve {} is a class called Steve
Tsjech
TsjechOP14mo ago
ye
Pobiega
Pobiega14mo ago
note how classes can exist at top level, inside a namespace methods, variables etc can't
Tsjech
TsjechOP14mo ago
mhm
Pobiega
Pobiega14mo ago
only type declarations can be at the top level
Tsjech
TsjechOP14mo ago
ah i see
Pobiega
Pobiega14mo 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
TsjechOP14mo ago
ight of what type?
Pobiega
Pobiega14mo ago
our event args class
Tsjech
TsjechOP14mo ago
ohhh wait i do think i had this all the way back in highschool u mean like Class class = new (Class)?
Pobiega
Pobiega14mo ago
kiiinda [Type] [variableName] = new [Type]();
Tsjech
TsjechOP14mo ago
No description
Tsjech
TsjechOP14mo ago
so like this
Pobiega
Pobiega14mo ago
yeee
Tsjech
TsjechOP14mo ago
ok ye thats what i meant with new ()
Pobiega
Pobiega14mo ago
its almost perfect we just need to tell it who won
Tsjech
TsjechOP14mo ago
and i put that info in gfea?
Pobiega
Pobiega14mo ago
so change it to be ... new GameFinishedEventArgs() { Winner = 2;}
Tsjech
TsjechOP14mo ago
wait doesnt the ; have to be behind }? especially if im giving it multiple variables also i did
Pobiega
Pobiega14mo 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
TsjechOP14mo ago
yeah but on what condition does it get called cuz it should be when FinishGame is being ran or after*
Pobiega
Pobiega14mo ago
it runs when you invoke it I thought that was obvious
Tsjech
TsjechOP14mo ago
so the parent class should have an method with object o and GameFinishedEventArgs gfea right?
Pobiega
Pobiega14mo ago
kinda you could do that, and wire it up actually, VS will help us do that so leave it alone for now
Tsjech
TsjechOP14mo ago
i thought it only set the variables in the class for every1 to read
Pobiega
Pobiega14mo ago
thats what new GameFinishedEventArgs()... does
Tsjech
TsjechOP14mo ago
oh ye right
Pobiega
Pobiega14mo ago
we then pass that to the event with invoke invoking all the event handlers go to the parent class, specially the constructor
Tsjech
TsjechOP14mo ago
the constructor is in the board class..
Pobiega
Pobiega14mo ago
you dont know what a constructor is, do you? 🙂
Tsjech
TsjechOP14mo ago
uhm.. maybe ok im in the parent class
Pobiega
Pobiega14mo ago
this is getting hard without seeing code. can you screenshare perhaps?
Tsjech
TsjechOP14mo ago
i could
Pobiega
Pobiega14mo ago
#dev-vc-1
Accord
Accord14mo 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.
Want results from more Discord servers?
Add your server