C#, menustrip specific menu open when user have logged in
Domanda
in my menuStrip i have multiple forms,
So in 1 of them i want to be access only when use is pass from a login form i have create..
Lets say menustrip, menu is on form1, and the login form is Form2,
So how i am gonna comebine this to so menustrip form open when the user first pass the form2 login..
i am searching all day over the net i found only by passing a variable and then if its true the form1 is gonna pop-out but it didnt work well!
Soluzione
Well thinking I got it right "You want to have input in form 1
from form2
or something like modify something in form1 from form2"
It may not be the best but works
Well the simplest solution that has helped me till now is that you
- Make
menuStrip
modifier fromproperties
topublic
- create an instance on
Form1
inForm2
- Modify
menuStrip
fromForm2
for example we can have
Form2.cs
public partial class ViewCars : Form
{
Form1 mainForm;
public ViewCars (Form1 MainForm)
{
this.mainForm = MainForm;
}
}
private void LoginButton_Click(object sender, EventArgs e)
{
mainForm.menuStrip.Visible = false;
}
Now to when you make the instance of Form2
you need to have give parameter this
i.e
ViewCars view = new ViewCars(this);
view.Show();
This is not Limited to controls you can do anything with it Good Luck
Altri suggerimenti
Somewhere you will need to have a static Boolean property telling you if the user is logged in.
public static class Config {
public static bool LoggedIn { get; private set; }
public static Login ()
{
var frm = new frmLogin();
if (frm.ShowDialog() == DialogResult.OK) {
LoggedIn = frm.LoggedIn;
}
}
}
In frmLogin
have again a public, but this time non-static property LoggedIn
, which you set true if username and password are OK.
Then, in the menustrip form enable or disable the menu items accordingly in the form Activated event handler:
private void Form1_Activated(object sender, EventArgs e)
{
myToolStripMenuItem.Enabled = Config.LoggedIn;
}