Youyuan Xu

Youyuan Xu

  • NA
  • 1
  • 0

[confusing]With the same code run in the .netframework sdk[dos] and visual studio 2005[ide],got different results

Mar 2 2006 9:51 AM
I've learned c# for almost one years.yet I have recently encounter a problem that really puzzled me.
Here is the source code :

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

class ImageOpen : Form
{
    protected string strProgName;
    protected string strFileName;
    protected Image image;
    public static void Main()
    {
        Application.Run(new ImageOpen());
    }
    public ImageOpen()
    {
        Text = strProgName = "Image Open";
        ResizeRedraw = true;

        Menu = new MainMenu();
        Menu.MenuItems.Add("&File");
        Menu.MenuItems[0].MenuItems.Add(new MenuItem("&Open",
            new EventHandler(menufileopenonclick), Shortcut.CtrlO));
    }
    void menufileopenonclick(object obj, EventArgs ea)
    {
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.Filter = "All Image Files|*.bmp;*.ico;*.gif;*.jpg;";

        if (dlg.ShowDialog( ) == DialogResult.OK)
        {
            try
            {
                image = Image.FromFile(dlg.FileName);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, strProgName);
                return;
            }
            strFileName = dlg.FileName;
            Text = strProgName + "-" + Path.GetFileName(strFileName);
            Invalidate();

        }
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics gf = e.Graphics;
        if (image != null)
            gf.DrawImage(image, 0, 0);
        //base.OnPaint(e);
    }
}

When I complie it in the .netFramework sdk enivonment,it can run peacefully;when I run the program with the visual studio 2005[ide],it also can run,it browes a Form,then I press the Menuitem as File-----open,it throws out an exception.  What confused me most was that it did not throw out an exception(in fact ,like I said run smoothly)when i complie it in the .netframework sdk enivonment[dos] and run the program in the same way(press File---open).

I hope somebody who can handle the problem can discuss it with us or contact me.
email:
[email protected]
msn: [email protected]

Thank you very much!







Sorry,I can handle it now,I should have added       [STAThread]   
before                                            public static void Main() .