软键盘开发

在mobile开发中,用户的输入是有一定困难的,所以,提供数字小键盘,可以极大方便用户的输入

我开发了一个简单的小键盘应用,界面很简单,如图:


图片

 

我选择的是把它做为一个用户控件,应用单键模式,使整个页面或整个程序中只有一个这样的小键盘实例存在,并提供与它关联的输入控件TextBox公共属性,及实现清空,关闭等按钮动作,当然,可以加输入的验证部分,这里我没有用到,把它注释掉了,相关代码如下,我把该用户控件的所有代码都复制过来了.

view plaincopy to clipboardprint?
public partial class UCNumeralKeyboard : UserControl  
{  
   #region 变量   
   //唯一的实例,私有变量   
   private static UCNumeralKeyboard numeralKeyboard = null;   
   //要在其中输入值的TextBox控件   
   private static TextBox textBox = null;  
   #endregion  
    #region 构造函数   
  private UCNumeralKeyboard()   
    {   
        InitializeComponent();   
    }  
    #endregion  
    #region 获得唯一实例的方法   
    public static UCNumeralKeyboard GetNumeralKeyBoard()   
    {   
        if (numeralKeyboard == null)   
        {   
            numeralKeyboard = new UCNumeralKeyboard();   
        }   
        return numeralKeyboard;   
    }  
    #endregion  

    #region 设置输入控件(属性)   
    public TextBox TB   
    {   
        set  
        {   
           textBox = value;   
        }   
    }  
    #endregion  
    #region 点击各按钮时的动作   
    private void btn1_Click(object sender, EventArgs e)   
    {   
        Button btn = sender as Button;   
        if (btn != null && textBox != null)   
        {  
            #region comment   
            switch (btn.Text)   
            {   
                case "1":   
                case "2":   
                case "3":   
                case "4":   
                case "5":   
                case "6":   
                case "7":   
                case "8":   
                case "9":   
                case "0":   
                    int indexMinus = textBox.Text.IndexOf("-");   
                    int indexBegin = 0;   
                    int indexDot = textBox.Text.IndexOf(".");   
                 if (indexDot < 0)   
                    {   
                        if (textBox.Text.Length == 0 || textBox.Text.Substring(indexBegin, 1) != "0" || indexDot > 0)   
                        {   
                            textBox.Text += btn.Text;   
                      }   
                    }   
                    else   
                    {   
                        indexBegin = 1;   
                       if (textBox.Text.Length == 1 || textBox.Text.Substring(indexBegin, 1) != "0" || indexDot > 0)   
                       {   
                            textBox.Text += btn.Text;   
                      }   
                    }   

                    break;     
               case "-":   
                   if (textBox.Text.IndexOf("-") < 0)   
                   {   
                        textBox.Text = "-" + textBox.Text;   
                    }   
                    else   
                    {   
                        textBox.Text = textBox.Text.Substring(1);   
                    }   
                    break;   
               case ".":   
                   if (textBox.Text.IndexOf(".") < 0)   
                   {   
                        if ((textBox.Text.IndexOf("-") >= 0 && textBox.Text.Length > 1)   
                            || (textBox.Text.IndexOf("-") < 0 && textBox.Text.Length > 0)   
                            )   
                       {   
                           textBox.Text += btn.Text;   
                        }   
                   }   
                   break;        
            }  
            #endregion   
            if (textBox.Text.Trim().Length < textBox.MaxLength)   
            {   
                textBox.Text += btn.Text;   
            }   
        }   
    }  
    #endregion  
    #region 关闭按钮动作   
    private void btnClose_Click(object sender, EventArgs e)   
    {   
        this.Parent.Controls.Remove(this);   
    }  
    #endregion  
    #region 清空关联的TextBox控件   
    private void btnClear_Click(object sender, EventArgs e)   
    {   
        textBox.Text = "";   
    }  
    #endregion  

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值