Archive for Agosto 8th, 2008

Valid US Postal code format Regex

CodeKeep C# Feed Agosto 8th, 2008

Description: Allows 5 digit, 5+4 digit and 9 digit zip codes

Link: http://www.codekeep.net/snippets/6ae965ad-297e-4ad6-8329-1856f3ed9d90.aspx

#
public static bool IsValidUSZip(string num)
#
{
#
    // Allows 5 digit, 5+4 digit and 9 digit zip codes
#
    string pattern = @"^(\d{5}-\d{4}|\d{5}|\d{9})$";
#
    Regex match = new Regex(pattern);
#
    return match.IsMatch(num);
#
} 

method to validate string is alphanumberic

CodeKeep C# Feed Agosto 8th, 2008

Description: Check a string if it is only alpha numeric by using a regular expression

Link: http://www.codekeep.net/snippets/709e2731-8c4b-4967-808d-283f2064232e.aspx

public bool IsAlphaNumeric(String str)
{
  Regex regexAlphaNum=new Regex("[^a-zA-Z0-9]");
  return !regexAlphaNum.IsMatch(str);    
}

Auto Complete TextBox C#

CodeKeep C# Feed Agosto 8th, 2008

Description: See Auto Complete TextBox XAML. see notes for link

Link: http://www.codekeep.net/snippets/5486829b-a1c3-49ea-8a2e-1cf50b9cece2.aspx

/* Auto Complete TextBox
 * by Phil Vollhardt
 * thephilv@gmail.com
 */
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Collections.Generic;

namespace philVsControls {
    public partial class AutoCompTextBox {
        protected List<string> _values;
        protected CollectionViewSource _viewS;
        public static readonly DependencyProperty HorizontalScrollbarProperty = DependencyProperty.Register("HorizontalScrollbar", typeof(ScrollBarVisibility), 
            typeof(AutoCompTextBox), new UIPropertyMetadata(ScrollBarVisibility.Disabled, new PropertyChangedCallback(OnHorizontalScrollbarChanged), 
                new CoerceValueCallback(OnCoerceHorizontalScrollbar)));
        public static readonly DependencyProperty VerticalScrollbarProperty = DependencyProperty.Register("VerticalScrollbar", typeof(ScrollBarVisibility), 
            typeof(AutoCompTextBox), new UIPropertyMetadata(ScrollBarVisibility.Disabled, new PropertyChangedCallback(OnVerticalScrollbarChanged), 
                new CoerceValueCallback(OnCoerceVerticalScrollbar)));
        public static readonly DependencyProperty SourceObjDataProviderProperty = DependencyProperty.Register("SourceObjDataProvider", typeof(ObjectDataProvider), 
            typeof(AutoCompTextBox), new UIPropertyMetadata(null, new PropertyChangedCallback(OnSourceObjDataProviderChanged), 
                new CoerceValueCallback(OnCoerceSourceObjDataProvider)));
        public static readonly DependencyProperty DropBoxMaxHeightProperty = DependencyProperty.Register("DropBoxMaxHeight", typeof(double), typeof(AutoCompTextBox), 
            new UIPropertyMetadata(100.0, new PropertyChangedCallback(OnDropBoxMaxHeightChanged), new CoerceValueCallback(OnCoerceDropBoxMaxHeight)));

        public AutoCompTextBox() {
            this.InitializeComponent();

            // Insert code required on object creation below this point.
            _viewS = (CollectionViewSource)this.FindResource("_vs");
            _tb.PreviewKeyUp += new System.Windows.Input.KeyEventHandler(_tb_PreviewKeyDown);
        }

        protected void _tb_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e) {
            switch (e.Key) {
                case System.Windows.Input.Key.Up:
                    if (_lb.SelectedIndex > 0) {
                        _lb.SelectedIndex -= 1;
                        _lb.ScrollIntoView(_lb.SelectedItem);
                    }
                    break;
                case System.Windows.Input.Key.Down:
                    if (_lb.SelectedIndex < _lb.Items.Count) {
                        _lb.SelectedIndex += 1;
                        _lb.ScrollIntoView(_lb.SelectedItem);
                    }
                    break;
                case System.Windows.Input.Key.Enter:
                    if (_lb.Items.Count > 0) {
                        _tb.Text = _lb.SelectedItem.ToString();
                        _lb.Visibility = Visibility.Hidden;
                        _tb.SelectionStart = _tb.Text.Length;
                    }
                    break;
            }
        }


        protected void CollectionViewSource_Filter(object sender, FilterEventArgs e) {
            e.Accepted = e.Item.ToString().StartsWith(_tb.Text, StringComparison.CurrentCultureIgnoreCase);

        }

        protected void _tb_TextChanged(object sender, TextChangedEventArgs e) {

            if (null != _viewS.Source) {
                this._lb.Visibility = this._tb.Text != "" ? Visibility.Visible : Visibility.Hidden;
                _viewS.View.Refresh();
            }
            
        }

        protected void _uc_SizeChanged(object sender, SizeChangedEventArgs e) {
            this.Resources["_width"] = e.NewSize.Width;
        }

        #region horizscrollbar dp
        private static object OnCoerceHorizontalScrollbar(DependencyObject o, object value) {
            AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
            if (autoCompTextBox != null)
                return autoCompTextBox.OnCoerceHorizontalScrollbar((ScrollBarVisibility)value);
            else
                return value;
        }

        private static void OnHorizontalScrollbarChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) {
            AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
            if (autoCompTextBox != null)
                autoCompTextBox.OnHorizontalScrollbarChanged((ScrollBarVisibility)e.OldValue, (ScrollBarVisibility)e.NewValue);
        }

        protected virtual ScrollBarVisibility OnCoerceHorizontalScrollbar(ScrollBarVisibility value) {
            // TODO: Keep the proposed value within the desired range.
            return value;
        }

        protected virtual void OnHorizontalScrollbarChanged(ScrollBarVisibility oldValue, ScrollBarVisibility newValue) {
            // TODO: Add your property changed side-effects. Descendants can override as well.
            _lb.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, newValue);
        }

        /// <summary>
        /// Gets or sets the use of a horizontal scrollbar in dropbox. Default is disabled
        /// </summary>
        public ScrollBarVisibility HorizontalScrollbar {
            // IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
            get {
                return (ScrollBarVisibility)GetValue(HorizontalScrollbarProperty);
            }
            set {
                SetValue(HorizontalScrollbarProperty, value);
            }
        }
        #endregion
        
        #region vertscrollbar dp
        private static object OnCoerceVerticalScrollbar(DependencyObject o, object value) {
            AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
            if (autoCompTextBox != null)
                return autoCompTextBox.OnCoerceVerticalScrollbar((ScrollBarVisibility)value);
            else
                return value;
        }

        private static void OnVerticalScrollbarChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) {
            AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
            if (autoCompTextBox != null)
                autoCompTextBox.OnVerticalScrollbarChanged((ScrollBarVisibility)e.OldValue, (ScrollBarVisibility)e.NewValue);
        }

        protected virtual ScrollBarVisibility OnCoerceVerticalScrollbar(ScrollBarVisibility value) {
            // TODO: Keep the proposed value within the desired range.
            return value;
        }

        protected virtual void OnVerticalScrollbarChanged(ScrollBarVisibility oldValue, ScrollBarVisibility newValue) {
            // TODO: Add your property changed side-effects. Descendants can override as well.
            _lb.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, newValue);
        }

        /// <summary>
        /// Gets or sets the use of a vertical scrollbar in dropbox. Default is disabled
        /// </summary>
        public ScrollBarVisibility VerticalScrollbar {
            // IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
            get {
                return (ScrollBarVisibility)GetValue(VerticalScrollbarProperty);
            }
            set {
                SetValue(VerticalScrollbarProperty, value);
            }
        }
        #endregion
        
        #region SourceObjectDataProvider
        private static object OnCoerceSourceObjDataProvider(DependencyObject o, object value) {
            AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
            if (autoCompTextBox != null)
                return autoCompTextBox.OnCoerceSourceObjDataProvider((ObjectDataProvider)value);
            else
                return value;
        }

        private static void OnSourceObjDataProviderChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) {
            AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
            if (autoCompTextBox != null)
                autoCompTextBox.OnSourceObjDataProviderChanged((ObjectDataProvider)e.OldValue, (ObjectDataProvider)e.NewValue);
        }

        protected virtual ObjectDataProvider OnCoerceSourceObjDataProvider(ObjectDataProvider value) {
            // TODO: Keep the proposed value within the desired range.
            return value;
        }

        protected virtual void OnSourceObjDataProviderChanged(ObjectDataProvider oldValue, ObjectDataProvider newValue) {
            // TODO: Add your property changed side-effects. Descendants can override as well.
            _viewS.SetValue(CollectionViewSource.SourceProperty, newValue);
        }

        public ObjectDataProvider SourceObjDataProvider {
            // IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
            get {
                return (ObjectDataProvider)GetValue(SourceObjDataProviderProperty);
            }
            set {
                SetValue(SourceObjDataProviderProperty, value);
            }
        }
        #endregion
        #region Max Height of listbox
        private static object OnCoerceDropBoxMaxHeight(DependencyObject o, object value) {
            AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
            if (autoCompTextBox != null)
                return autoCompTextBox.OnCoerceDropBoxMaxHeight((double)value);
            else
                return value;
        }

        private static void OnDropBoxMaxHeightChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) {
            AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
            if (autoCompTextBox != null)
                autoCompTextBox.OnDropBoxMaxHeightChanged((double)e.OldValue, (double)e.NewValue);
        }

        protected virtual double OnCoerceDropBoxMaxHeight(double value) {
            if (value < 0) value = 0;
            return value;
        }

        protected virtual void OnDropBoxMaxHeightChanged(double oldValue, double newValue) {
            // TODO: Add your property changed side-effects. Descendants can override as well.
            _lb.SetValue(ListBox.MaxHeightProperty, newValue);
        }

        /// <summary>
        /// Gets or sets the MaxHeight of the listbox.  dependency property. default is 100
        /// </summary>
        public double DropBoxMaxHeight {
            // IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
            get {
                return (double)GetValue(DropBoxMaxHeightProperty);
            }
            set {
                SetValue(DropBoxMaxHeightProperty, value);
            }
        }
        
        #endregion

    }
}

Launching p4.exe

CodeKeep C# Feed Agosto 8th, 2008

Description: nothing exciting

Link: http://www.codekeep.net/snippets/818b9a6c-e92d-4e2d-b3f4-2a27d2997598.aspx

{
	System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("p4.exe", command);
	startInfo.EnvironmentVariables["P4CLIENT"] = P4Client();
	if (input != null)
	{
		startInfo.RedirectStandardInput = true;
	}
	startInfo.RedirectStandardOutput = true;
	startInfo.UseShellExecute = false;
	System.Diagnostics.Process proc = System.Diagnostics.Process.Start(startInfo);

	if (input != null)
	{
		System.IO.StreamWriter writer = proc.StandardInput;
		while (!input.EndOfStream)
		{
			string line = input.ReadLine();
			writer.WriteLine(line);
			Log(">>>" + line);
		}
		writer.Close();
	}
	System.IO.StreamReader reader = proc.StandardOutput;
	while (!reader.EndOfStream)
	{
		string line = reader.ReadLine();
		Log(line);
		Console.WriteLine(line);
	}

	proc.WaitForExit();
	if (proc.ExitCode != 0)
	{
		Log("Exit code = ", proc.ExitCode.ToString());
	}
}