Jump to content

C# - How to avoid an infinte While / If Loop?

Dravinian

I have 10 bits of data copied into a TextBox and delimited by a TAB.

 

If there are less than 7 pieces of data entered (or entered in a format that cannot be .Split by the preset delimiter) I want to throw up an error and send the user back to correct it.


This all happens on an ApplyButton_Click event (I am working on learning better event handlers but it is tricky because the user can button press while still in the textBox) and has to enter 10 bits of data, so it is hard to event handle something that is ongoing when the user can press a button at anytime with the mouse without leaving the textBox.

 

I tried this first:

 

int tooLow;

            int.TryParse(phrase, out tooLow);

            if (tooLow < 7)
            {
                MessageBox.Show("The data has been entered incorrectly. Please try again.");
            }

The form appears to work, as if it is less than 7 bits of data delimited by a Tab, you get the message, and if it is 10 bits of data but not split by Tab, then you get an error as the .Split will only see 1 string and cannot produce substrings.

 

I press ok in the MessageBox, it throws up an exception as a later part of code needs 7 bits of data (taking data from array positions, which don't exist if less than 7 bits of data or not delimited correctly).

 

So I tried:

 

bool broken = true;
int tooLow

while (broken)
            {
                int.TryParse(phrase, out tooLow);
                if (tooLow < 7)
                {
                    MessageBox.Show("The data has been entered incorrectly. Please try again.");
                                                            
                }
                break;
            }

Which again works, but throws up the same error.  Now I added the 'break' because without it, if I press ok in the MessageBox it just pops up again, because it is still below 7, I can't put an if x > 7 as the user never has a chance to correct the mistake as they can never get out of the MessageBox

 

I am stuck in an infinite While/If Loop.

 

I looked at Do loops but they don't help as it creates the same issue.


What I would like to do is end ApplyButton event but leave up the GUI ready for further input and the possibility of another ApplyButton event.

 

There is a lot of user input so I don't want to reset the entire GUI for the error.  I have googled for "cancel ApplyButton" and variants of that, but I have not found a solution.

 

So come seeking your wisdom!

Link to comment
Share on other sites

Link to post
Share on other sites

int tooLow;

            int.TryParse(phrase, out tooLow);

            if (tooLow < 7)
            {
                MessageBox.Show("The data has been entered incorrectly. Please try again.");
                return;
            }

Goddamit.

 

I spent two hours debating whether to post this question, as I felt sure that I could find an answer.

 

Then immediately after pressing enter, I realise that 'return' is the code I need, I just had it in the wrong damn place.

 

What an idiot.  The code is above if anyone happens to search and find this post looking for an answer.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

For anyone that is following along at home....

 

The above won't work either. it will return an error even if you input > 7 bits.  Had a cup of tea and a think, this works:

 

            string phrase = SpreadsheetValue.Text;
            string[] words = phrase.Split('\t');

            int tooLow = words.Count();

            if (tooLow < 7)
            {
                MessageBox.Show("The data has been entered incorrectly. Please try again.");
                return;
            }
            

So hopefully my stupidity may help someone in the future.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×