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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted May 30, 2013, 12:14 PM EDT
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();
VulpesRank #55 · 29.2K reputationPosted May 29, 2013, 7:51 AM EDT
amber34 reputationPosted May 30, 2013, 11:24 AM EDT
amber34 reputationPosted Jun 1, 2013, 7:20 AM EDT