Rekcs
Rekcs

Reputation: 879

MessageBox is showing multiple times in C#

I am trying to build a code where it will be able to after passing a time span it will display a MessageBox to alert the user that he needs to solve the task scheduled by him.

This is the code I got:

private void timer1_Tick(object sender, EventArgs e)
{
    GetTimeStamp();
}

private void GetTimeStamp()
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        bool bDisplayed = false;
        TimeSpan span = (DateTime.Now - Convert.ToDateTime(row.Cells[2].Value));

        String.Format("{0}, {1}, {2}",
            span.Hours, span.Minutes, span.Seconds);

        row.Cells[3].Value = span.ToString(@"hh\:mm\:ss").Trim();

        if (Convert.ToDateTime(row.Cells[3].Value) >= DateTime.Parse("0:05:00")
            && Convert.ToDateTime(row.Cells[3].Value) <= DateTime.Parse("0:19:59") 
            && row.Cells[1].Value.ToString().Trim() == "4 - Needs to be solved")
        {

                MessageBox.Show("There is one or more tasks to be solved", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Error);
                break;
        }
    }
}

The problem is that the MessageBox is displaying multiple times until it gets the DateTime.Parse("0:19:59"). I just what it to display the message one time.

How can I solve it?

Upvotes: 0

Views: 1430

Answers (2)

Steve
Steve

Reputation: 216273

You can simply ask the DataRows collection if there is a row that satisfies your conditions using IEnumerable.Any and then print your messagebox if true.
No explict for required

bool result = dataGridView1.Rows.Cast<DataGridRow>().Any(row => 
    Convert.ToDateTime(row.Cells[3].Value) >= DateTime.Parse("0:05:00") &&
    Convert.ToDateTime(row.Cells[3].Value) <= DateTime.Parse("0:19:59") &&
    row.Cells[1].Value.ToString().Trim() == "4 - Needs to be solved");
if (result)
{
    // Of course you should stop the timer to avoid another call to this
    // with the same result and with another MessageBox.
    timer1.Enabled = false;
    MessageBox.Show("You have 1 task to be solved!");
}

Upvotes: 1

Andrew Truckle
Andrew Truckle

Reputation: 19097

Just introduce a bool variable. Add it to your class.

private void timer1_Tick(object sender, EventArgs e)
{
    if(!m_bDisplayed)
        GetTimeStamp();
}

private void GetTimeStamp()
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        TimeSpan span = (DateTime.Now - Convert.ToDateTime(row.Cells[2].Value));

        String.Format("{0}, {1}, {2}",
            span.Hours, span.Minutes, span.Seconds);

        row.Cells[3].Value = span.ToString(@"hh\:mm\:ss").Trim();

        if (Convert.ToDateTime(row.Cells[3].Value) >= DateTime.Parse("0:05:00")
            && Convert.ToDateTime(row.Cells[3].Value) <= DateTime.Parse("0:19:59") 
            && row.Cells[1].Value.ToString().Trim() == "4 - Needs to be solved")
        {

                MessageBox.Show("There is one or more tasks to be solved", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Error);
                m_bDisplayed = true;
        }
    }
}

Or, as suggested in the comments:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if (Convert.ToDateTime(row.Cells[3].Value) >= DateTime.Parse("0:05:00")
        && Convert.ToDateTime(row.Cells[3].Value) <= DateTime.Parse("0:19:59") 
        && row.Cells[1].Value.ToString().Trim() == "4 - Needs to be solved")
    {
        MessageBox.Show("You have 1 task to be solved!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
        break;
    }
}

Upvotes: 2

Related Questions