Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中访问其他函数中的变量#_C#_Winforms_Visual Studio Code - Fatal编程技术网

C# 在C中访问其他函数中的变量#

C# 在C中访问其他函数中的变量#,c#,winforms,visual-studio-code,C#,Winforms,Visual Studio Code,我最近在我的Raspberry Pi(或Headmeled的Code OSS)上安装了Visual Studio代码,并将其与mono“连接”起来,以创建和运行WinForms应用程序。但是,我总是使用VisualStudio创建WinForms应用程序,我不知道如何向WinForms应用程序添加按钮和标签。我住在中国,无法访问谷歌或YouTube。我试过在Bing上搜索,但大多数网站都被屏蔽了 编辑:我已成功创建了一个窗口,但似乎无法在其他功能中访问它。。。代码: using System;

我最近在我的Raspberry Pi(或Headmeled的Code OSS)上安装了Visual Studio代码,并将其与mono“连接”起来,以创建和运行WinForms应用程序。但是,我总是使用VisualStudio创建WinForms应用程序,我不知道如何向WinForms应用程序添加按钮和标签。我住在中国,无法访问谷歌或YouTube。我试过在Bing上搜索,但大多数网站都被屏蔽了

编辑:我已成功创建了一个窗口,但似乎无法在其他功能中访问它。。。代码:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Program
{
    [STAThread]
    private static void clicked(object sender, EventArgs e){
        password = textbox.Text;
        if(password == "mypassword"){
            stateLabel.Text = "Password is correct";
        }else{
            stateLabel.Text = "Password is incorrect";
        }
    }
    public static void Main()
    {
        var window = new Form();
        window.Text = "Login";
        window.Height = 130;
        window.Width = 365;
        TextBox textbox = new TextBox();
        Label passwordLabel = new Label();
        Button passwordButton = new Button();
        Label stateLabel = new Label();

        stateLabel.Text = "Please enter your password";
        passwordLabel.Text = "Password";
        passwordButton.Text = "Login";

        passwordLabel.Location = new Point(25, 30);
        textbox.Location = new Point(125, 25);
        passwordButton.Location = new Point(260, 25);
        stateLabel.Location = new Point(125, 60);
        passwordButton.Click += new System.EventHandler(clicked);

        window.Controls.Add(textbox);
        window.Controls.Add(passwordLabel);
        window.Controls.Add(passwordButton);
        window.Controls.Add(stateLabel);
        Application.Run(window);
    }
}

当我运行它时,它只会给我一个错误,说明“窗口”未在单击的函数中定义。

您可以通过创建该控件的实例并将其添加到
控件
集合,以编程方式将控件添加到窗体中

TextBox textbox = new TextBox();
textbox.Location = new Point(25,25);
this.Controls.Add (textbox);
您可以以类似的方式添加其他控件


要了解更多信息,您还可以检查

您的代码是否有效!我不得不将System.Drawing.dll添加到mcs编译器中,并使用System.Drawing
。谢谢!