using System; using System.Collections.Generic; using System.Windows.Forms; using NHibernate; using TBF.Entities; using TBF.Resources; namespace TBF.Forms { public partial class EditSelectedUser : Form { public User CurrentEditUser; public NHibernate.ISession Session; public EditSelectedUser() { InitializeComponent(); this.toolTip1.SetToolTip(this.txtName, Strings.MaxChars.Replace("_X_", Convert.ToString(20))); btnOk.Enabled = false; } private void EditSelectedUser_Load(object sender, EventArgs e) { txtName.Text = CurrentEditUser.Name; txtDescription.Text = CurrentEditUser.Description; txtPassword.Text = ""; ShowGroups(); } public void ShowGroups() { checkedListBoxGroups.Items.Clear(); for (int i= 0; (int)i < Grp.Count; i++) { Group group = Grp.FromId((Grp.GID)i); bool boolIsMember = CurrentEditUser.IsMemberOf((Grp.GID)group.Gid); if (boolIsMember == true) { checkedListBoxGroups.Items.Add(group.Name); checkedListBoxGroups.SetItemCheckState(i, CheckState.Checked); } else { checkedListBoxGroups.Items.Add(group.Name); checkedListBoxGroups.SetItemCheckState(i, CheckState.Unchecked); } } } public bool Save() { // todo: check if this name is given to another user if (IsUserNameAllreadyTaken(txtName.Text)) { MessageBox.Show(Strings.Username_taken); return false; } CurrentEditUser.Name = txtName.Text; if (txtPassword.Text != "") { CurrentEditUser.SetPassword(txtPassword.Text); } CurrentEditUser.Description = txtDescription.Text; // Groups CurrentEditUser.Groups.Clear(); for (int i = 0; (int)i < checkedListBoxGroups.Items.Count ; i++) { if (checkedListBoxGroups.GetItemChecked(i)) { Group newGroup = Grp.FromId((Grp.GID)i); CurrentEditUser.Groups.Add(newGroup ); } } Cursor.Current = Cursors.WaitCursor; FluentCommon.SaveToDb(Session, CurrentEditUser); Cursor.Current = Cursors.Default; return true; } /// /// returns true if the username allready exists for another user /// private bool IsUserNameAllreadyTaken(string Username) { // have a look into all other users IList ListOfUsers = User.GetAllUsers(); foreach (var person in ListOfUsers) { if (person.Id != CurrentEditUser.Id) { // other user if (person.Name.ToLower() == Username.ToLower()) { // Name is equal, so allready taken return true; } } } return false; } private void btnOk_Click(object sender, EventArgs e) { if (Save()) { this.Close(); } } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } private void CheckIfFormCanBeSubmitted() { btnOk.Enabled = false; if (txtName.Text == "") return; btnOk.Enabled = true; } private void EditSelectedUser_Shown(object sender, EventArgs e) { txtName.Focus(); } private void txtName_Validated(object sender, EventArgs e) { CheckIfFormCanBeSubmitted(); } private void txtName_KeyPress(object sender, KeyPressEventArgs e) { CheckIfFormCanBeSubmitted(); } private void txtName_TextChanged(object sender, EventArgs e) { CheckIfFormCanBeSubmitted(); } private void txtPassword_TextChanged(object sender, EventArgs e) { CheckIfFormCanBeSubmitted(); } private void txtDescription_TextChanged(object sender, EventArgs e) { CheckIfFormCanBeSubmitted(); } private void txtDescription_KeyPress(object sender, KeyPressEventArgs e) { CheckIfFormCanBeSubmitted(); } private void checkedListBoxGroups_Click(object sender, EventArgs e) { CheckIfFormCanBeSubmitted(); } } }