Subscribe to our RSS Feeds

【C#】一个命令行参数解释器

0 Comments »
测试效果:
E:\Desktop>c -arg1 test1 -arg2 test2 param "quotes support"
Command Line Arguments:
   arg1 = test1
   arg2 = test2

Command Line Parameters:
    param
    quotes support


支持各类参数样式,如“-”、“/”等,支持开关和参数共同使用。

代码下载地址:
范例&代码:http://www.uushare.com/user/snow518no2/file/811139
代码:http://www.uushare.com/user/snow518no2/file/811140

转自:
http://www.codeproject.com/KB/cs/CommandLineParser.aspx
, , 9/20/2008 02:35:00 下午

【C#】一个输入数字专用的文本框控件

0 Comments »
蓝蓝小雪原创:
一个输入数字专用的文本框控件;
支持各类数据格式;
支持用上下箭头、Page Up、Page Down选择;
支持控制大小范围。

使用很简单,我就不说了。直接把下面的代码贴到TextBoxEx.cs中就可以了。
之后用这个控件,在属性面板中可以看到新增的属性,这些属性就可以调整文本框的行为了。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace BlueSnowStudio.Common.CommonControls
{
    public enum TextBoxExContentType
    {
        Text,
        NumberCurrency,
        NumberDecimal,
        NumberSingle,
        NumberDouble,
        NumberSmallInteger,
        NumberInteger,
        NumberLargeInteger
    }

    public class TextBoxEx : TextBox
    {
        private TextBoxExContentType m_ContentType = TextBoxExContentType.Text;
        private bool m_EnableMinMax = false;
        private double m_MinValue = 0.00;
        private double m_MaxValue = 100.00;

        public TextBoxEx()
        {
            InitializeComponent();
        }

        public TextBoxEx(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
        }

        [Category("TextBoxEx"), DefaultValue(0)]
        public TextBoxExContentType ContentType
        {
            get
            {
                return m_ContentType;
            }
            set
            {
                m_ContentType = value;
                switch (m_ContentType)
                {
                    case TextBoxExContentType.NumberCurrency:
                    case TextBoxExContentType.NumberDecimal:
                    case TextBoxExContentType.NumberDouble:
                    case TextBoxExContentType.NumberInteger:
                    case TextBoxExContentType.NumberLargeInteger:
                    case TextBoxExContentType.NumberSingle:
                    case TextBoxExContentType.NumberSmallInteger:
                        this.ContextMenu = new ContextMenu();
                        break;
                    default:
                        this.ContextMenu = null;
                        m_EnableMinMax = false;
                        break;
                }
                this.Text = "";
            }
        }

        [Category("TextBoxEx"), DefaultValue(false)]
        public bool EnableMinMax
        {
            get
            {
                return m_EnableMinMax;
            }
            set
            {
                switch (m_ContentType)
                {
                    case TextBoxExContentType.NumberCurrency:
                    case TextBoxExContentType.NumberDecimal:
                    case TextBoxExContentType.NumberDouble:
                    case TextBoxExContentType.NumberInteger:
                    case TextBoxExContentType.NumberLargeInteger:
                    case TextBoxExContentType.NumberSingle:
                    case TextBoxExContentType.NumberSmallInteger:
                        m_EnableMinMax = value;
                        break;
                    default:
                        m_EnableMinMax = false;
                        break;
                }
                this.OnLeave(new EventArgs());                
            }
        }

        [Category("TextBoxEx"), DefaultValue(-100.00)]
        public double MinValue
        {
            get
            {
                return m_MinValue;
            }
            set
            {
                m_MinValue = value;
                this.OnLeave(new EventArgs());                
            }
        }
        [Category("TextBoxEx"), DefaultValue(100.00)]
        public double MaxValue
        {
            get
            {
                return m_MaxValue;
            }
            set
            {
                m_MaxValue = value;
                this.OnLeave(new EventArgs());
            }
        }

        public void InitializeComponent()
        {
            this.KeyPress += new KeyPressEventHandler(TextBoxEx_KeyPress);
            this.KeyDown += new KeyEventHandler(TextBoxEx_KeyDown);
            this.Leave += new EventHandler(TextBoxEx_Leave);
        }

        void TextBoxEx_Leave(object sender, EventArgs e)
        {
            switch (m_ContentType)
            {
                case TextBoxExContentType.NumberCurrency:
                case TextBoxExContentType.NumberDecimal:
                case TextBoxExContentType.NumberDouble:
                case TextBoxExContentType.NumberInteger:
                case TextBoxExContentType.NumberLargeInteger:
                case TextBoxExContentType.NumberSingle:
                case TextBoxExContentType.NumberSmallInteger:
                    // handle - and leading zeros input since KeyPress handler must allow this
                    if (base.Text != "")
                    {
                        if (!IsValidNumber(base.Text, false))
                            base.Text = "";
                        else if (Double.Parse(base.Text) == 0)    // this used for -0, 000 and other strings
                            base.Text = "0";
                    }
                    break;
                default:
                    break;
            }
        }

        void TextBoxEx_KeyDown(object sender, KeyEventArgs e)
        {
            switch (m_ContentType)
            {
                case TextBoxExContentType.NumberCurrency:
                case TextBoxExContentType.NumberDecimal:
                case TextBoxExContentType.NumberDouble:
                case TextBoxExContentType.NumberInteger:
                case TextBoxExContentType.NumberLargeInteger:
                case TextBoxExContentType.NumberSingle:
                case TextBoxExContentType.NumberSmallInteger:
                    Keys keyData = e.KeyData;
                    if (keyData == (Keys)Shortcut.CtrlV || keyData == (Keys)Shortcut.ShiftIns)
                    {
                        IDataObject iData = Clipboard.GetDataObject();

                        // assemble new string and check IsValid
                        string newText;
                        newText = base.Text.Substring(0, base.SelectionStart)
                            + (string)iData.GetData(DataFormats.Text)
                            + base.Text.Substring(base.SelectionStart + base.SelectionLength);

                        // check if data to be pasted is convertable to inputType
                        if (!IsValidNumber(newText, true))
                            e.Handled = true;
                    }
                    else if (keyData == Keys.Up || keyData == Keys.Down)
                    {
                        double addvalue;
                        switch (m_ContentType)
                        {
                            case TextBoxExContentType.NumberCurrency:
                            case TextBoxExContentType.NumberDecimal:
                            case TextBoxExContentType.NumberSingle:
                            case TextBoxExContentType.NumberDouble:
                                addvalue = keyData == Keys.Up ? +0.1 : -0.1;
                                break;
                            //case TextBoxExContentType.NumberInteger:
                            //case TextBoxExContentType.NumberLargeInteger:
                            //case TextBoxExContentType.NumberSmallInteger:
                            default:
                                addvalue = keyData == Keys.Up ? +1 : -1;
                                break;
                        }
                        this.NumberOperate(addvalue);

                        e.Handled = true;
                    }
                    else if (keyData == Keys.PageUp || keyData == Keys.PageDown)
                    {
                        double addvalue;
                        switch (m_ContentType)
                        {
                            case TextBoxExContentType.NumberCurrency:
                            case TextBoxExContentType.NumberDecimal:
                            case TextBoxExContentType.NumberSingle:
                            case TextBoxExContentType.NumberDouble:
                                addvalue = keyData == Keys.PageUp ? +1 : -1;
                                break;
                            //case TextBoxExContentType.NumberInteger:
                            //case TextBoxExContentType.NumberLargeInteger:
                            //case TextBoxExContentType.NumberSmallInteger:
                            default:
                                addvalue = keyData == Keys.PageUp ? +10 : -10;
                                break;
                        }
                        this.NumberOperate(addvalue);

                        e.Handled = true;
                    }
                    else
                    {

                    }
                    break;
                default:
                    break;
            }
        }

        void TextBoxEx_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (m_ContentType)
            {
                case TextBoxExContentType.NumberCurrency:
                case TextBoxExContentType.NumberDecimal:
                case TextBoxExContentType.NumberDouble:
                case TextBoxExContentType.NumberInteger:
                case TextBoxExContentType.NumberLargeInteger:
                case TextBoxExContentType.NumberSingle:
                case TextBoxExContentType.NumberSmallInteger:
                    char c = e.KeyChar;
                    if (!Char.IsControl(c))    // not sure about this?? nothing in docs about what is Control char??
                    {
                        // prevent spaces
                        if (c.ToString() == " ")
                        {
                            e.Handled = true;
                            return;
                        }

                        string newText = base.Text.Substring(0, base.SelectionStart)
                            + c.ToString() + base.Text.Substring(base.SelectionStart + base.SelectionLength);

                        if (!IsValidNumber(newText, true))
                            e.Handled = true;
                    }
                    break;
                default:
                    break;
            }
        }

        // IsVaildNumber, Copy from Oscar Bowyer's Numerice Edit Control (http://www.codeproject.com/KB/edit/numedit.aspx).
        private bool IsValidNumber(string val, bool user)
        {
            bool ret = true;

            if (val.Equals("")
                || val.Equals(String.Empty))
                return ret;

            if (user)
            {
                if (val.Equals("-"))
                    return ret;
            }

            double Min = m_MinValue;
            double Max = m_MaxValue;

            try
            {
                switch (m_ContentType)
                {
                    case TextBoxExContentType.NumberCurrency:
                        decimal dec = decimal.Parse(val);
                        int pos = val.IndexOf(".");
                        if (pos != -1)// 2 decimals + "."
                            ret = val.Substring(pos).Length <= 3;
                        //if (m_EnableMinMax) ret &= Min <= (double)dec && (double)dec <= Max;
                        if (m_EnableMinMax)
                        {
                            if ((double)dec <= Min)
                            {
                                dec = (decimal)Min;
                                this.Text = dec.ToString();
                                this.SelectAll();
                                ret = false;
                            }
                            else if ((double) dec >= Max)
                            {
                                dec = (decimal)Max;
                                this.Text = dec.ToString();
                                this.SelectAll();
                                ret = false;
                            }
                        }

                        break;
                    case TextBoxExContentType.NumberSingle:
                        float flt = float.Parse(val);
                        //if (m_EnableMinMax) ret &= Min <= flt && flt <= Max;
                        if (m_EnableMinMax)
                        {
                            if ((double)flt <= Min)
                            {
                                flt = (float)Min;
                                this.Text = flt.ToString();
                                this.SelectAll();
                                ret = false;
                            }
                            else if ((double)flt >= Max)
                            {
                                flt = (float)Max;
                                this.Text = flt.ToString();
                                this.SelectAll();
                                ret = false;
                            }
                        }

                        break;
                    case TextBoxExContentType.NumberDouble:
                        double dbl = double.Parse(val);
                        //if (m_EnableMinMax) ret &= Min <= dbl && dbl <= Max;
                        if (m_EnableMinMax)
                        {
                            if (dbl <= Min)
                            {
                                dbl = Min;
                                this.Text = dbl.ToString();
                                this.SelectAll();
                                ret = false;
                            }
                            else if (dbl >= Max)
                            {
                                dbl = Max;
                                this.Text = dbl.ToString();
                                this.SelectAll();
                                ret = false;
                            }
                        }

                        break;
                    case TextBoxExContentType.NumberDecimal:
                        decimal dec2 = decimal.Parse(val);
                        //if (m_EnableMinMax) ret &= Min <= (double)dec2 && (double)dec2 <= Max;
                        if (m_EnableMinMax)
                        {
                            if ((double)dec2 <= Min)
                            {
                                dec2 = (decimal)Min;
                                this.Text = dec2.ToString();
                                this.SelectAll();
                                ret = false;
                            }
                            else if ((double)dec2 >= Max)
                            {
                                dec2 = (decimal)Max;
                                this.Text = dec2.ToString();
                                this.SelectAll();
                                ret = false;
                            }
                        }

                        break;
                    case TextBoxExContentType.NumberSmallInteger:
                        short s = short.Parse(val);
                        //if (m_EnableMinMax) ret &= Min <= s && s <= Max;
                        if (m_EnableMinMax)
                        {
                            if ((double)s <= Min)
                            {
                                s = (short)Min;
                                this.Text = s.ToString();
                                this.SelectAll();
                                ret = false;
                            }
                            else if ((double)s >= Max)
                            {
                                s = (short)Max;
                                this.Text = s.ToString();
                                this.SelectAll();
                                ret = false;
                            }
                        }

                        break;
                    case TextBoxExContentType.NumberInteger:
                        int i = int.Parse(val);
                        //if (m_EnableMinMax) ret &= Min <= i && i <= Max;
                        if (m_EnableMinMax)
                        {
                            if ((double)i <= Min)
                            {
                                i = (int)Min;
                                this.Text = i.ToString();
                                this.SelectAll();
                                ret = false;
                            }
                            else if ((double)i >= Max)
                            {
                                i = (int)Max;
                                this.Text = i.ToString();
                                this.SelectAll();
                                ret = false;
                            }
                        }

                        break;
                    case TextBoxExContentType.NumberLargeInteger:
                        long l = long.Parse(val);
                        //if (m_EnableMinMax) ret &= Min <= l && l <= Max;
                        if (m_EnableMinMax)
                        {
                            if ((double)l <= Min)
                            {
                                l = (long)Min;
                                this.Text = l.ToString();
                                this.SelectAll();
                                ret = false;
                            }
                            else if ((double)l >= Max)
                            {
                                l = (long)Max;
                                this.Text = l.ToString();
                                this.SelectAll();
                                ret = false;
                            }
                        }

                        break;
                    default:
                        throw new ApplicationException();
                }
            }
            catch
            {
                ret = false;
            }
            return ret;
        }

        private void NumberOperate(double addValue)
        {
            string val = this.Text;

            if (val.Equals("") || val.Equals(String.Empty))
            {
                val = "0";
            }

            try
            {
                switch (m_ContentType)
                {
                    case TextBoxExContentType.NumberCurrency:
                        decimal dec = decimal.Parse(val);
                        dec += (decimal)addValue;
                        if (IsValidNumber(dec.ToString(), false)) this.Text = dec.ToString();
                        break;
                    case TextBoxExContentType.NumberSingle:
                        float flt = float.Parse(val);
                        flt += (float)addValue;
                        if (IsValidNumber(flt.ToString(), false)) this.Text = flt.ToString();
                        break;
                    case TextBoxExContentType.NumberDouble:
                        double dbl = double.Parse(val);
                        dbl += (double)addValue;
                        if (IsValidNumber(dbl.ToString(), false)) this.Text = dbl.ToString();
                        break;
                    case TextBoxExContentType.NumberDecimal:
                        decimal dec2 = decimal.Parse(val);
                        dec2 += (decimal)addValue;
                        if (IsValidNumber(dec2.ToString(), false)) this.Text = dec2.ToString();
                        break;
                    case TextBoxExContentType.NumberSmallInteger:
                        short s = short.Parse(val);
                        s += (short)addValue;
                        if (IsValidNumber(s.ToString(), false)) this.Text = s.ToString();
                        break;
                    case TextBoxExContentType.NumberInteger:
                        int i = int.Parse(val);
                        i += (int)addValue;
                        if (IsValidNumber(i.ToString(), false)) this.Text = i.ToString();
                        break;
                    case TextBoxExContentType.NumberLargeInteger:
                        long l = long.Parse(val);
                        l += (long)addValue;
                        if (IsValidNumber(l.ToString(), false)) this.Text = l.ToString();
                        break;
                    default:
                        throw new ApplicationException();
                }
            }
            catch { }

            this.SelectAll();
        }

    }
}
, , , , 9/20/2008 11:17:00 上午

【公告】.NET 超市网站上线

0 Comments »
  经过紧张的制作,.NET 超市终于上线。
  .NET 超市是一个以免费共享 VB.net & C#.net 代码的地方,当然以后也可能会出售代码DVD哦!本网站每周更新7篇以上的文章,主要集中在周末更新,这是因为本网站的团队都是学生。
  希望本网站能给您带来有用的东西!
如何联系我们?
蓝蓝小雪
邮箱:**********@*****.*** (单击这儿查看这个地址)
QQ:502462725

慕容天枫
邮箱:YangJianPRO@HotMail.com
QQ:582878145
9/20/2008 10:33:00 上午

With Blue Snow Studio, Nothing is impossible.