如何显示openfiledialog? [英] How do I display an openfiledialog?

查看:105
本文介绍了如何显示openfiledialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Windows窗体编写一个小程序(而不是WPF),但文件选择器控件没有显示在窗体上。



我尝试了什么:



我尝试使用TextBox并且它出现了,但是文件选择器停靠在设计器下方的这个较小的选项卡中。 br />
我在IntializeComponent方法中找到了Form1.Designer.cs代码,并添加了

 this.openFileDialog1.ShowDialog(); 

这会导致文件选择器立即触发表单加载(因为它正在初始化,甚至在任何控件都被加载之前),然后当我关闭它时,我只能在表单上找到TextBox但没有文件选择器。





我也尝试在容器控件中包装内容,但它也没有帮助。



[设计师的图像给我^ ]

决方案

打开文件组件根本不显示 - 这就是为什么它们没有出现在设计器的表单上,而是出现在它下面。



您需要决定何时显示对话框,并编写代码以在该点显示对话框。例如,您可能需要一个打开按钮:

  private   void  OpenFile_Click( object  sender,EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
...
if (ofd.ShowDialog()= DialogResult.OK)
{
...
}
}


请参见此处的示例:如何:使用OpenFileDialog组件打开文件Microsoft Docs [ ^ ]

如果不是绝对必要,请不要更改 Form1.Designer.cs

I want to write a tiny program with Windows form (instead of maybe WPF) but the file picker control is not showing up on the form.

What I have tried:

I tried using the TextBox and it appeared, but the file picker instead docks in this smaller tab below the designer.
I went to the Form1.Designer.cs code, in the IntializeComponent method, and added

this.openFileDialog1.ShowDialog();

This causes the file picker to trigger immediately the form loads (as it's initializing, before any control is even loaded), then when I close it, I can only find the TextBox on the form but no file picker.


I also tried wrapping contents in a container control but it didn't help either.

[Images of what the designer is giving me^]

解决方案

Open File components don't display at all - that's why they do not appear on the form in the designer, but underneath it.

What you need to is decide when the dialog should be displayed, and write code to display it at that point. For example, you might want an "Open" button:

private void OpenFile_Click(object sender, EventArgs e)
   {
   OpenFileDialog ofd = new OpenFileDialog();
   ...
   if (ofd.ShowDialog() = DialogResult.OK)
      {
      ...
      }
   }


See example here: How to: Open Files Using the OpenFileDialog Component | Microsoft Docs[^]
And please do not change Form1.Designer.cs if it is not absolutely necessary !


这篇关于如何显示openfiledialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆