简体   繁体   中英

How would I go About Getting a Global Variables value in another form?

So I am new to Programming and I am working In Visual Studios c# Windows application Forms, and I have a button on the first form That needs to set a Glabel Bool To true when pressed while also opening the second form.

On the second forms startup I need it to tell if that bool is set to true or false, to enable a button(on the second Form) or keep it disabled(if False.

Here Is the First Forms Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Project
{
    public partial class FirstPage: Form
    {
        public FirstPage()
        {
            InitializeComponent();
        }

        public bool IsManager = false;

        public static class Global
        {
            public static bool IsManager = false;
        }

        private void Button_Btn_Click(object sender, EventArgs e)
        {
            this.hide();  
            Form2 f2 = new form2(IsManager);
            f2.ShowDialog();

            IsManager = true;
        }
    }
}

Here is my second Forms Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Project
{
    public partial class SecondPage : Form
    {
        public FirstPage()
        {
            InitializeComponent();
            IsManager = isManager;
        }
        private bool isManager;

        private void setButtonVisibility()
        {
            if(isManager == true)
            {
                MessageBox.Show("Button Message Display")
            }
        {


        //Need to check if the Global variable is true or false on page load to set a button to either enabled or disabled
    }
}

Hopefully this makes sense If not let me know:) Thank You!

You can acomplish this by passing the variable to the constructor of the second form. Let's assume you want to send the IsManager value to the second form. Call the second form while passing the value to the constructor:

Form2 f2 = new Form2(IsManager);

In the second form, read the value:

public partial class Form2 : Form
{
    private bool IsManager;
    public Form2(bool isManager)
    {
        InitializeComponent();
        IsManager = isManager;
    }
    //Render button or whatever based on IsManager value;
}

EDIT: To check if the button needs to be enabled or not:

public partial class Form2 : Form
{
    private bool IsManager;
    public Form2(bool isManager)
    {
        InitializeComponent();
        IsManager = isManager;
        setButtonVisibility(); //call method setVisibility() -> this is what was missing in your code

    }
    private void setButtonVisibility()
    {
        if(IsManager == true)
        {
            MessageBox.Show("Button Message Display");
            //or YourButton.Enable = true; in order to enable it.
        }
    }
}

In your second form, there are a couple of issues:

  • Class name and constructor name are not the same;
  • The parameter for the Form2 constructor is missing;
  • isManager should be assigned the value of IsManager which should have been passed as a parameter to the constructor.

Well after reading your question I am assuming that you're trying to set a value to the global bool IsManager and want to do some action in the other second form based on this value.

You can achieve this in multiple ways one passes your global bool in the constructor of second form something like this

public partial class SecondPage: Form
{
    bool _isManager;
    public SecondPage(bool IsManager)
    {
        InitializeComponent();
       _isManager = IsManager; // the problem was isManager as mistakenkly i used small "i" 
    }
}

and from the first form pass value like

 private void Button_Btn_Click(object sender, EventArgs e)
 {
    this.hide();  
    IsManager = true;
    Form2 f2 = new form2(IsManager);
    f2.ShowDialog();
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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