C
C#16mo ago
xLelik

✅ Button and if else

I recently started programming and I don’t understand how to combine Button and if else, please help.
No description
16 Replies
cap5lut
cap5lut16mo ago
if u want on the else branch a condition u need to use else if:
if (...)
{
...
}
else if (...)
{
...
}
if (...)
{
...
}
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
if (...)
{
...
}
else
{
...
}
if (...)
{
...
}
else
{
...
}
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)
if (button1.Enabled == true)
{
...
}
else if (button1.Enabled == false)
{
...
}
if (button1.Enabled == true)
{
...
}
else if (button1.Enabled == false)
{
...
}
is the same as
if (button1.Enabled)
{
...
}
else
{
...
}
if (button1.Enabled)
{
...
}
else
{
...
}
xLelik
xLelikOP16mo ago
thanks
bighugemassive3
bighugemassive316mo ago
are you trying to change the colour when it is pressed/unpressed?
xLelik
xLelikOP16mo ago
Yes
bighugemassive3
bighugemassive316mo ago
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
xLelik
xLelikOP16mo ago
how to implement this
bighugemassive3
bighugemassive316mo ago
you'd handle those 2 events you're already handling the OnClick event i assume based on the function button1_Click
xLelik
xLelikOP16mo ago
I will try to do this
bighugemassive3
bighugemassive316mo ago
if you're using windows forms, there should be an event like "mouse down" in the designer tab somewhere
xLelik
xLelikOP16mo ago
yes I know where it is
xLelik
xLelikOP16mo ago
No description
xLelik
xLelikOP16mo ago
I was able to make it so that control was carried out with two mouse buttons
No description
xLelik
xLelikOP16mo ago
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?
bighugemassive3
bighugemassive316mo ago
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 button
trunksvn
trunksvn16mo ago
Primitive.
Accord
Accord16mo ago
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.

Did you find this page helpful?