Sylloge
A C# helper library
code/Controls/ControlExtensions.cs
Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 
00006 namespace Sylloge.Controls
00007 {
00008     #region Windows Form Extensions
00009 
00014     public class ListBox : System.Windows.Forms.ListBox
00015     {
00016         private delegate void DelegateAddMessage(object item);
00017 
00021         public bool AutoScroll { get; set; }
00022 
00027         public void Add(object item)
00028         {
00029             try {
00030                 if (this.InvokeRequired) {
00031                     this.Invoke(new DelegateAddMessage(this.Add), new object[] { item });
00032                 } else {
00033                     this.Items.Add(item);
00034                     if (this.AutoScroll) {
00035                         try {
00036                             this.SelectedIndex = this.Items.Count - 1;
00037                         } catch (Exception ex) {
00038                             Console.WriteLine(ex.Message);
00039                         }
00040                     }
00041                 }
00042             } catch (Exception ex) {
00043                 Console.WriteLine(ex.Message);
00044             }
00045         }
00046 
00051         public new void RefreshItem(int index)
00052         {
00053             base.RefreshItem(index);
00054         }
00055     }
00056 
00060     public class Textbox : System.Windows.Forms.TextBox
00061     {
00066         public enum TextboxType
00067         {
00071             Default,
00075             Numeric,
00079             AlphaNumeric
00080         }
00081 
00085         public TextboxType Type { get; set; }
00086 
00090         public bool AutoScroll { get; set; }
00091 
00095         public Textbox()
00096             : base()
00097         {
00098             base.TextChanged += new EventHandler(this.Textbox_TextChanged);
00099             base.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Textbox_KeyPress);
00100         }
00101 
00108         private void Textbox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
00109         {
00110             // A handler for the input, you can still hook into your own handler
00111             // if you implement this extension
00112             switch (this.Type) {
00113                 case TextboxType.AlphaNumeric: // Letters/Numbers
00114                     e.Handled = !char.IsLetterOrDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
00115                     break;
00116                 case TextboxType.Numeric: // Numeric only
00117                     e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
00118                     break;
00119             }
00120         }
00121 
00128         private void Textbox_TextChanged(object sender, EventArgs e)
00129         {
00130             if (this.AutoScroll) {
00131                 this.Select(this.Text.Length, 0);
00132                 this.ScrollToCaret();
00133             }
00134         }
00135     }
00136 
00137     #endregion
00138 }
 All Classes Namespaces Files Functions Variables Enumerations Properties Events