how do i make it so when i press it this happens and if i do it again another thing happens
ive made a code in winforms so when i press with my left mouse button it hides everything, how do i make so it shows everything if i press once more?
51 Replies
code would help, of course
broadly, the idea for a toggle, in my mind, is:
thanks a lot š
gotchu!
do you know how i can make a if statement between 2 numbers so if the number is 13 and the if statement is 10-100
can you show some surrounding code, and maybe an attempt at what you think you want (even if it shows an error)? I'm not sure I understand what you want atm
you can put code in triple backticks like this to format it like I did
a backtick is this `
(you use 3 in a row at the start of the code, and 3 in a row at the end)
its a cookie clicker esc game so u know
so first we got
int n = 0;
then
private void Boar_Click(object sender, EventArgs e)
{
n += Money;
}
aha, this shows how to do a code block in discord:
https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks#syntax-highlighting
if (n == 10)
{
Counter1.Left = ClientSize.Height - 235;
}
if (n == 100)
{
Counter1.Left = ClientSize.Height - 245;
}
if (n == 1000)
{
Counter1.Left = ClientSize.Height - 255;
}
if (n == 10000)
{
Counter1.Left = ClientSize.Height - 265;
}
if (n == 100000)
{
Counter1.Left = ClientSize.Height - 275;
}
whats a code block
like how I got my code have formatting and syntax higlighting/colors
like this
mkay
i know what && is from before but what does >= and <=
is that just "between" you can say
pretty much, yes
>=
is "greater than or equal to" (there is also >
for just "greater than")
<=
is "less than or equal to" <
exists as wellok got it
basically,
n >= 10 && n <= 100
will be true
if n
is 10 or bigger and 100 or smaller.ok ima check if it works
It works! thank you so much bro
awesome, happy to help!
wait just a quick question
fire away
so my first part is this, wait a sec
if (n >= 10 && n <= 100)
{
Counter1.Left = ClientSize.Height - 235;
}
which works
if (n >= 101 && n <= 1000)
{
Counter1.Left = ClientSize.Height - 245;
}
should i have n >= 100 or n >= 101
it would be
n > 100
or n >= 101
(so what you have is correct)ok thanks š
however, I'll let ya in on a secret. If you use
else
, you can actually skip the n >= 101
entirely, and it will make the code very slightly fasterwait wdym
e.g. you can do
Because the
else if
only runs if the first one didn't run, you don't need to check that n >= 101
, since if it was less than that, an earlier block would've caught it.
edited, dropped en else by accidentwait... can you have multiple else if
nah no way
you definitely can! infinite chains!
(that's not always good, if it gets to long, usually means there is a way to simplify your logic -- but you can do it!)
ok thanks
and one thing before i go
you know my game is cookie clicker esc
or nvm i think i know a way
bye o/
I actually have a last thing I thought of lol
I saw a little pattern here that you can use if you're into quirky little math tricks. (entirely optional, just a thing I noticed with these particular numbers, it might even be slower than what you have, idk)
You're very close to basically always adding the number of digits (times 10) in the number
n
to 215.
e.g.
- 10-99 -> 2 digits -> 215 + (2 x 10) = 235
- 100-999 -> 3 digits -> 215 + (3 x 10) = 245
- 1000-9999 -> 4 digits -> 215 + (4 x 10) = 255
You can get the number of digits of a base-10 number with (int)Math.Log10(theNumber)
,
so you could technically simplify all of this to:
I might be getting too fancy for my own good, though. lol
Also note, what I wrote in this math one is off-by-one compared to what you have (e.g. 1000 gets 255 instead of 245, but 999 correctly gets 245)
šyo i got a question how do i make a delay
if you're in async method, I'd do
await Task.Delay(yourDelayInMilliseconds)
1000 milliseconds in a secondwym with async method
if you're not in an async method, I'd consider changing the method to be async... but if you really can't, then you can use
Thread.Sleep(timeInMs)
to force the thread to stop all work for the duration
sleeping the thread can freeze UIs though (it prevents anything else from using the thread for the duration)i tried thread.sleep but it just made everything stop but clue me more into these async
do i just type asynce infront of a Method and it becomes a async method
asnyc*
and and change it's return type to
Task
, async methods should near-always return tasks
or Task<WhateverTypeYouWereReturningBefore>
ok so i have to put in task also
got it
void
-> Task
int
-> Task<int>
double
-> Task<double>
,etc.mkay
im getting error CS1994 The 'async' modifier can only be used in methods that have a body.
nvm i just didnt put {}
how do i use the lets say DoSomethingAsync()
how do i call it
a thing to note is that
async
has a tendency to leak
What I mean is... if you have
in this case, if I called DoSomething()
, I'd see "Message Sent!" before "send message internal" (this is because just invoking an async method means "I want this done, go do it when you have time"). If I wanted "Message Sent!" to wait for SendMessageAsync()
to finish, I'd have to await
it (which means "don't do anything else in this method until this is done!").
But if I want to await
something inside DoSomething
, that means I need to make DoSomething
async
. This is that "leak" I'm referring to.
So, I'd need this:
wait, bunked up the example
1 seck
OK, updated, that should be correct
i think i understand a lil
Yeah async can take a bit to wrap one's head around. I didn't get it first try. Or second. lol
Here is an example of part 1 you can run:
https://dotnetfiddle.net/JdP1SB
And here is an example of part 2:
https://dotnetfiddle.net/wm7xUj
DoSomething (async flyaway example) | C# Online Compiler | .NET Fiddle
DoSomething (async flyaway example) | Test your C# code online with .NET Fiddle code editor.
[Fork] DoSomethingAsync (async flyaway example) | C# Online Compile...
[Fork] DoSomethingAsync (async flyaway example) | Test your C# code online with .NET Fiddle code editor.
ITS WORKING
its taking away the picturebox if i dont click it in 5 seconds (i want that)
thanks so much u fr my teacher
oh, that's perfect, that situation, its probably perfectly fine to not make the caller
async
so you just need the one
happy to help!š ok goodnight its 1:30 for me
gn!
i have a question
i need help i have an error i cant get it away
I have work rn, you'll have better luck opening a new thread dedicated to that issue in #help or make a post in #help-0
ok