繁体   English   中英

无法将控件放在Visual Studio中的表单上

[英]Can't place a control on a form in Visual Studio

我是Visual Studio Express 2013和C#的新手。 我借用了一个简单的C#Windows应用程序,该应用程序可以正常构建和运行,现在我想在主窗体中添加一些对象。

我从工具箱中选择一个对象,然后在我要显示它的主窗体上的设计器中单击。 对于某些简单的对象(如Label),此方法效果很好,但是当我选择OpenFileDialog对象时,表单上没有任何内容。 相反,OpenFileDialog“框”出现在表单下方的栏中,并且我无法将其拖动到表单中(出现斜线)。

我肯定错过了一些简单的事情。 谢谢你的帮助。

您不能将OpenFileDialog拖到窗体上,因为它是非可视控件。 要添加一个OpenFileDialog,只需双击工具箱中的控件即可将其添加到窗体中。现在要显示该对话框,您必须在后面的代码中使用OpenFileDialog.ShowDialog()。单击按钮。:

private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
DialogResult result=  openFileDialog1.ShowDialog();
if(result==DialogResult.OK)
using (StreamReader reader = new StreamReader(openFileDialog1.FileName))
{
string s = reader.ReadLine();
}
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM