| |
Sign In
Chooser c = new Chooser("Select Item", new string[]{"Option 1", "Option 2", "Option 3"}, true, true); DialogResult d = c.ShowDialog();c.Dispose();if(d != DialogResult.Cancel) MessageBox.Show("You chose " + Chooser.SelectedItem);
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Threading;namespace IbenzaMobileUtil{ public partial class Chooser : Form { private MainMenu mainMenu1; private MenuItem mmOK; private MenuItem mmCancel; private Label lblCaption; public static string SelectedItem = null; public static List<string> CheckedItems = null; private TreeView treeView1; private bool selectInvokesClose; public Chooser() { InitializeComponent(); } private Chooser(string Caption, bool FullScreen, bool SelectInvokesClose) { InitializeComponent(); CheckedItems = new List<string>(); SelectedItem = null; this.selectInvokesClose = SelectInvokesClose; this.lblCaption.Text = Caption; this.mmOK.Enabled = false; if (FullScreen) { this.WindowState = FormWindowState.Maximized; this.FormBorderStyle = FormBorderStyle.None; this.ControlBox = false; this.MinimizeBox = false; } } public Chooser(string Caption, string[] Items, bool FullScreen, bool SelectInvokesClose) : this(Caption, FullScreen, SelectInvokesClose) { this.treeView1.BeginUpdate(); foreach (string s in Items) this.treeView1.Nodes.Add(s); this.treeView1.EndUpdate(); } public Chooser(string Caption, string[] Items, bool FullScreen, bool CheckBoxes, bool SelectInvokesClose, bool CheckAll) : this(Caption, Items, FullScreen, SelectInvokesClose) { this.treeView1.CheckBoxes = CheckBoxes; if (CheckBoxes && CheckAll) foreach (TreeNode node in this.treeView1.Nodes) node.Checked = true; if(CheckBoxes) this.mmOK.Enabled = true; // don't require any user input } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { this.mmOK.Enabled = true; if (selectInvokesClose) this.mmOK_Click(sender, EventArgs.Empty); } #region Windows Form Designer generated code /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.mmCancel = new System.Windows.Forms.MenuItem(); this.mmOK = new System.Windows.Forms.MenuItem(); this.lblCaption = new System.Windows.Forms.Label(); this.treeView1 = new System.Windows.Forms.TreeView(); this.SuspendLayout(); // // mainMenu1 // this.mainMenu1.MenuItems.Add(this.mmCancel); this.mainMenu1.MenuItems.Add(this.mmOK); // // mmCancel // this.mmCancel.Text = "Cancel"; this.mmCancel.Click += new System.EventHandler(this.mmCancel_Click); // // mmOK // this.mmOK.Text = "OK"; this.mmOK.Click += new System.EventHandler(this.mmOK_Click); // // lblCaption // this.lblCaption.Dock = System.Windows.Forms.DockStyle.Top; this.lblCaption.Font = new System.Drawing.Font("Tahoma", 10F, System.Drawing.FontStyle.Bold); this.lblCaption.Location = new System.Drawing.Point(0, 0); this.lblCaption.Name = "lblCaption"; this.lblCaption.Size = new System.Drawing.Size(240, 27); this.lblCaption.Text = "..."; // // treeView1 // this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill; this.treeView1.Location = new System.Drawing.Point(0, 27); this.treeView1.Name = "treeView1"; this.treeView1.ShowLines = false; this.treeView1.ShowPlusMinus = false; this.treeView1.ShowRootLines = false; this.treeView1.Size = new System.Drawing.Size(240, 241); this.treeView1.TabIndex = 12; // // Chooser // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; this.AutoScroll = true; this.ClientSize = new System.Drawing.Size(240, 268); this.ControlBox = false; this.Controls.Add(this.treeView1); this.Controls.Add(this.lblCaption); this.KeyPreview = true; this.Menu = this.mainMenu1; this.MinimizeBox = false; this.Name = "Chooser"; this.Text = "Choose Item"; this.Load += new System.EventHandler(this.Chooser_Load); this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Chooser_KeyDown); this.ResumeLayout(false); } #endregion private void Chooser_KeyDown(object sender, KeyEventArgs e) { if ((e.KeyCode == System.Windows.Forms.Keys.Enter)) this.mmOK_Click(sender, EventArgs.Empty); } private void mmOK_Click(object sender, EventArgs e) { if (this.treeView1.SelectedNode != null) Chooser.SelectedItem = this.treeView1.SelectedNode.Text; if (this.treeView1.CheckBoxes) foreach (TreeNode n in this.treeView1.Nodes) if (n.Checked) CheckedItems.Add(n.Text); this.DialogResult = DialogResult.OK; this.Close(); } private void mmCancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); } private void Chooser_Load(object sender, EventArgs e) { this.treeView1.Focus(); this.treeView1.SelectedNode = null; this.treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect); } }}
Remember Me