gpt4 book ai didi

c# - OpenFileDialog 不显示

转载 作者:行者123 更新时间:2023-11-30 12:45:45 29 4
gpt4 key购买 nike

我有这个简单的代码:

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

当我运行程序时窗体不显示并退出 Debug模式。

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)。在您当前的代码中,我找不到任何会导致此问题的内容。如果您正在访问非托管代码,您可能需要启用非托管代码调试。

关于c# - OpenFileDialog 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23089664/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com