0

I am New In C sharp,

I am using messagebox to display the line. but it is in a loop so the messagebox display more times i want to display it only one time and message box should not be display for the next time.

try{
    using (var sr = File.OpenText("test.txt")){
        string newone = 20110501;//date
        string line;
        while ((line = sr.ReadLine()) != null){
            if (three => one){
                MessageBox.Show("Buy : " + line);
                //Its Displaying more than 50 times i want to displayit only
                //onetime and should dispose for the next Time
            }
        }
    }
} catch { }

Thanks In Advance.

1
  • I'm agree with @SLaks, you should learn and learn and learn. Good lock Commented Sep 25, 2011 at 4:44

3 Answers 3

5

use this:

try {
    using (var sr = File.OpenText("test.txt")) {
        bool flag = true;
        string newone = 20110501;//date
        string line;
        while ((line = sr.ReadLine()) != null) {
            if (flag) {
                MessageBox.Show("Buy : " + line);
                //Its Displaying more than 50 times i want to displayit only
                //onetime and should dispose for the next Time
            }
            flag = false;
        }
    }
} catch { }
Sign up to request clarification or add additional context in comments.

Comments

3

Simple have a boolean flag saying whether the message box has already been shown or not, and use it to guard that action.

Update:

bool shown = false;
...
if( !shown )
{
     MessageBox.Show("Buy : " + line);
     shown = true;
}

Comments

2

You're trying to break; from the loop.

1 Comment

You need to learn the basics of C#. We will not write your program for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.