Subscribe to our RSS Feeds

【Small Basic】练手小程序2之黑板

1 Comments »
恩……这个程序是根据帮助文件中的一个改得,增加了调整画笔粗细和颜色的按键。也是没什么说头的。status = True
clearWidth = 10
penWidth = 2
GraphicsWindow.BackgroundColor = "Black"
GraphicsWindow.PenColor = "White"
GraphicsWindow.BrushColor = "White"
GraphicsWindow.PenWidth = penWidth
GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.MouseMove = OnMouseMove
GraphicsWindow.KeyDown = OnKeyDown
GraphicsWindow.Title = "黑板"
GraphicsWindow.DrawText(0, 0, "X:擦除 1:白色 2:黄色 3:蓝色 4:绿色 5:红色 Q/W:调整粗细")

Sub OnMouseDown
  prevX = GraphicsWindow.MouseX
  prevY = GraphicsWindow.MouseY
EndSub

Sub OnKeyDown
  If (GraphicsWindow.LastKey = "X") then
    GraphicsWindow.PenColor = GraphicsWindow.BackgroundColor
    GraphicsWindow.PenWidth = clearWidth
    status = False
  EndIf
  If (GraphicsWindow.LastKey = "D1") then
    GraphicsWindow.PenColor = "White"
    GraphicsWindow.PenWidth = penWidth
    status = True
  EndIf
  If (GraphicsWindow.LastKey = "D2") then
    GraphicsWindow.PenColor = "Yellow"
    GraphicsWindow.PenWidth = penWidth
    status = True
  EndIf
  If (GraphicsWindow.LastKey = "D3") then
    GraphicsWindow.PenColor = "Blue"
    GraphicsWindow.PenWidth = penWidth
    status = True
  EndIf
  If (GraphicsWindow.LastKey = "D4") then
    GraphicsWindow.PenColor = "Lime"
    GraphicsWindow.PenWidth = penWidth
    status = True
  EndIf
  If (GraphicsWindow.LastKey = "D5") then
    GraphicsWindow.PenColor = "Red"
    GraphicsWindow.PenWidth = penWidth
    status = True
  EndIf
  If (GraphicsWindow.LastKey = "Q") then
    If (status = True) then
      penWidth = penWidth - 1
      GraphicsWindow.PenWidth = penWidth
    Else
      clearWidth = clearWidth - 1
      GraphicsWindow.PenWidth = clearWidth
    EndIf
  EndIf
  If (GraphicsWindow.LastKey = "W") then
    If (status = True) then
      penWidth = penWidth + 1
      GraphicsWindow.PenWidth = penWidth
    Else
      clearWidth = clearWidth + 1
      GraphicsWindow.PenWidth = clearWidth
    EndIf
  EndIf
EndSub

Sub OnMouseMove
  x = GraphicsWindow.MouseX
  y = GraphicsWindow.MouseY
  If (Mouse.IsLeftButtonDown) then
    GraphicsWindow.DrawLine(prevX, prevY, x, y)
  endif
  prevX = x
  prevY = y
EndSub


提示:GraphicsWindow.LastKey属性中的D0到D9指数字键。
11/16/2008 10:25:00 上午

【Small Basic】练手小程序1之数的开方

0 Comments »
这是一个命令行程序。代码如下:TextWindow.Write("请输入一个数字:")
org = TextWindow.ReadNumber()
ClearLine()
TextWindow.Write("数字 ")
TextWindow.Write(org)
TextWindow.WriteLine(":")
number = 1
p = 0
TextWindow.Write("  ")
TextWindow.Write(org)
TextWindow.Write("^")
TextWindow.Write(p)
TextWindow.Write("=")
TextWindow.WriteLine(number)
number = number * org
p = p + 1
While (1=1)
  TextWindow.Write("  ")
  TextWindow.Write(org)
  TextWindow.Write("^")
  TextWindow.Write(p)
  TextWindow.Write("=")
  TextWindow.WriteLine(number)
  number = number * org
  p = p + 1
  PauseAndClearMessage()
EndWhile

Sub ChinesePause
  TextWindow.WriteLine("请按任意键继续. . .")
  TextWindow.PauseWithoutMessage()
EndSub

Sub PauseAndClearMessage
  ChinesePause()
  ClearLine()
EndSub

Sub ClearLine
  TextWindow.CursorTop = TextWindow.CursorTop - 1
  TextWindow.CursorLeft=0
  x = TextWindow.CursorTop
  TextWindow.WriteLine("                                                                                                        ")
  TextWindow.CursorTop=x
  TextWindow.CursorLeft=0
EndSub
特点是每次只显示一个,按下任意键才显示接下来的。

用到了一个Sub ClearLine,意思是清除光标上一行。其实这句如果用.NET写的话更简单。

程序作者:蓝蓝小雪
11/16/2008 09:22:00 上午

【Small Basic】微软推出Small Basic

0 Comments »
经过一年的封闭开发,微软上个月发布了Small Basic,一款针对儿童的免费编程语言。不像Scratch和Alice,这款工具属于“无编码”环境,本质上讲是一个简略版本的BASIC语言。
该语言脱胎于传统的BASIC语言,但是基于.Net开发框架重新建立。它有三个显著特点:

语言
只包含14个关键词,Small Basic是完全基于.Net开发框架运行的。

环境
Small Basic的开发环境非常简单,但是提供了只有专业开发人员才能使用的包含智能感知的IDE。


Small Basic有一系列的开发库,并允许用户创建新的库或者修改现有的。它还允许加在第三方开发库。



一份非常简单易懂的(我们通过一个9岁的孩子测试过)62页的PDF文档可以帮助你快速上手,每个对Small Basic感兴趣的人都可以下载。

40年前出现的BASIC语言经过了许多次改进,已经变得更加强大,特别是对于初学者来说。即使Small Basic主要针对的是儿童,许多编程的初学者都会对此感兴趣。


下载地址:http://msdn.microsoft.com/en-us/devlabs/cc950524.aspx

本博客以后也会更新关于 Small Basic 的东西!
11/16/2008 12:24:00 上午

【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.