简体   繁体   English

名称InitializeComponent在当前上下文中不存在

[英]The name InitializeComponent does not exist in the current context

I wanted to learn about building files with c# so I created a Windows Forms Application and I created this code/form. 我想学习有关使用c#构建文件的知识,因此我创建了Windows窗体应用程序,并创建了此代码/窗体。 But It did not seem to work. 但是它似乎没有用。
.

在此处输入图片说明

using System;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "*.exe |*.exe";
            DialogResult result = sfd.ShowDialog();
            if (result == DialogResult.OK)
            {
                build(sfd.FileName, textBox1.Text, textBox2.Text);
            }
        }

        private void build(string output, string msg, string name)
        {
            CompilerParameters p = new CompilerParameters();
            p.GenerateExecutable = true;
            p.ReferencedAssemblies.AddRange(new string[] { "System.dll", "System.Windows.Forms.dll"});
            p.OutputAssembly = output;
            p.CompilerOptions = "/t:winexe";

            string source = Properties.Resources.source;
            string errors = string.Empty;

            source = source.Replace("[MSG]", msg);
            source = source.Replace("[NAME]", name);

            CompilerResults results = new CSharpCodeProvider().CompileAssemblyFromSource(p, source);

            if (results.Errors.Count > 0)
            {
                foreach (CompilerError err in results.Errors)
                {
                    errors += "Error: " + err.ToString() + "\r\n\r\n";
                }
            }
            else
            {
                errors = "Successfully built:\n" + output;
            }

            MessageBox.Show(errors, "Build", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

Source.Txt: 的Source.txt:

using System;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("[msg]",  "[title]" );
        }
    }
}

But when I click the compile button it gives me an error. 但是,当我单击编译按钮时,它给了我一个错误。 The name InitializeComponent does not exist in the current context. 名称InitializeComponent在当前上下文中不存在。

在此处输入图片说明

The constructor is calling a method called "InitializeComponent", but that method is not defined in your class. 构造函数正在调用一个名为“ InitializeComponent”的方法,但该方法未在您的类中定义。 Typically, InitializeComponent is used by the VS designer to hold code needed to apply your selections in the designer at runtime. 通常,VS设计器使用InitializeComponent来保存在运行时在设计器中应用选择所需的代码。 If you copied this form from an example, either add an empty method called "InitializeComponent" to this class or remove this method call from your constructor. 如果您从示例中复制了此表单,请向此类添加一个名为“ InitializeComponent”的空方法,或者从构造函数中删除此方法调用。

暂无
暂无

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

相关问题 当前上下文中不存在“ InitializeComponent” - 'InitializeComponent' does not exist in the current context 当前上下文中不存在名称“InitializeComponent”(c#,xamarin) - The name 'InitializeComponent' does not exist in the current context(c#,xamarin) 我的 WPF 应用程序的当前上下文中不存在名称“InitializeComponent” - The name 'InitializeComponent' does not exist in the current context in my WPF application WPF应用程序中的当前上下文中不存在名称“InitializeComponent” - The name 'InitializeComponent' does not exist in the current context in WPF application .NET Maui 当前上下文中不存在名称“InitializeComponent” - .NET Maui The name 'InitializeComponent' does not exist in the current context 当前上下文中不存在名称“InitializeComponent” - The name 'InitializeComponent' doesn't exist in the current context 当前上下文错误中不存在InitializeComponent - InitializeComponent does not exist in the current context error 当前上下文中不存在“InitializeComponent”和“_FrameViews” - The 'InitializeComponent' and '_FrameViews' does not exist in the current context c# 不包含“InitializeComponent”的定义,名称“controlName”在当前上下文中不存在 - c# does not contain a definition for "InitializeComponent" and name "controlName" does not exist in current context 使用Visual Studio 2015打开WPF项目后,当前上下文错误中不存在名称“ InitializeComponent” - The name 'InitializeComponent' does not exist in the current context error after opening WPF project with Visual Studio 2015
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM