shaeffer
shaeffer

Reputation: 13

Make a message box only pop up once in c#

I have attached my code below. I am trying to get the message box to go away after one pop up until another action within the application triggers it.

using (VIModel vi = new VIModel())
{
    var VideoFiles = vi.VI_VIDEO_FILES.Where(a => a.SEGMENT_ID == segmentId);
    foreach (var vf in VideoFiles)
    {
        string location = vf.URL;
        location = location.Replace('/', '\\');

        string[] smain = location.Split('\\');
        string unamemain = smain.Last().ToString();


        string fileToCopySource = Path.Combine(inputDirectory, location);
        string fileToCopyDestination = Path.Combine(outputDirectory, unamemain);

        foreach (char c in fileToCopySource)
        {
            if (fileToCopySource.Contains(c))
            {
                // notify user that the inputDirectory isn't valid
                MessageBox.Show("FOO");
            }
        }
        if (!File.Exists(fileToCopySource))
        {
            // notify user that file doesn't exist
            MessageBox.Show("FOO");
        }

        //File.Copy(inputDirectory + location, outputDirectory + unamemain, true);
        File.Copy(fileToCopySource, fileToCopyDestination, true);
        lbConsole.Items.Add("Moved " + location + " from: " + inputDirectory + " to " + fileToCopyDestination);
    }
}

Upvotes: 0

Views: 1040

Answers (1)

rory.ap
rory.ap

Reputation: 35260

Your code is alerting multiple times -- once for every "alertable" condition it finds, i.e. input directory not valid. What you need to do is record the fact that anything caused the condition that will need to be alerted, and then alert only once, outside of the loop:

bool needAlert = false;

foreach (char c in fileToCopySource)
{
    if (fileToCopySource.Contains(c))
    {
        needAlert = true;
        break; // no more looping needed.
    }
}

if(needAlert) MessageBox.Show("FOO"); // notify user that the inputDirectory isn't valid

Upvotes: 3

Related Questions