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
JP
JP•2y ago
code would help, of course broadly, the idea for a toggle, in my mind, is:
private bool IsHidden = false;

private void ShowEverything()
{
IsHidden = false;
// your "show everything" logic
}

private void HideEverything()
{
IsHidden = true;
// your "hide everything" logic
}

public void HandleClick()
{
if (IsHidden) {
ShowEverything();
} else {
HideEverything();
}
}
private bool IsHidden = false;

private void ShowEverything()
{
IsHidden = false;
// your "show everything" logic
}

private void HideEverything()
{
IsHidden = true;
// your "hide everything" logic
}

public void HandleClick()
{
if (IsHidden) {
ShowEverything();
} else {
HideEverything();
}
}
daniel🔥🔥🔥
thanks a lot 🙂
JP
JP•2y ago
gotchu!
daniel🔥🔥🔥
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
JP
JP•2y ago
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)
daniel🔥🔥🔥
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; }
daniel🔥🔥🔥
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
JP
JP•2y ago
like how I got my code have formatting and syntax higlighting/colors
public void DoSomething();
public void DoSomething();
like this
JP
JP•2y ago
I think you want:
if (n >= 10 && n <= 100) {
DoSomething();
}
if (n >= 10 && n <= 100) {
DoSomething();
}
in this example && is logical and
daniel🔥🔥🔥
i know what && is from before but what does >= and <= is that just "between" you can say
JP
JP•2y ago
pretty much, yes >= is "greater than or equal to" (there is also > for just "greater than") <= is "less than or equal to" < exists as well
daniel🔥🔥🔥
ok got it
JP
JP•2y ago
basically, n >= 10 && n <= 100 will be true if n is 10 or bigger and 100 or smaller.
daniel🔥🔥🔥
ok ima check if it works It works! thank you so much bro
JP
JP•2y ago
awesome, happy to help!
daniel🔥🔥🔥
wait just a quick question
JP
JP•2y ago
fire away
daniel🔥🔥🔥
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
JP
JP•2y ago
it would be n > 100 or n >= 101 (so what you have is correct)
daniel🔥🔥🔥
ok thanks 🙂
JP
JP•2y ago
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 faster
daniel🔥🔥🔥
wait wdym
JP
JP•2y ago
e.g. you can do
if (n < 10) {
// do nothing
}
else if (n <= 100) // previously (n >= 10 && n <= 100)
{
Counter1.Left = ClientSize.Height - 235;
}
else if (n <= 1000) // previously (n >= 101 && n <= 1000)
{
Counter1.Left = ClientSize.Height - 245;
}
else if (n <= 10000) // previously would've been (n >= 1001 && n <= 10000)
{
// etc..
}
if (n < 10) {
// do nothing
}
else if (n <= 100) // previously (n >= 10 && n <= 100)
{
Counter1.Left = ClientSize.Height - 235;
}
else if (n <= 1000) // previously (n >= 101 && n <= 1000)
{
Counter1.Left = ClientSize.Height - 245;
}
else if (n <= 10000) // previously would've been (n >= 1001 && n <= 10000)
{
// etc..
}
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 accident
daniel🔥🔥🔥
wait... can you have multiple else if nah no way
JP
JP•2y ago
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!)
daniel🔥🔥🔥
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/
JP
JP•2y ago
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:
int n = 100;
if (n >= 10) {
int digits = (int)Math.Log10(100);
Counter1.Left = ClientSize.Height - (215 + (digits * 10));
}
int n = 100;
if (n >= 10) {
int digits = (int)Math.Log10(100);
Counter1.Left = ClientSize.Height - (215 + (digits * 10));
}
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) 👀
daniel🔥🔥🔥
yo i got a question how do i make a delay
JP
JP•2y ago
if you're in async method, I'd do await Task.Delay(yourDelayInMilliseconds) 1000 milliseconds in a second
daniel🔥🔥🔥
wym with async method
JP
JP•2y ago
public async Task DoSomethingAsync()
{
// some logic
}
public async Task DoSomethingAsync()
{
// some logic
}
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)
daniel🔥🔥🔥
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*
JP
JP•2y ago
and and change it's return type to Task, async methods should near-always return tasks or Task<WhateverTypeYouWereReturningBefore>
daniel🔥🔥🔥
ok so i have to put in task also got it
JP
JP•2y ago
void -> Task int -> Task<int> double -> Task<double>,etc.
daniel🔥🔥🔥
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
JP
JP•2y ago
a thing to note is that async has a tendency to leak What I mean is... if you have
void DoSomething()
{
SendMessageAsync();
Console.WriteLine("Message Sent!");
}

async Task SendMessageAsync()
{
await Task.Delay(200);
Console.WriteLine("send message internal");
/* some logic to send a message to someone */
}
void DoSomething()
{
SendMessageAsync();
Console.WriteLine("Message Sent!");
}

async Task SendMessageAsync()
{
await Task.Delay(200);
Console.WriteLine("send message internal");
/* some logic to send a message to someone */
}
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:
async Task DoSomething()
{
await SendMessageAsync();
Console.WriteLine("Message Sent!");
}

async Task SendMessageAsync()
{
await Task.Delay(200);
Console.WriteLine("send message internal");
/* some logic to send a message to someone */
}
async Task DoSomething()
{
await SendMessageAsync();
Console.WriteLine("Message Sent!");
}

async Task SendMessageAsync()
{
await Task.Delay(200);
Console.WriteLine("send message internal");
/* some logic to send a message to someone */
}
wait, bunked up the example 1 sec
JP
JP•2y ago
OK, updated, that should be correct
daniel🔥🔥🔥
i think i understand a lil
JP
JP•2y ago
Yeah async can take a bit to wrap one's head around. I didn't get it first try. Or second. lol
JP
JP•2y ago
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.
daniel🔥🔥🔥
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
JP
JP•2y ago
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!
daniel🔥🔥🔥
🙂 ok goodnight its 1:30 for me
JP
JP•2y ago
gn!
daniel🔥🔥🔥
i have a question i need help i have an error i cant get it away
JP
JP•2y ago
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

Did you find this page helpful?