Создание Windows-приложений на основе Visual C#


           

Форма выбора пользователя selectUser.cs


Форма выбора пользователя появляется при запуске программы, она также используется для смены пользователя. Значения свойства Name элементов управления этой формы приведены на рис. 3.33:


Рис. 3.33.  Форма selectUser

Полный листинг формы:

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.IO;

namespace MailApplication { /// <summary> /// Summary description for selectUser. /// </summary> public class selectUser : System.Windows.Forms.Form { public System.Windows.Forms.ListView lstViewUsers; private System.Windows.Forms.Button btnCancel; private System.Windows.Forms.Button btnSelect; private System.Windows.Forms.Label lblUserSelect; private System.Windows.Forms.ImageList imgLstUser; private System.Windows.Forms.ColumnHeader colUserName; private System.ComponentModel.IContainer components;

public selectUser() { // // Required for Windows Form Designer support // InitializeComponent();

// // TODO: Add any constructor code after InitializeComponent call // }

/// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); }

#region Windows Form Designer generated code /// <summary> /// Required method for Designer support — do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(selectUser)); this.lstViewUsers = new System.Windows.Forms.ListView(); this.colUserName = new System.Windows.Forms.ColumnHeader(); this.imgLstUser = new System.Windows.Forms.ImageList(this.components); this.btnCancel = new System.Windows.Forms.Button(); this.btnSelect = new System.Windows.Forms.Button(); this.lblUserSelect = new System.Windows.Forms.Label(); this.SuspendLayout(); // // lstViewUsers // this.lstViewUsers.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { this.colUserName}); this.lstViewUsers.Cursor = System.Windows.Forms.Cursors.Hand; this.lstViewUsers.GridLines = true; this.lstViewUsers.Location = new System.Drawing.Point(8, 40); this.lstViewUsers.MultiSelect = false; this.lstViewUsers.Name = "lstViewUsers"; this.lstViewUsers.Size = new System.Drawing.Size(272, 176); this.lstViewUsers.SmallImageList = this.imgLstUser; this.lstViewUsers.TabIndex = 0; this.lstViewUsers.View = System.Windows.Forms.View.Details; // // colUserName // this.colUserName.Text = "Имя пользователя"; this.colUserName.Width = 268; // // imgLstUser // this.imgLstUser.ImageSize = new System.Drawing.Size(16, 16); this.imgLstUser.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgLstUser.ImageStream"))); this.imgLstUser.TransparentColor = System.Drawing.Color.Transparent; // // btnCancel // this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.btnCancel.Location = new System.Drawing.Point(200, 232); this.btnCancel.Name = "btnCancel"; this.btnCancel.TabIndex = 1; this.btnCancel.Text = "Отмена"; this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click); // // btnSelect // this.btnSelect.Location = new System.Drawing.Point(128, 232); this.btnSelect.Name = "btnSelect"; this.btnSelect.TabIndex = 2; this.btnSelect.Text = "Выбор"; this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click); // // lblUserSelect // this.lblUserSelect.Location = new System.Drawing.Point(8, 8); this.lblUserSelect.Name = "lblUserSelect"; this.lblUserSelect.Size = new System.Drawing.Size(272, 23); this.lblUserSelect.TabIndex = 3; this.lblUserSelect.Text = "Выберите пользователя из списка:"; this.lblUserSelect.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // selectUser // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.CancelButton = this.btnCancel; this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.lblUserSelect); this.Controls.Add(this.btnSelect); this.Controls.Add(this.btnCancel); this.Controls.Add(this.lstViewUsers); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Name = "selectUser"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "Выбор пользователя"; this.Load += new System.EventHandler(this.selectUser_Load); this.ResumeLayout(false);

} #endregion

private void selectUser_Load(object sender, System.EventArgs e) { DirectoryInfo dir = new DirectoryInfo(Environment.CurrentDirectory); FileInfo[] users = dir.GetFiles("*.usr"); foreach(FileInfo user in users) { ListViewItem item = new ListViewItem(user.Name, 0); lstViewUsers.Items.Add(item); } }

private void btnSelect_Click(object sender, System.EventArgs e) { if(lstViewUsers.SelectedItems.Count == 0) MessageBox.Show("Выберите пользователя для начала работы", "Пользователь не выбран"); else { this.DialogResult = DialogResult.OK; this.Close(); } }

private void btnCancel_Click(object sender, System.EventArgs e) { this.Close(); } } }

Листинг 3.25.



Содержание  Назад  Вперед