When I try to initiate a message box (usually to notify of an exception, but I have tried to just initiate one from the form) the program seems to stall and I am unable to click anything except to close it but it has to be done from the visual studio window.
I have narrowed the problem down to something to do with the pictureBox1_paint event handler as when I remove this code the message boxes start to appear again. Here is the relevant code:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Black, 1))
{
e.Graphics.DrawRectangle(pen, mRect);
}
pictureBox1.Invalidate();
}
The call stack shows that it is on some external code which when expanded seems to me that it is 'painting' the message box to the screen but seems to being held up by something? If the program is paused it shows the NEXT line to be executed is when it returns from whatever it is doing, an example shown below:
private void button2_Click(object sender, EventArgs e)
{
if (GlobalPlot != null)
{
resize = ExpandToBound(GlobalPlot.Size, pictureBox1.Size);
}
else return;
try
{
PlotPixel(resize);
}
catch (System.ArgumentOutOfRangeException index)
{
//this is the next line to execute:
MessageBox.Show(index.Message,"Exception",MessageBoxButtons.OK);
return;
}
}
Not really sure what is going on, but any help much appreciated, thanks.
the messagebox will be there but under your form where you cannot see it. Try this :
MessageBox.Show(this, index.Message,"Exception",MessageBoxButtons.OK);
When the messagebox appears it probably covers the picture, causing it to raise the paint event. In the paint event there is a call to Invalidate() which again raises the paint event, which will call Invalidate() again which again raises the paint event and this continues and makes your form appear frozen.
Remove the Invalidate() from the paint event.