C
C#

how do i make it so when i press it this happens and if i do it again another thing happens

how do i make it so when i press it this happens and if i do it again another thing happens

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

Looking for more? Join the community!

C
C#

how do i make it so when i press it this happens and if i do it again another thing happens

Join Server
Want results from more Discord servers?
Add your server
Recommended Posts
Can't access object attributes using X.PagedListI have a search field that returns a list of movies. It works fine if I reference the model as @MoviTryin to install scriptscs but cinst is not a recognized commandI'm trying to install scriptcs but cinst isn't a recognized command. I have uninstalled and reinstalHow do I utilize Identity Framework and AspNetUser Table?Hello, I am building an MVC app where people will need to sign up. They can then upload stuff to thāœ… Is there a way to run a commands method inside of another command? (DSharpPlus)Title explains it. ```c# #region RandomImage [SlashCommand("randomimage", "Visa en rhelpGood evening, I have a monolithic application completed, can someone help me migrate a monolithic apProject structure (MVC API, microservice arch.)https://github.com/classy-giraffe/BookshopHow can I insert my sortedlist into a listbox?I am new to C# and currently using a sortedlist<string, int> in order to store values that I need toHow to map Entity Framework entities containing circular references to DTOs?When querying data with Entity Framework, the result data appears to contain circular references. (āœ… Typeorm doesnt get the name from entityI need that typeorm get the ```name: "noInfoMILeadsToDistribute"``` inside Entity, but it wants to gProcessing files as fast as possibleHi everyone! I have a question that Iā€™m sure you can help me with. Iā€™m making a library that needs tāœ… [SOLVED] Does the 3rd line of code hold memory as a reference or does it hold the actual value?ayooo, quick question. pretty simple. Book is a custom class just so you know.... ```csharp Book boRequestsHello, i have never done something that i will say before, how can i send a request to an API Url, gWhy is my grid on Line 28 not calling back to my other script called Grid?āœ… Visual Studio 2022 Failing to Create C# ProjectWhat's happening here?āœ… Help with creating shapes on a windows form appI have created a windows form app to display shapes on a bitmap, my circle function works fine but mNo inbuilt functionsHello, im a year 1 student so my knowledge is not that massive. So i have this task for university wSignalR initial connection takes way too long (logs included in comments)Hey there. So I have two versions of the same application, using essentially the same code. One of āœ… How to make DontDestroyOnLoad work for only single scene?Hello! I have a problem. In my game, when I am on my Main Menu Scene and trying to load Main Game ScUI is lagging while scrolling a listViewHi, I wanted to create an application that retrieves data from the API and displays it. I'm having tHow can i fix this error code?he's giving me an error code for private?