C
C#15mo ago
pat

✅ Help with basic guessing game

Hi guys. I just started programming classes last week and it's all very new to me. This is a guessing game. You have ten tries, the lights on each side tell you if the number you're guessing is higher or lower than the hidden number. I'm having a hard time even getting started. I have to get the button "Accepter" to work and "Nombre d"essais" has to have a max value of 10 display. I tried; vsb.NbEssais.Value = 10; But that just makes my button become the number 10 with the accept button serving no purpose. I'm trying to show the properties in my screenshot. The button's name is "btnAccepter". I'll post the code page too but I doubt it's any help if you don't speak French.
46 Replies
phaseshift
phaseshift15mo ago
and "Nombre d"essais" has to have a max value of 10 display. I tried; vsb.NbEssais.Value = 10; But that just makes my button become the number 10
You're using a button as a label? I don't get it. Setting the value of a label is quite simple
pat
patOP15mo ago
It is a label. I use the word button loosely here, sorry
phaseshift
phaseshift15mo ago
so the label has to shows how many tries left. So the label value needs to be assigned (changed) each time the button is pressed
pat
patOP15mo ago
The label has to show how many tries you have done, with a maximum of 10 tries
phaseshift
phaseshift15mo ago
ok. So start it at "0", when you push the button, update the label value. That's the general process
pat
patOP15mo ago
I still can't seem to get this started for the life of me. Everything I try fails.
phaseshift
phaseshift15mo ago
Nobody can help without details
pat
patOP15mo ago
Every time the "Accepter" button is pressed, the "Nombre d'essais" value has to go up by one to a maximum of 10. I can't figure out how the two link together. The only line I've managed to run without error is vsiNbEssais.Value = 10; But that only sets the value of "Nombre d'essais" to 10 without the button doing anything or the number ever changing. I'm guessing I have to do an if statement but again I get nothing but errors. For example vsiNbEssais.Value = 0; if (btnAccepter = true) { vsiNbEssais.Value = (vsiNbEssais.Value +1) } This is literally from my third class and it's all very overwhelming.
Angius
Angius15mo ago
Keep the number in a field or a property, and when the click method runs, increment it and change the label text accordingly
phaseshift
phaseshift15mo ago
Yeah, I think you're a bit ahead of yourself. Do you know how buttons and events and event handlers interact?
Angius
Angius15mo ago
Quick and dirty pseudocode example:
private int _clicks = 0;
public void OnButtonClick()
{
_clicks++;
SomeLabel.Text = $"Clicked {_clicks} times"
}
private int _clicks = 0;
public void OnButtonClick()
{
_clicks++;
SomeLabel.Text = $"Clicked {_clicks} times"
}
pat
patOP15mo ago
Nope, I have no idea how those work. The thing is, I know it's simple code to get it to work because well we're on our third class. For example, this was the previous problem (which I managed to solve yay)
pat
patOP15mo ago
Alright I'll ask one last time and if I get no answer I will go to school early and ask the prof. All I'm trying to do is get the counter to start going up every time I click the accept button. I really cannot figure out this logic for the life of me. Pic related is a terrible attempt and the properties of them
SinFluxx
SinFluxx15mo ago
Every time the "Accepter" button is pressed, the "Nombre d'essais" value has to go up by one to a maximum of 10. I can't figure out how the two link together.
Well you have your btnAccepter_Click method yes? Which is called whenever you click the button You found that doing:
vsiNbEssais.Value = 10;
Just sets the value to 10 straight away, so obviously you don't want to do exactly, you instead want to add 1 to the value instead
pat
patOP15mo ago
I'm not sure what method refers to but yes that's my issue
SinFluxx
SinFluxx15mo ago
private void btnAccepter_Click(object sender, EventArgs e)
{

}
private void btnAccepter_Click(object sender, EventArgs e)
{

}
that is a method, and any code inside that method will run when you click the btnAccepter button
pat
patOP15mo ago
We learned none of this so I don't think it's that. We pretty much learned true/false and if/else so far. That's kind of why I'm tripping balls here
SinFluxx
SinFluxx15mo ago
I'm guessing your teacher's supplied all the above code and given you the TODO comments to indicate where your code should be added and what it should be doing, so that you don't strictly need to worry about all those existing parts just yet (I'm not sure my french is basically non existent...)? so in that method you need to: 1) Check if the Value of vsiNbEssais is less than 10 2) If it is, increase it by 1
pat
patOP15mo ago
Yeah that soudns about right
SinFluxx
SinFluxx15mo ago
ok, so you said you've been over if/else statements, so you should know the format of what you're entering and you were given above a line for how to increase the value of something by one: myValue++; or you can also do: myValue += 1; Does that make sense?
pat
patOP15mo ago
pat
patOP15mo ago
Am I getting there? Again it just sets it to 1
SinFluxx
SinFluxx15mo ago
Have you clicked the button more than once? Is that in the right method, should it not be inside btnAccepter_Click? In this case you also don't need the else statement
pat
patOP15mo ago
The program starts with the number of tries already being 1 and clicking accept does nothing
SinFluxx
SinFluxx15mo ago
yes I think because you have your code inside the wrong method, shouldn't it be inside btnAccepter_Click ? Try moving it there instead
pat
patOP15mo ago
What do you mean? We haven't learned anything about click. The accept button itself works right now. When I click it it just does nothing, but it's functional. I have to make the tries number go up when it's clicked
SinFluxx
SinFluxx15mo ago
you have the method btnAccepter_Click in your file, yes? Can you find it now? it's in the first screenshot you sent
SinFluxx
SinFluxx15mo ago
SinFluxx
SinFluxx15mo ago
I'm saying, put that new code you've just written inside that method instead
pat
patOP15mo ago
He put us a TO-DO list in order. What you highlighted is the last step (TO-DO 6) where the lightbulb lights up according to if the number you are guessing is higher or lower than the number that is hidden. What I'm trying to do is step 1 which is just initializing the number of tries counter None of this code is mine
SinFluxx
SinFluxx15mo ago
I know, I just think it's not been very clear which part you're trying to work on So what exactly are you trying to do right now?
pat
patOP15mo ago
One minute I will get his demo exe running and record what I'm trying to do
pat
patOP15mo ago
Ok so, here is the full thing running. All I want to do right now is step 1, which is to get the counter going up (to a max of 10 tries) every time the Accpeter is pressed
Angius
Angius15mo ago
vsinbessais is the label that displays the count?
pat
patOP15mo ago
Yes
Angius
Angius15mo ago
Labels are usually text, not numbers You can't exactly increment a string I'd store the number of clicks in a field
private int _clicks = 0;
private void btnAccepter_Click(object sender, EventArgs e)
{
if (clicks < 10)
{
_clicks += 1;
}
vsinbessais.Value = _clicks;
}
private int _clicks = 0;
private void btnAccepter_Click(object sender, EventArgs e)
{
if (clicks < 10)
{
_clicks += 1;
}
vsinbessais.Value = _clicks;
}
pat
patOP15mo ago
I feel stupid and this is not getting through to me. We really have not learned anything advanced so idk wtf is going on. Sorry for wasting your time fellas. I'll try to see a professor today. I'll update this later I guess if I figure it out.
pat
patOP15mo ago
Starts and stuck on 1 with this
Angius
Angius15mo ago
I just told you, you cannot increment the value of a label Furthermore, label has the Text property to set what it shows Not Value So... is it even a label?
pat
patOP15mo ago
Wdym? Nombre d'essais is VisualInt
Angius
Angius15mo ago
tf is VisualInt?
pat
patOP15mo ago
I have no idea man. I'm sorry but thanks anyways.
SinFluxx
SinFluxx15mo ago
I don't know why we're assuming it's some text based control when there's no intellisense showing up when vsiNbEssais.Value is used, and it successfully changes the control content, it's clearly some sort of numeric control, so I still say try putting the code you've written inside the btnAccepter_Click method
pat
patOP15mo ago
You were absolutely right. I didn’t understand what you meant but after it was explained to me, yeah what you were saying was exactly it. Thanks again.
SinFluxx
SinFluxx15mo ago
$close
MODiX
MODiX15mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server