سلام و ببخشید از تاخیر که خیلی در گیر بودم
ببین این یک روتین است که با کلیک بر روی منو اجرا می شود
privatevoid costpointInterfaceToolStripMenuItem1_Click(object sender, EventArgs e)
{
CanPass_bol = false;
FullName_Str = "";
using (Login Login_Obj = new Login(1,
"Data Source=servername;Initial Catalog=" +
"databasename" +
" ;User ID=username;pwd=password"))
{
if (Login_Obj.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
UserName_Str = Login_Obj.UserName.Trim();
// پیدا کردن نام کامل کاربراز درون دیتا بیس (شما باید کد خود را جایگزین نمایید
ClsRetrieveCPUserName Names_Obj = newClsRetrieveCPUserName();
Names_Obj.RetrieveName(UserName_Str);
//================================================
UserType_int = Names_Obj.User_Type;
FullName_Str = Names_Obj.FirstName + " " + Names_Obj.LastName;
Names_Obj = null;
CanPass_bol = true;
toolStripStatusLabel2.Text = "User: " + FullName_Str;
}
} //using
if (CanPass_bol)
{
Form MyCPInterface = newFrmCPInterface(FullName_Str, UserType_int, UserName_Str);
MyCPInterface.ShowDialog();
}
}
همانطور که ملاحظه می کنی سازنده فرم من (FrmCPInterface) نام کامل کاربر و نوع کاربر و نام کاربری را دریافت می نماید.
این هم سازنده فرم که کد آن درون فرم قرار دارد و با اجرای
Form MyCPInterface = newFrmCPInterface(FullName_Str, UserType_int, UserName_Str);
اجرا می شود
public FrmCPInterface(string MyUser_str, int MyUserType_int, string My_User_Name_str)
{
InitializeComponent();
toolStripStatusLabel2.Text = "User: " + MyUser_str;
User_Full_Name = MyUser_str;
User__Name = My_User_Name_str;
User_AccessType_int = MyUserType_int;
if (MyUserType_int == 5)
//تنظیم سطح دسترسی (باید براساس برنامه خودت آن را تعریف نمایی)
toolStripMyUtility.Visible = true;
else
toolStripMyUtility.Visible = false;
label5.Text = ""; label6.Text = "";
//=============================================
}
همانطور که می بینی یک سری Property در درون فرم تعریف شده است که از درون سازنده نام کامل کاربر و کد ان و Username را دریافت می کند
toolStripStatusLabel2.Text = "User: " + MyUser_str;
User_Full_Name = MyUser_str;
User__Name = My_User_Name_str;
User_AccessType_int = MyUserType_int;
این هم تعریف Property ها
privatestring User_Full_Name
{
set { _My_User_Full_Name_str = value; }
get { return _My_User_Full_Name_str; }
}
privatestring User__Name
{
set { _My_User_Name_str = value; }
get { return _My_User_Name_str; }
}
این هم سورس برنامه Login که باید قبلا از آقای هاشمیان از بابت کتاب خوبشان تشکر نمایم
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace AE_Login_T
{
publicpartialclassLogin : Form
{
#region // Define Variables
// Private variables
privateint AttemptCount_int = 0;
privatebool AllowClosing_bol = false;
privateint UserID_int;
privateint UserCheckType_int;
privatestring UserName_str;
privatestring Conn_str;
privatebool PassOK_bol = false;
// Public Delegates
publicdelegatevoid_LoginFailed(object sender, EventArgs e);
publicdelegatevoid_LoginSucceeded(object sender, EventArgs e);
publicdelegatevoid_LoginCancelled(object sender, EventArgs e);
// Public Events
publicevent_LoginFailed LoginFailed;
publicevent_LoginSucceeded LoginSucceeded;
publicevent_LoginCancelled LoginCancelled;
#endregion
privatevoid btnCancel_Click(object sender, EventArgs e)
{
//Raise the Loging Cancell event
if (this.LoginCancelled != null)
this.LoginCancelled(this, newEventArgs());
//Turn on the Allow Closing Flag
AllowClosing_bol = true;
}
privatevoid btnOK_Click(object sender, EventArgs e)
{
//Was a username entered?
if (txtUserName.Text.Trim().Length > 0)
{
switch (UserCheckType_int)
{
case 1: // CpInterface
ClsUpdateAuxiliaryTable.ClsCP Myd_obj =
new ClsUpdateAuxiliaryTable.ClsCP();
Myd_obj.ReaddbData(Conn_str);
System.Data.SqlClient.SqlConnection _Myc =
new System.Data.SqlClient.SqlConnection("Data Source=seoul.doosan.local;Initial Catalog=" +
Myd_obj.DatbaseName.Trim() + ";User ID=" +
txtUserName.Text.Trim() +
";pwd=" + txtPassword.Text.Trim());
//System.Data.SqlClient.SqlConnection _Myc =
// new System.Data.SqlClient.SqlConnection("Data Source=seoul.doosan.local;Initial Catalog=" +
// Myd_obj.DatbaseName.Trim() + ";User ID=" +
// Myd_obj.Username.Trim() +
// ";pwd=" + Myd_obj.Password.Trim());
try
{
_Myc.Open();
UserID_int = 27;
UserName_str = txtUserName.Text.Trim();
//if (this.LoginSucceeded != null)
//{
// this.LoginSucceeded(this, new LoginEventAgrs(UserID_int, UserName_str));
// turn on allow Closing Flag
AllowClosing_bol = true;
PassOK_bol = true;
//}
//else
// PassOK_bol = false;
}
catch
{
PassOK_bol = false;
}
_Myc.Close();
_Myc = null;
Myd_obj = null;
break;
default:
break;
}
if(!PassOK_bol)
{
AttemptCount_int++;
// Informed the user that the password is Invalid
MessageBox.Show("The Username OR Password you entered was invalid",
"Login Try " + AttemptCount_int.ToString() + " of 3",
MessageBoxButtons.OK, MessageBoxIcon.Information);
// Increment the attempt count
//Check the attempt count
if (AttemptCount_int == 3)
{
//Raise the Loging Failed Event
if (this.LoginFailed != null)
this.LoginFailed(this, newEventArgs());
// Set the cancel dialog result
this.DialogResult = DialogResult.Cancel;
//Turn on the Allow Closing Flag
AllowClosing_bol = true;
}//(AttemptCount_int == 3)
else
{
// Inform the User that they must supply a username
//MessageBox.Show("You must supply a username", "Login", MessageBoxButtons.OK,
// MessageBoxIcon.Information);
}
}
}//(txtUserName.Text.Trim().Length > 0)
}
public Login()
{
InitializeComponent();
}
public Login(int MyType, string Connection_str)
{
InitializeComponent();
UserCheckType_int = MyType;
Conn_str = Connection_str;
}
privatevoid Login_Activated(object sender, EventArgs e)
{
txtUserName.Text = Environment.UserName;
txtPassword.Focus();
}
privatevoid Login_FormClosing(object sender, FormClosingEventArgs e)
{
// If we are not allowing the form to close
if (!AllowClosing_bol)
{
// Set the cancel flag to true
e.Cancel = true;
}
}
publicint UserID
{
get
{
return UserID_int;
}
}
publicstring UserName
{
get
{
return UserName_str;
}
}
}
}
کلاس مورد نیاز
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AE_Login_T
{
publicclassLoginEventAgrs : EventArgs
{
#region // Define Variables
int User_ID_int;
string UserName_str;
#endregion
// constructor
public LoginEventAgrs(int UserIdentifier, string UserNameIdentifier)
{
User_ID_int = UserIdentifier;
UserName_str = UserNameIdentifier;
}
}
}
از باز هم مشکل داری بگو ببینم چکار می توانم بکنم
موفق باشی