Note that there are some explanatory texts on larger screens.

plurals
  1. POC# / Windows Forms: form's paint code not being executed
    text
    copied!<p>I posted a similar but less specific question a couple hours ago, but the circumstances have changed. I'm working on a program that transforms graphics, presenting them in a panel in the upper left of the form. It was painting fine earlier, but now isn't and I can't undo, load old version, etc. Previously, the app was unresponsive even to events in the menus, as well as painting. I started a new project and got it up and running, the menus work and I brought over the buttons and stuff. But it still won't paint the axes and gridlines in the panel. I put breakpoints in both the main forms's paint handler and the splitContainer2_Panel1_Paint handler that should be doing the work, but the code here isn't even being executed. I have a timer that's active and invalidating the whole form every 100 ms, so why would the paint event handler not be called? Help?</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace TransformerA { public partial class Transformer : Form { /* Initialize parameters */ private bool drawAxes = true; private bool drawGrid = true; private List&lt;ObjectSettings&gt; dispObjects = new List&lt;ObjectSettings&gt;(); /* Initialize form */ public Transformer() { InitializeComponent(); } private void Transformer_Load(object sender, EventArgs e) { // Populate available objects listbox string currentDir = Directory.GetCurrentDirectory(); string[] fileEntries = Directory.GetFiles(currentDir + @"\Objects"); foreach (string s in fileEntries) { int start = s.LastIndexOf(@"\"); int end = s.LastIndexOf(@"."); availObjectsListBox.Items.Add(s.Substring(start + 1, end - start - 1)); } // end foreach } /* Paint graphics */ // Paint main form private void Transformer_Paint(object sender, PaintEventArgs e) { splitContainer2_Panel1_Paint(sender, e); } // Paint graphics panel private void splitContainer2_Panel1_Paint(object sender, PaintEventArgs e) { Rectangle r = splitContainer2.Panel1.ClientRectangle; //Graphics g = splitContainer2.Panel1.CreateGraphics(); Graphics g = e.Graphics; Pen axisPen = new Pen(Color.Gray, 2.0f); Pen gridPen = new Pen(Color.Gray, 1.0f); g.Clear(Color.Blue); if (drawAxes) { g.DrawLine(axisPen, r.Left + 0.5f * r.Width, r.Top, r.Left + 0.5f * r.Width, r.Bottom); g.DrawLine(axisPen, r.Left, r.Top + 0.5f * r.Height, r.Right, r.Top + 0.5f * r.Height); } if (drawGrid) { // Vertical lines int xVal = 0; int xCenter = r.Width / 2; g.DrawLine(gridPen, xCenter, r.Top, xCenter, r.Bottom); for (int i = 0; i &lt; 10; i++) { xVal += r.Width / 20; g.DrawLine(gridPen, xCenter + xVal, r.Top, xCenter + xVal, r.Bottom); g.DrawLine(gridPen, xCenter - xVal, r.Top, xCenter - xVal, r.Bottom); } // Horizontal lines int yVal = 0; int yCenter = r.Height / 2; g.DrawLine(gridPen, r.Left, yCenter, r.Right, yCenter); for (int i = 0; i &lt; 10; i++) { yVal += r.Height / 20; g.DrawLine(gridPen, r.Left, yCenter + yVal, r.Right, yCenter + yVal); g.DrawLine(gridPen, r.Left, yCenter - yVal, r.Right, yCenter - yVal); } } // foreach object in displayed objects // keep list of displayed objects and their settings (make struct) g.Dispose(); axisPen.Dispose(); gridPen.Dispose(); } /* File menu */ private void saveImageToolStripMenuItem_Click(object sender, EventArgs e) { } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Close(); } /* Options menu */ private void axesOnoffToolStripMenuItem_Click(object sender, EventArgs e) { if (drawAxes == true) drawAxes = false; else drawAxes = true; } private void gridOnoffToolStripMenuItem_Click(object sender, EventArgs e) { if (drawGrid == true) drawGrid = false; else drawGrid = true; } /* Help menu */ private void helpToolStripMenuItem_Click(object sender, EventArgs e) { AboutBox dlg = new AboutBox(); dlg.ShowDialog(); } /* Other */ private void timer1_Tick(object sender, EventArgs e) { Invalidate(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload