Hi,
i wanna display logged in user name in a title bar on every form in my c# winform application. Is it possible?? There are six forms in an application n m using Access-2007 for database storage.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted May 30, 2013, 4:14 PM
internal static string UserName = "";
Now change your checkvaliduser method slightly:
public bool checkvaliduser (string a, string b)
{
string ambi1 = "SELECT * FROM Pword where Usr = '" + a + "' and Pwd = '" + b + "'"; OleDbCommand cmd = new OleDbCommand(ambi1, conn);
conn.Open();
OleDbDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
conn.Close();
checklogin = true;
UserName = a;
return true;
}
else
{
conn.Close();
return false;
}
}
Suppose, the class name of the login form is LogIn.
The code to write the username to the title bar of each form will now become:
string user = "User: " + LogIn.UserName;
foreach(Form f in Application.OpenForms) f.Text = user;
Notice that the static field will still be available after the LogIn form has been closed.
Notice also that if all your 4 forms are not opened at once, you can set the title bar text as you open them individually with code such as this following:
SomeForm f = new SomeForm();
f.Text = "User: " + LogIn.UserName;
f.Show();
amberPosted Jun 1, 2013, 11:20 AM
amberPosted May 30, 2013, 3:24 PM
VulpesPosted May 29, 2013, 11:51 AM