2014-04-15 176 views
4

我有這個簡單的代碼:打開文件對話框不顯示

private void buttonOpen_Click(object sender, EventArgs e) 
{ 
    if (openFileDialog1.ShowDialog() == DialogResult.OK) 
    { 
     textBox2.Text = openFileDialog1.FileName; 
    } 
} 

當我運行程序的形式不顯示和調試模式退出。

在輸出視圖中寫入:程序'[4244] openfiledialog.vshost.exe:託管(v4.0.30319)'已退出,代碼爲1073741855(0x4000001f)。

我有Visual Studio 2010 Professional。

編輯:form1.designer.cs

 private void InitializeComponent() 
    { 
     this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); 
     this.buttonOpen = new System.Windows.Forms.Button(); 
     this.textBox1 = new System.Windows.Forms.TextBox(); 
     this.textBox2 = new System.Windows.Forms.TextBox(); 
     this.SuspendLayout(); 
     // 
     // openFileDialog1 
     // 
     this.openFileDialog1.FileName = "openFileDialog1"; 
     // 
     // buttonOpen 
     // 
     this.buttonOpen.Location = new System.Drawing.Point(13, 48); 
     this.buttonOpen.Name = "buttonOpen"; 
     this.buttonOpen.Size = new System.Drawing.Size(75, 23); 
     this.buttonOpen.TabIndex = 0; 
     this.buttonOpen.Text = "open"; 
     this.buttonOpen.UseVisualStyleBackColor = true; 
     this.buttonOpen.Click += new System.EventHandler(this.buttonOpen_Click); 
     // 
     // textBox1 
     // 
     this.textBox1.Location = new System.Drawing.Point(113, 50); 
     this.textBox1.Name = "textBox1"; 
     this.textBox1.Size = new System.Drawing.Size(279, 20); 
     this.textBox1.TabIndex = 1; 
     // 
     // textBox2 
     // 
     this.textBox2.Location = new System.Drawing.Point(13, 98); 
     this.textBox2.Name = "textBox2"; 
     this.textBox2.Size = new System.Drawing.Size(385, 20); 
     this.textBox2.TabIndex = 2; 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(445, 216); 
     this.Controls.Add(this.textBox2); 
     this.Controls.Add(this.textBox1); 
     this.Controls.Add(this.buttonOpen); 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.ResumeLayout(false); 
     this.PerformLayout(); 
+0

你在哪裏聲明你的openFileDialog? – jsmith

+0

關於openfiledialog的一個很好的教程http://www.c-sharpcorner.com/uploadfile/mahesh/openfiledialog-in-C-Sharp/ –

+3

你需要發佈比這更多的代碼,以及關於什麼是更多細節發生。表單顯示了嗎?你點擊按鈕? –

回答

0

一般來說我初始化和被稱之爲事件中使用我的OpenFileDialog的。我想不出一個我希望它成爲我窗口屬性的情況。我要做的第一件事就是將它作爲一個屬性刪除並在你的事件中初始化它。

private void buttonOpen_Click(object sender, EventArgs e) 
{ 
    using (OpenFileDialog openFileDialog1 = new OpenFileDialog()) 
    { 
     if (openFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      textBox2.Text = openFileDialog1.FileName; 
     } 
    } 
} 

您不需要將FileName屬性設置爲任何內容,因爲該對話框將爲您執行此操作。

我在您的錯誤代碼中發現的唯一一件事是(Program and debugger quit without indication of problem)。你目前的代碼中找不到任何會導致此問題的內容。如果您正在訪問非託管代碼,則可能需要啓用非託管代碼調試。

+0

不要忘記圍繞對話框對象使用''。 – DonBoitnott

相關問題