Resolved Accessing WinForms var in other function

NoInternet

New member
Joined
Apr 23, 2020
Messages
3
Programming Experience
3-5
I have recently installed Visual Studio Code on my Raspberry Pi 3B+ (small pocket-computer) and I have also installed Mono to compile and run C# applications. I've made a simple window that has buttons and textboxes but I can't change their properties in the "clicked" function. There is no Visual Studio for Linux and if there is one, it would require too much resources and the Pi won't handle it. Is there any way of passing another variable alongside with object sender and EventArgs e?

My Code:
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);
    }
}
 
As always, if you expect to be able to access a variable in multiple methods then you need to declare it outside all methods. Currently, you are declaring textbox and stateLabel in the Main method so that's the only place you can access them. If you want to access them in another method as well, you'd have to declare them outside both, i.e. as fields. Be sure to declare them static, so that they are accessible from static methods.

As an alternative, the Button that was clicked is accessible via the sender parameter so you could use that to gain access to the form and then get the other controls by name from that. It's a more laborious option though.
 
That not how you normally set up and start a Forms application. By default the Main method in Program class only calls Application.Run(new FormX());. In a separate class FormX the form is set up, using the constructor to initialize the components. Inside FormX class you code as instance not static. Don't Visual Studio Code have standard project templates? For example new 'Windows Forms App (.Net Framework)' ?
 
That not how you normally set up and start a Forms application. By default the Main method in Program class only calls Application.Run(new FormX());. In a separate class FormX the form is set up, using the constructor to initialize the components. Inside FormX class you code as instance not static. Don't Visual Studio Code have standard project templates? For example new 'Windows Forms App (.Net Framework)' ?
Nope, I cannot find project templates in Visual Studio, I tried searching, but they're all for Visual Studio. In Visual Studio, if I create a standard WinForms application, it makes 3 files. Program.cs, Form1.designer.cs, and Form1.cs. I cannot compile all 3 of them. I've tried compiling Visual Studio solutions (.sln) and csproject files, but it doesn't work also
 
As always, if you expect to be able to access a variable in multiple methods then you need to declare it outside all methods. Currently, you are declaring textbox and stateLabel in the Main method so that's the only place you can access them. If you want to access them in another method as well, you'd have to declare them outside both, i.e. as fields. Be sure to declare them static, so that they are accessible from static methods.

As an alternative, the Button that was clicked is accessible via the sender parameter so you could use that to gain access to the form and then get the other controls by name from that. It's a more laborious option though.
Thanks jmcilhinney! I can access the other labels and textboxes from the clicked method!
Last question- Is there any way to access the var window outside of the main function? I cannot seem to create a var outside of a "local variable declaration"
 
dotnet new winforms command will set up a new winforms project exactly like I described. None of your code belongs in the Program class, it belongs in the Form class.
 
VSCode doesn't have templates, or anything of the sort, its not like Visual Studio. VSCode was designed for cross platform development, and expects you to use command line to build your app/files.

Why would you want to build a dotnet driven application on a Linux based OS? Why not use one of the already available GUI's like Gtk#, which is more logical and resource friendly?

Running dotnet on a monolithic kernel is rather silly when you have more suitably compatible alternatives available and they do the same job. Why squeeze the board for resources it doesn't have?

There are extensions you can use to help, but you will likely find they are buggy, and more hassle than they're worth.
 
Back
Top Bottom