简体   繁体   English

在C#中创建Form2并进行编辑

[英]Creating a Form2 in C# and editing it

So I'm very, very new to C#, and relatively new to programming. 所以我对C#非常非常新,而且编程也相对较新。 I decided to learn by writing a program for my niece, but I'm having a bit of an issue trying to understand how I can manage my "Form2" using the visual designer in VS 2010. 我决定通过为我的侄女编写程序来学习,但是我在尝试理解如何使用VS 2010中的可视化设计器来管理我的“Form2”时遇到了一些问题。

The code I have so far doesn't have a problem running in debug, and the form is launched fine, but I can't find it in the solution explorer. 到目前为止我的代码在调试中运行没有问题,并且表单启动正常,但我在解决方案资源管理器中找不到它。 This, I'm sure, has a really simple solution, and I feel stupid for asking it, but I can't find it on Google, or maybe I just don't know how to phrase the question. 我确定,这有一个非常简单的解决方案,我觉得它很愚蠢,但是我无法在谷歌上找到它,或者我可能只是不知道如何表达这个问题。

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void azbuka_Click(object sender, EventArgs e)
        {
            Form2 azbukatest = new Form2();
            azbukatest.ShowDialog();
        }
    }

    public partial class Form2 : Form
    {
        public Form2()
        {

        }
    }
}

It looks like you defined the second form in the same class as the first, rather than making another full form. 看起来您在第一个类中定义了第二个表单,而不是另一个完整表单。 If you want the full designer, create a new form using the Add button within the project: 如果您需要完整的设计器,请使用项目中的“ Add按钮创建新表单:

在此输入图像描述

This way visual studio will take care of all the extra details that allow you to utilize the designer and many other perks. 通过这种方式,visual studio将处理所有额外的细节,让您可以利用设计师和许多其他特权。

Follow this walkthrough. 按照本演练。 Next time search and then post questions. 下次搜索然后发布问题。 This is basic stuff and you can easily find tutorials/walkthroughs. 这是基本的东西,你可以很容易地找到教程/演练。

http://msdn.microsoft.com/en-us/library/vstudio/dd492132.aspx http://msdn.microsoft.com/en-us/library/vstudio/dd492132.aspx

C# winforms has two components to it, a Designer and Code Behind. C#winforms有两个组件,一个Designer和Code Behind。

What you've written here is just the code behind, which holds logic, events etc. 你在这里写的只是背后的代码,它包含逻辑,事件等。

The visual form is the designer which holds the UI elements like buttons and textboxes. 视觉形式是设计者,它包含按钮和文本框等UI元素。

Try adding a form using the Visual Studio menus (Add-> New -> Form) in your project, and it will add the dependant files. 尝试使用项目中的Visual Studio菜单(Add-> New - > Form)添加表单,它将添加相关文件。

Also, if you create a new class, it doesn't come up in the solution explorer as a new file automatically, try looking at the "Class View". 此外,如果您创建一个新类,它不会自动作为新文件出现在解决方案资源管理器中,请尝试查看“类视图”。

If you want a form generated that you can access in Solution Explorer you should right click on the project name and click Add-> New Item and select Windows Form from there. 如果要生成可在解决方案资源管理器中访问的表单,则应右键单击项目名称,然后单击添加 - >新项目,然后从中选择Windows窗体。

This will create a form you can edit in the IDE. 这将创建一个可以在IDE中编辑的表单。 Remember to remove 记得删除

public partial class Form2 : Form
{
    public Form2()
    {

    }
}

before doing this if you want it to be called Form2 (since creating the form via Add Item will create this code for you 在执行此操作之前,如果您希望将其称为Form2 (因为通过Add Item创建表单将为您创建此代码

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

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