views:

301

answers:

2

So I've been putting this graphics transformation program together and suddenly some change I can't figure out has made the app unresponsive. The menus no longer function, and it's supposed to draw axes and a grid on one of the panels... nothing. Any ideas?

 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 Transformer
{
    public partial class Transformer : Form
    {
     /* Initialize parameters */
     private bool drawAxes = true;
     private bool drawGrid = true;

     private List<ObjectSettings> dispObjects = new List<ObjectSettings>();


     /* 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)
     {
     }

     // Paint graphics panel
     private void splitContainer2_Panel1_Paint(object sender, PaintEventArgs e)
     {
      Rectangle r = splitContainer2.Panel1.ClientRectangle;
      Graphics g = splitContainer2.Panel1.CreateGraphics();
      Pen axisPen = new Pen(Color.Gray, 2.0f);
      Pen gridPen = new Pen(Color.Gray, 1.0f);

      g.Clear(Color.White);

      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 < 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 < 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 stuff */

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

     // ">>" button
     private void availToDispButton_Click(object sender, EventArgs e)
     {
      dispObjectsListBox.Items.Add(availObjectsListBox.SelectedItem);
     }

     // "<<" button
     private void dispToAvailButton_Click(object sender, EventArgs e)
     {
      availObjectsListBox.Items.Add(dispObjectsListBox.SelectedItem);
      dispObjectsListBox.Items.Remove(dispObjectsListBox.SelectedItem);
     }

     // Clear all button
     private void clearAllButton_Click(object sender, EventArgs e)
     {

     }

     // Update preview box
     private void availObjectsListBox_SelectedIndexChanged(object sender, EventArgs e)
     {

     }


    }
}

Thanks!

A: 

If this is happening when loading then obviously the amount of files in the directory could be causing the GUI thread to hang.

Other then that my quick look through only makes me think to check the bools you are using to control drawing and to make sure that the panel you are using the paint event for is actually visible.

You should also check that your timer is actually ticking, and check its interval.

I would also look at using the using statement, or at least a finally block for your dispose. But that isn't what your question is about.

Most of those are obvious and you might have already checked them all before posting here, but I thought I would put up something in case you had missed it. Hopefully I will get a chance to look through in more detail and spot something else.

Glenn Condron
I checked the timer, visibility, etc. All good. The bizarre thing is that the menus will drop down, but clicking on, say, File->Exit does nothing, when there's an event handler right there for it.
justin
Can the designer code cause anything like this?
justin
Is it a problem with the event handler wire ups? Check that the code to wire the event handlers to the events is still there. If the menu and such are working then it means that the GUI thread is not hung so perhaps you lost all your event wireups.
Glenn Condron
Very good! Ah well, I already restarted the project (with some copypasta) but you were right so I'll give you the vote. How on earth would every event wireup stop working?
justin
Which version of visual studio are you using? I remember in 2003 there was some sort of bug that caused all the event wire ups to disappear for no apparent reason. I never did find out why.
Glenn Condron
+1  A: 

Try commenting out (separately) the "load" and "paint" code, see which is the problem.

If the problem is the paint... I wonder - rather than creating your own Graphics, use the one given to you? Namely, e.Graphics. Note that you didn't create this, so it isn't your job to Dispose() it (so don't do that). I would also cache the Pen etc in fields rather than create them each time. Note that if you do create a Pen (etc) in a method, then using is a better way to Dispose() it.

There is also a foreach comment in the paint code that suggests something has been removed - this may be relevant to the problem...

Marc Gravell
Neither the load nor the paint seems to be the problem... even the menu event handlers are affected. They drop down and look selected, but nothing doing when I click them
justin