Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Ok so I am trying to pass a boolean from my Login form to my Home form, normally this would be fine for me and I would just use a property. However I thought I could use a similar method this time but I am implementing the singleton factory on the forms.

Here is the Login code relevant to this:
The AdminAccess property gets set fine and I have checked the value is correct.

C#
private bool adminAccess;

public bool  AdminAccess
        {
            get { return adminAccess; }
            private set { adminAccess = value; }
        }

private void btnLogin_Click(object sender, EventArgs e)
        {
            //Some Code Does Stuff
            
             OpenHome();
        }

private void OpenHome()
        {
            HomeForm CreateHomeForm = HomeForm.HomeUI;
            CreateHomeForm.StartupHome = this;

            //Trying to set the property.
            CreateHomeForm.AdminPermissions= this.AdminAccess;

            CreateHomeForm.Show();
            this.Hide();
        }


Here is the relevant code from the Home form:

C#
public HomeForm()
        {
            InitializeComponent();
            //just to check what is in the property quickly
            textBox1.Text = AdminPermissions.ToString();
        }

private bool adminPermissions;
public bool AdminPermissions
        {
            private get { return adminPermissions; }
            set { adminPermissions = value; }
        }

public Form StartupHome
        {
            set;
            get;
        }

private static HomeForm homeUI;
public static HomeForm HomeUI
        {
            get
            {
                if (homeUI == null || homeUI.IsDisposed)
                {
                    homeUI = new HomeForm();
                }
                return homeUI;
            }
        }


The value gets reset when the HomeUI if loop runs as a new instance of the form is created. I can't seem to think how to modify this to get a working solution. As you can tell I am fairly amateur so I'm just looking for a quick and clean solution to this :)
Thank you very much for your time in advance!
Posted
Comments
Sergey Alexandrovich Kryukov 24-Apr-12 12:01pm    
And where is your singleton?
--SA

Why don't you just pass the value through the HomeForm constructor?
 
Share this answer
 
Comments
Jack Eker 24-Apr-12 10:39am    
I tried that and I get an error trying to pass anything inside the "homeUI = new HomeForm();" constructor :S
[no name] 24-Apr-12 10:55am    
What does "an error" mean?
The solution I have come to is as follows:

C#
public HomeForm()
        {
            InitializeComponent();
        }

private void HomeForm_Shown(object sender, EventArgs e)
        {
            if (AdminPermissions)
            {
                btnCreateUser.Visible = true;
            }
            else
            {
                btnCreateUser.Visible = false;
            }
        }

private bool adminPermissions;
public bool AdminPermissions
        {
            get { return adminPermissions; }
            set { adminPermissions = value; }
        }

public Form StartupHome
        {
            set;
            get;
        }

private static HomeForm homeUI;
public static HomeForm HomeUI
        {
            get
            {
                if (homeUI == null || homeUI.IsDisposed)
                {
                    homeUI = new HomeForm();
                }
                return homeUI;
            }
        }
 
Share this answer
 
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900