C
C#ā€¢8mo ago
hugheselite

āœ… 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
hugheselite
hugheseliteā€¢8mo ago
BlazeBin - xpjzxfmjjeik
A tool for sharing your source code with the world!
hugheselite
hugheseliteā€¢8mo ago
else function is right at the bottom I feel i'm being real stupid here
Thinker
Thinkerā€¢8mo ago
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
hugheselite
hugheseliteā€¢8mo ago
the one at the very bottom { ErrorHandlingWindow.Text = InvalidCommandMessage; return; }
Thinker
Thinkerā€¢8mo ago
Only situation in which that could execute is if none of the above cases succeeded
hugheselite
hugheseliteā€¢8mo ago
I realised this when I'd implemented nearly everything haha šŸ˜¦
Thinker
Thinkerā€¢8mo ago
Have you tried debugging the code? The most obvious answer would be that CommandSplit[0] is not what you're expecting it to be
hugheselite
hugheseliteā€¢8mo ago
it's still executing even if a condition is working for example
hugheselite
hugheseliteā€¢8mo ago
No description
hugheselite
hugheseliteā€¢8mo ago
even though the circle was drawn on th bitmap fine, still outputs the error message
ZacharyPatten
ZacharyPattenā€¢8mo ago
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
hugheselite
hugheseliteā€¢8mo ago
i've posted the full code in the thread šŸ™‚
ZacharyPatten
ZacharyPattenā€¢8mo ago
that isn't the full code
hugheselite
hugheseliteā€¢8mo ago
this one
ZacharyPatten
ZacharyPattenā€¢8mo ago
we cannot run that
Thinker
Thinkerā€¢8mo ago
that's one file
hugheselite
hugheseliteā€¢8mo ago
ohh got you okay sure yes my bad one moment
hugheselite
hugheseliteā€¢8mo ago
Could you please let me know if this is downloadable? thank you
ZacharyPatten
ZacharyPattenā€¢8mo ago
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 handler
ZacharyPatten
ZacharyPattenā€¢8mo ago
No description
ZacharyPatten
ZacharyPattenā€¢8mo ago
sorry I know that is low quality of a screenshot, but hopefully you see can see where the two bindings are
hugheselite
hugheseliteā€¢8mo ago
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
ZacharyPatten
ZacharyPattenā€¢8mo ago
I saw this and I pretty much immediately knew the issue:
No description
hugheselite
hugheseliteā€¢8mo ago
awesome good to know going forward
ZacharyPatten
ZacharyPattenā€¢8mo ago
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
hugheselite
hugheseliteā€¢8mo ago
right on, well thank you again for you time, have a great day !close
Accord
Accordā€¢8mo ago
Closed!
Want results from more Discord servers?
Add your server
More Posts