C
C#10mo 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
cap5lut10mo 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
xLelik10mo ago
thanks
br4kejet
br4kejet10mo ago
are you trying to change the colour when it is pressed/unpressed?
xLelik
xLelik10mo ago
Yes
br4kejet
br4kejet10mo 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
xLelik10mo ago
how to implement this
br4kejet
br4kejet10mo ago
you'd handle those 2 events you're already handling the OnClick event i assume based on the function button1_Click
xLelik
xLelik10mo ago
I will try to do this
br4kejet
br4kejet10mo ago
if you're using windows forms, there should be an event like "mouse down" in the designer tab somewhere
xLelik
xLelik10mo ago
yes I know where it is
xLelik
xLelik10mo ago
No description
xLelik
xLelik10mo ago
I was able to make it so that control was carried out with two mouse buttons
No description
xLelik
xLelik10mo 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?
br4kejet
br4kejet10mo 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
trunksvn10mo ago
Primitive.
Accord
Accord10mo 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.