简体   繁体   English

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

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

I'm a newbie with Visual Studio Express 2013 and C#. 我是Visual Studio Express 2013和C#的新手。 I've borrowed a simple C# Windows application which builds and runs fine, and now I want to add some objects to the main form. 我借用了一个简单的C#Windows应用程序,该应用程序可以正常构建和运行,现在我想在主窗体中添加一些对象。

I select an object from the Toolbox, then click in the Designer on my main form where I want it to appear. 我从工具箱中选择一个对象,然后在我要显示它的主窗体上的设计器中单击。 This works fine for some simple objects like Label, but when I select an OpenFileDialog object nothing appears on my form. 对于某些简单的对象(如Label),此方法效果很好,但是当我选择OpenFileDialog对象时,表单上没有任何内容。 An OpenFileDialog "box" appears in a bar below my form instead, and I can't drag it to my form (I get a slashed circle). 相反,OpenFileDialog“框”出现在表单下方的栏中,并且我无法将其拖动到表单中(出现斜线)。

I'm surely missing something simple. 我肯定错过了一些简单的事情。 Thanks for any help. 谢谢你的帮助。

You cannot drag an OpenFileDialog to the form because it is a non visual control. 您不能将OpenFileDialog拖到窗体上,因为它是非可视控件。 To add an OpenFileDialog,just double click the control in the toolbox.That will add it to the form.Now to show the dialog you have to use OpenFileDialog.ShowDialog() in the code behind.Here is an example which shows the dialog on the click of a button.: 要添加一个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