✅ Button and if else
I recently started programming and I don’t understand how to combine Button and if else, please help.
16 Replies
if u want on the else branch a condition u need to use else if:
also
=
is to assign a value, for comparison u need to use ==
and regarding ur else branch u do not need the else if, because button1.Enabled
can only be true or false and nothing else, so just the else without any condition would be enough in this case
and because the condition results in a bool
value anyway, u do not need to check for button1.Enabled == true
u can simply write if (button1.Enabled)
basically when u replace button1.Enabled
with its actual value it would become (lets assume its enabled for this example)
if (true == true)
this more simplified would be if (true)
is the same as
thanks
are you trying to change the colour when it is pressed/unpressed?
Yes
i think you have to handle the mouse release event too
pressed*
the click event is usually called on mouse release
so that a button won't be "clicked" unless the mouse is over it
OnMouseDown set the background to red, OnClick set to green
how to implement this
you'd handle those 2 events
you're already handling the OnClick event i assume based on the function button1_Click
I will try to do this
if you're using windows forms, there should be an event like "mouse down" in the designer tab somewhere
yes I know where it is
I was able to make it so that control was carried out with two mouse buttons
how to control only the left mouse button?
how to make it so that when you press it, one action is performed, and when you press the same button again, another action is performed?
you could store an int called
ClickedCounter
and increment it at the end of the function
if it's 0 then they clicked it once, if it's 1 then they clicked again (and you can set it back to 0 to reset the process)
well you could probably use a bool called HasClickedOnce
instead, but at that point, you're replicating the behaviour of the built in toggle buttonPrimitive.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.