✅ else statement executing regardless of whether I want it to or not, windows form app
I've created a simple windows form app to create shapes on a bitmap, I have a function that gives an error if the user inputs an invalid input. it's currently executing even if the input is valid..
I'm pretty sure it's a simple fix would just appreciate an extra pair of eyes on this one lol
28 Replies
BlazeBin - xpjzxfmjjeik
A tool for sharing your source code with the world!
else function is right at the bottom
I feel i'm being real stupid here
There are a lot of else cases here, which one do you mean?
Also this would be an ideal situation to use a switch statement
the one at the very bottom
{
ErrorHandlingWindow.Text = InvalidCommandMessage;
return;
}
Only situation in which that could execute is if none of the above cases succeeded
I realised this when I'd implemented nearly everything haha 😦
Have you tried debugging the code?
The most obvious answer would be that
CommandSplit[0]
is not what you're expecting it to beit's still executing even if a condition is working
for example
even though the circle was drawn on th bitmap fine, still outputs the error message
if you share the full code (preferably with a github repo link) then we could help you. but without the full code it is hard for us to help other than encouraging you to put breakpoints and debug
i've posted the full code in the thread 🙂
that isn't the full code
this one
we cannot run that
that's one file
ohh got you
okay sure yes my bad
one moment
Could you please let me know if this is downloadable?
thank you
it is. I tested "
circle 25
" and it seemed to work. what input are you testing that is still hitting the else
that you don't want it to?
o wait
I was testing the button
when you press Enter is when it is running the code twice
One moment
SingleCommandLine.KeyDown += singleCommandLine_KeyDown_1;
SingleCommandLine.KeyDown += singleCommandLine_KeyDown_1;
you are binding your event handler twice
so every time you press enter it is currently runnign the code twice
in your Form1.Designer.cs
class is where that is happening
one on line 165 and one on line 52
you clear the text box during the code, so if you run the code twice, the first run will clear the text box so that the second time it runs it will be blank and therefore cause the error you are seeing
so in short... you should remove one of the double event bindings
you should only have one binding for that event handlersorry I know that is low quality of a screenshot, but hopefully you see can see where the two bindings are
There is currently an IT guy installing a new router in my office
In about 20 mins I'm going to go an edit my code
Your logic sounds 100% correct here to me so thank you vm for your time
You are completely correct and fixed the issue
If you don't mind sharing how you went about figuring out the problem? that'd be wonderful
I saw this and I pretty much immediately knew the issue:
awesome good to know going forward
there are times when you do want mutiple UI elements to bind to the same handler... but it is not typical for KeyDown events specifically
right on, well thank you again for you time, have a great day
!close
Closed!