马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

这是一个关于sender的问题

  [复制链接]
查看5336 | 回复4 | 2014-11-30 16:58:11 | 显示全部楼层 |阅读模式
这是代码
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Operator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
            }
        }

        private void txtNum1_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox currentTextBox = sender as TextBox;
            if (e.KeyChar < '0' || e.KeyChar > '9')     //不是数字
            {
                e.Handled = true;
            }
            if (e.KeyChar == 8)
            {
                e.Handled = false;
            }
            if (e.KeyChar == 46)
            {
                if (currentTextBox.Text.IndexOf(".") == -1)
                {
                    if (currentTextBox.SelectionStart > 0)
                    {
                        e.Handled = false;
                    }
                }
            }

        }

        private void btnCompute_Click(object sender, EventArgs e)
        {
            double num1 = Convert.ToDouble(txtNum1.Text);
            double num2 = Convert.ToDouble(txtNum2.Text);
            switch (cboOperator.Text)
            { 
                case "+":
                    lblResult.Text = (num1 + num2).ToString();
                    break;
                case "-":
                    lblResult.Text = (num1 - num2).ToString();
                    break;
                case "*":
                    lblResult.Text = (num1 * num2).ToString();
                    break;
                case "/":
                    lblResult.Text = (num1 / num2).ToString();
                    break;
            }
        }

        
    }
}

这是应用界面


代码的目的是让两个textbox开头不能输入小数点且只能输入一个小数点,为什么我的第二个textbox达不到这样的效果

360截图20141130165510686.jpg
独孤求败 | 2014-11-30 16:59:54 | 显示全部楼层
这是打包的程序

Operator.zip

55.35 KB, 下载次数: 1

ibcadmin | 2014-11-30 17:52:43 | 显示全部楼层
txtNum1_KeyPress    因为你只给 txtNum1给了键盘事件 没有给txtNum2事件   
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
独孤求败 | 2014-11-30 18:47:30 | 显示全部楼层
ibcadmin 发表于 2014-11-30 17:52
txtNum1_KeyPress    因为你只给 txtNum1给了键盘事件 没有给txtNum2事件

我知道,所以我用了
[AppleScript] 纯文本查看 复制代码
Textbox cureetTextbox=sender as Textbox
  按理会有效果的,但是达不到啊
ibcadmin | 2014-11-30 18:52:13 | 显示全部楼层
独孤求败 发表于 2014-11-30 10:47
我知道,所以我用了[mw_shl_code=applescript,true]Textbox cureetTextbox=sender as Textbox[/mw_shl_co ...

你的text2绑定这个事件了么? 没绑定绑啊
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则