ます’s Blog - どうでもいい記事100選

どうでもいい記事100選

GUIアプリケーション(OpenFileDialog)

最近はC#(正確には.NET)でGUIアプリケーションを作って現実逃避している毎日です。
少ない記述で、GUIアプリケーションがモリモリ作成できるので「うひゃっ」って感じで楽しいです。時代は変わったねぇ。
幼い頃に(勝手に)思い描いていたプログラマ像というのが、GUIアプリケーションを作れる人。後、ゲームを作れる人。。。っていうのはどうでも良くて。
色々と模索している最中ですが、一点だけ解決できない問題が出てきました。
OpenFileDialogを使っているのですが、「ファイルの種類」タブを変更しても切り替わってくれません。

f:id:masugata:20081119165333p:image
「ファイルの種類」タブを変更する事によって内容が変化するハズなのですが。。。真っ白な状態になってしまいます。_| ̄|○
で。面白い事に「ファイル名」を空にして「開く」ボタンを押すと(何故か)期待した動作になります(一覧が描画される)。ありえねぇ。。。
更に興味深いのはVisual Studio 2008 Express Editionを使って同じようなGUIアプリケーションを(ウィザード形式でポコポコと)作ると、そっちは期待した通りに動くんだよねぇ。。。何が違うのだろうか。
ツールで作るのと自前で作るのでは構成がカナリ違っているのですが、核の部分は同じハズだと思うのに。。。この差が分からん。誰か分かる人、教えてください。_| ̄|○


ソースは以下の通りです。神の光臨に期待したい。。。

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

namespace example
{
  class example
  {
    public static void Main( )
    {
      Application.EnableVisualStyles( );
      Application.SetCompatibleTextRenderingDefault( false );
      Application.Run( new exampleForm( ) );
    }
  }

  class exampleForm : Form
  {
    public exampleForm( )
    {
      this.Width         = 200;
      this.Height        = 200;
      this.Text          = "サンプル";
      this.BackColor     = Color.White;
      this.StartPosition = FormStartPosition.CenterScreen;

      Button button   = new Button( );
      button.Location = new Point( 65, 65 );
      button.Size     = new Size( 50, 30 );
      button.Text     = "選択";

      button.Click   += new EventHandler( this.buttonClick );
      Controls.Add( button );
    }

    void buttonClick( object sender, EventArgs e )
    {
      OpenFileDialog fileTarget = new OpenFileDialog( );
      fileTarget.Title          = "選択してください";
      fileTarget.Filter         = "テキスト(*.txt;*.csv;*.log)|*.txt;*.csv;*.log|すべて(*.*)|*.*";

      if( fileTarget.ShowDialog( ) == DialogResult.OK )
        {
           MessageBox.Show( fileTarget.FileName,
                            "選択ファイル",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information );
        }

      fileTarget.Dispose( );
    }
  }
}

コマンド・プロンプトで以下のように実行(正確にはexample.batを用意して、以下を定義してダブル・クリック)。

cd "C:\path\to\target dir"
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe" /target:winexe example.cs

ping -n 2 localhost
example.exe