6

I have this simple code:

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

When I run program form doesn't show and exit of debugging mode.

In output view writes:The program '[4244] openfiledialog.vshost.exe: Managed (v4.0.30319)' has exited with code 1073741855 (0x4000001f).

I have Visual Studio 2010 Professional.

Edit: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();
user3536856
  • 61
  • 1
  • 4
  • Where are you declaring your openFileDialog? – jsmith Apr 15 '14 at 16:41
  • a good tutorial about openfiledialog http://www.c-sharpcorner.com/uploadfile/mahesh/openfiledialog-in-C-Sharp/ – Nick Apr 15 '14 at 16:42
  • 3
    You'll need to post more code than that, as well as more detail about what is happening. Does the form show at all? Do you click on the button? – Jon B Apr 15 '14 at 16:43
  • I declaring in design( in toolbox i drag and drop).When I click to run debug it show form but when I click a button form close and exit of debug mode. – user3536856 Apr 15 '14 at 17:13
  • 2
    Goggling that exit code found this: http://stackoverflow.com/questions/4532457/program-and-debugger-quit-without-indication-of-problem. It's possible something else in your program is causing the debugger to exit. Since you've only posted the event handler, it's impossible to tell. – Jon B Apr 15 '14 at 17:18
  • You need to clean up your machine. Use SysInternals' Autoruns and disable shell extensions that you don't recognize or don't carry a Microsoft copyright notice. – Hans Passant Apr 15 '14 at 18:35
  • I'm succeed I run it in debug folder like administrator and work xD thank you all for help :) – user3536856 Apr 16 '14 at 21:27

1 Answers1

3

As a general rule I initialize and use my OpenFileDialog's within the event that is calling it. I can't think of a circumstance in which I would want it to be a property of my window. The first thing I would do is delete it as a property and initialize it in your event.

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

You don't need to set the FileName property to anything because the dialog will do it for you.

The only thing I found on your error code was this (Program and debugger quit without indication of problem). Which in your current code I cannot find anything that would cause this. If you are accessing unmanaged code you may need to enable unmanaged code debugging.

Community
  • 1
  • 1
jsmith
  • 7,198
  • 6
  • 42
  • 59