c# – OpenFileDialog没有显示

我有这个简单的代码

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

当我运行程序窗体时不显示退出调试模式.

In output view writes:The program ‘[4244] openfiledialog.vshost.exe: Managed (v4.0.30319)’ has exited with code 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();

解决方法

作为一般规则,我在调用它的事件中初始化并使用我的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).在您当前的代码中,我找不到任何可能导致此问题的内容.如果要访问非托管代码,则可能需要启用非托管代码调试.

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...