Frmd has been created a limited scope, e.g. it has been created in line 18 and it dies at line 20! You need to create at the upper scope and just set enable there. Also, you need to keep the reference to frmd object by FrmMain:
private void btnlogin_Click(object sender, EventArgs e)
{
if(cmbusername.Text != "" && txtpassword.Text != "")
{
// Your code
if (dr.HasRows == true)
{
FormDataEntry frmd = new FormDataEntry();
if(cmbusername.Text == "USER")
{
frmd.button1.Enabled = false;
}
else if(cmbusername.Text == "ADMIN")
{
frmd.button1.Enabled = true;
}
this.Hide();
FrmMain FrmMain = new FrmMain();
//Keep a reference to frmd obj though FrmMain, otherwise you will lose it
// If is MDI relation: frmd.MdiParent = FrmMain;
// If not, something like this: FrmMain.DataEntryForm = frmd.
FrmMain.Show();
}
// the rest of your code
}
My Suggestion:
creating a singleton class for the user and use this class inside FormDataEntry & FrmMain classes:
public class User
{
public bool IsAdmin { get; set; }
public bool IsUser { get; set; }
private static User _currentUser;
private static object _locker = new object(); // Remove this if you don't need to support multi-thread.
public static User CurrentUser
{
get
{
// Returns _currentUser, if it has been create before
if (_currentUser != null) return _currentUser;
// lock all threads and let them go inside lock one by one
lock (_locker)
{
// Double-checked locking: This will prevent multi-time creation of _currentUser.
// e.g. two threads came together for the first time and wait in the lock(_locker).
if (_currentUser == null)
{
_currentUser = new User();
}
return _currentUser;
}
}
}
}
Then btnlogin_Click will like this:
private void btnlogin_Click(object sender, EventArgs e)
{
if(cmbusername.Text != "" && txtpassword.Text != "")
{
// Your code
if (dr.HasRows == true)
{
User.CurrentUser.IsUser= cmbusername.Text == "USER";
User.CurrentUser.IsAdmin = cmbusername.Text == "ADMIN";
this.Hide();
FrmMain FrmMain = new FrmMain();
FrmMain.Show();
}
// the rest of your code
}
Then, use User in FormDataEntry form loading event:
private void FormDataEntry_Load(object sender, EventArgs e)
{
button1.Enabled = User.CurrentUser.IsAdmin;
}