C
C#ā€¢13mo ago
fat brown dog

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ā€¢13mo 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();
}
}
fat brown dog
fat brown dogOPā€¢13mo ago
thanks a lot šŸ™‚
JP
JPā€¢13mo ago
gotchu!
fat brown dog
fat brown dogOPā€¢13mo ago
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ā€¢13mo 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)
fat brown dog
fat brown dogOPā€¢13mo ago
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; }
fat brown dog
fat brown dogOPā€¢13mo ago
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ā€¢13mo ago
like how I got my code have formatting and syntax higlighting/colors
public void DoSomething();
public void DoSomething();
like this
fat brown dog
fat brown dogOPā€¢13mo ago
mkay
JP
JPā€¢13mo ago
I think you want:
if (n >= 10 && n <= 100) {
DoSomething();
}
if (n >= 10 && n <= 100) {
DoSomething();
}
in this example && is logical and
fat brown dog
fat brown dogOPā€¢13mo ago
i know what && is from before but what does >= and <= is that just "between" you can say
JP
JPā€¢13mo 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
fat brown dog
fat brown dogOPā€¢13mo ago
ok got it
JP
JPā€¢13mo ago
basically, n >= 10 && n <= 100 will be true if n is 10 or bigger and 100 or smaller.
fat brown dog
fat brown dogOPā€¢13mo ago
ok ima check if it works It works! thank you so much bro
JP
JPā€¢13mo ago
awesome, happy to help!
fat brown dog
fat brown dogOPā€¢13mo ago
wait just a quick question
JP
JPā€¢13mo ago
fire away
fat brown dog
fat brown dogOPā€¢13mo ago
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ā€¢13mo ago
it would be n > 100 or n >= 101 (so what you have is correct)
fat brown dog
fat brown dogOPā€¢13mo ago
ok thanks šŸ™‚
JP
JPā€¢13mo 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
fat brown dog
fat brown dogOPā€¢13mo ago
wait wdym
JP
JPā€¢13mo 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
fat brown dog
fat brown dogOPā€¢13mo ago
wait... can you have multiple else if nah no way
JP
JPā€¢13mo 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!)
fat brown dog
fat brown dogOPā€¢13mo ago
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ā€¢13mo 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) šŸ‘€
fat brown dog
fat brown dogOPā€¢13mo ago
yo i got a question how do i make a delay
JP
JPā€¢13mo ago
if you're in async method, I'd do await Task.Delay(yourDelayInMilliseconds) 1000 milliseconds in a second
fat brown dog
fat brown dogOPā€¢13mo ago
wym with async method
JP
JPā€¢13mo 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)
fat brown dog
fat brown dogOPā€¢13mo ago
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ā€¢13mo ago
and and change it's return type to Task, async methods should near-always return tasks or Task<WhateverTypeYouWereReturningBefore>
fat brown dog
fat brown dogOPā€¢13mo ago
ok so i have to put in task also got it
JP
JPā€¢13mo ago
void -> Task int -> Task<int> double -> Task<double>,etc.
fat brown dog
fat brown dogOPā€¢13mo ago
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ā€¢13mo 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
fat brown dog
fat brown dogOPā€¢13mo ago
k
JP
JPā€¢13mo ago
OK, updated, that should be correct
fat brown dog
fat brown dogOPā€¢13mo ago
i think i understand a lil
JP
JPā€¢13mo 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ā€¢13mo 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.
fat brown dog
fat brown dogOPā€¢13mo ago
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ā€¢13mo 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!
fat brown dog
fat brown dogOPā€¢13mo ago
šŸ™‚ ok goodnight its 1:30 for me
JP
JPā€¢13mo ago
gn!
fat brown dog
fat brown dogOPā€¢13mo ago
i have a question i need help i have an error i cant get it away
JP
JPā€¢13mo 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
fat brown dog
fat brown dogOPā€¢13mo ago
ok
Want results from more Discord servers?
Add your server