gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:51:33 25 4
gpt4 key购买 nike

我想了解如何使用 C# 构建文件,因此我创建了一个 Windows 窗体应用程序并创建了此代码/窗体。但它似乎没有用。
.

enter image description here

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);
}
}
}

来源.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]" );
}
}
}

但是当我点击编译按钮时,它给了我一个错误。当前上下文中不存在名称 InitializeComponent。

enter image description here

最佳答案

构造函数正在调用名为“InitializeComponent”的方法,但该方法未在您的类中定义。通常,VS 设计器使用 InitializeComponent 来保存在运行时在设计器中应用您的选择所需的代码。如果您从示例中复制了此表单,请向此类添加一个名为“InitializeComponent”的空方法,或者从您的构造函数中删除此方法调用。

关于c# - 当前上下文中不存在名称 InitializeComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35515390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com