Reputation: 25
I have this project where you can login as a user and as an employee and if you log in as an employee certain buttons such as reports will show up on the homepage.
I have two forms named HomePage
and frmSignIn
. If the user signs in from frmSignIn
I want certain button on HomePage
to be visible. I have tried several ways but cannot get it to work.
I did make the button public
to see if it works, although I know this is bad practice.
if (ValidCredentials(txtUsername.Text, txtPassword.Text) == true && checkBoxEmployee.Checked == true && txtBoxEmpPin.Text.Equals(employeePin))
{
this.DialogResult = DialogResult.OK;
this.Tag = _usersId;
this.Close();
MessageBox.Show("You have successfully logged in as an employee.");
HomePage hp = new HomePage();
hp.Button.Visible = true; // !!!! Does not work !!!!
}
Upvotes: 0
Views: 1062
Reputation: 6203
You can use public property where You keep reference to your button, try this example.
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 x = new Form2();
x.Button1 = this.button1;
x.Show();
}
}
Form2
public partial class Form2 : Form
{
private Button _button1;
public Button Button1
{
get { return _button1; }
set { _button1 = value; }
}
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
_button1.Visible = false;
}
}
Upvotes: 1
Reputation: 451
One way to do this, is making your original homepage known to the frmSignIn.
To do this, you could create a property in your frmSignIn like
public MyHomePage Form { get; set; }
Then, when creating the SignIn form, also add the reference of the homepage to the new instance of frmSignIn. Something like:
var mySignInForm = new frmSignIn();
mySignInForm.MyHomePage = this;
Now, you have a decent reference in your frmSignIn to do whatever you want. You still would have to make the button public though.
Alternatively you could, instead of the complete form, just add a reference to the button you need in the same way.
This may not be the prettiest and most elegant solution, but it will work.
Upvotes: 1
Reputation: 4000
Create a method for homepage
which enables Button
public void EnableButton(){
this.Button.Visible = true;
}
And Do this,
this.DialogResult = DialogResult.OK;
this.Tag = _usersId;
HomePage hp = new HomePage(); //here, creating instance of homepage
hp.EnableButton(); //This enables the required button
MessageBox.Show("You have successfully logged in as an employee.");
hp.showDialog(); //Shows HomePage
this.Hide(); //Hides login page.
Upvotes: 0