| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2362 人关注过本帖
标题:[求助]小数点按钮的事件代码该怎么写
只看楼主 加入收藏
lijing88
Rank: 1
等 级:新手上路
威 望:1
帖 子:342
专家分:0
注 册:2006-11-8
收藏
得分:0 
可是编译出错啊!
它说不能用!='b'
还有也不能用>
运算符“<”无法应用于“System.Windows.Forms.TextBox”和“char”类型的操作数\
运算符“!=”无法应用于“System.Windows.Forms.TextBox”和“char”类型的操作数
就这个拉

2006-12-07 14:39
lijing88
Rank: 1
等 级:新手上路
威 望:1
帖 子:342
专家分:0
注 册:2006-11-8
收藏
得分:0 
以下是引用chenjin145在2006-12-7 14:39:13的发言:
if(cd.indexof('.')>-1) \\不懂耶
e.Handled = true
else
e.Handled = false

麻烦你给我翻译一下 行吗?


2006-12-07 14:40
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
如果cd中有. 返回.的位置  否則返回-1

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-12-07 14:43
lijing88
Rank: 1
等 级:新手上路
威 望:1
帖 子:342
专家分:0
注 册:2006-11-8
收藏
得分:0 
以下是引用chenjin145在2006-12-7 14:43:10的发言:
如果cd中有. 返回.的位置 否則返回-1

谢谢你哦!
我先试一下


2006-12-07 14:55
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
收藏
得分:0 
以下是引用chenjin145在2006-12-7 14:39:13的发言:
if(cd.indexof('.')>-1)
e.Handled = true
else
e.Handled = false

这样好像不能输入小数点了,LZ的要求是不能输入1个以上的小数点


2006-12-07 15:13
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
收藏
得分:0 
帮你找了段代码:慢慢学习吧

//基本的计算器
//蚕蛹 2001.11.26
//Using C#
//E-mail:sillnet@hotmail.com
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace wincalc
{
///
/// Summary description for calcForm.
///
public class calcForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button10;
private System.Windows.Forms.Button bClr;
private System.Windows.Forms.Button bDot;
private System.Windows.Forms.Button bPlus;
private System.Windows.Forms.Button bSub;
private System.Windows.Forms.Button bMul;
private System.Windows.Forms.Button bDiv;
private System.Windows.Forms.Button bEqu;
private System.Windows.Forms.TextBox txtCalc;

//以下是要添加的代码
//定义变量
Double dblAcc;
Double dblSec;
bool blnClear,blnFrstOpen;
String strOper;
//以上是添加的代码
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;

public calcForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

//以下是要添加的代码
//初始化设量
dblAcc=0;
dblSec=0;
blnFrstOpen=true;
blnClear=true;
strOper=new string('=',1);
//以上是添加的代码
}

///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.bPlus = new System.Windows.Forms.Button();
this.bMul = new System.Windows.Forms.Button();
this.bDot = new System.Windows.Forms.Button();
this.txtCalc = new System.Windows.Forms.TextBox();
this.bClr = new System.Windows.Forms.Button();
this.bDiv = new System.Windows.Forms.Button();
this.bSub = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.bEqu = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// bPlus
//
this.bPlus.BackColor = System.Drawing.SystemColors.Control;
this.bPlus.ForeColor = System.Drawing.SystemColors.ControlText;
this.bPlus.Location = new System.Drawing.Point(208, 112);
this.bPlus.Name = "bPlus";
this.bPlus.Size = new System.Drawing.Size(32, 80);
this.bPlus.TabIndex = 1;
this.bPlus.Text = "+";
//以下是要添加的代码
bPlus.Click += new System.EventHandler(this.btn_Oper);
//以上是添加的代码
//
// bMul
//
this.bMul.Location = new System.Drawing.Point(160, 112);
this.bMul.Name = "bMul";
this.bMul.Size = new System.Drawing.Size(32, 32);
this.bMul.TabIndex = 1;
this.bMul.Text = "*";
//以下是要添加的代码
bMul.Click += new System.EventHandler(this.btn_Oper);
//以上是添加的代码
//
// bDot
//
this.bDot.ForeColor = System.Drawing.Color.Black;
this.bDot.Location = new System.Drawing.Point(112, 208);
this.bDot.Name = "bDot";
this.bDot.Size = new System.Drawing.Size(32, 32);
this.bDot.TabIndex = 0;
this.bDot.Text = ".";
//以下是要添加的代码
bDot.Click += new System.EventHandler(this.btn_clk);
//以上是添加的代码
//
// txtCalc
//
this.txtCalc.Location = new System.Drawing.Point(16, 24);
this.txtCalc.Name = "txtCalc";
this.txtCalc.ReadOnly = true;
this.txtCalc.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.txtCalc.Size = new System.Drawing.Size(224, 21);
this.txtCalc.TabIndex = 2;
this.txtCalc.Text = "";
//
// bClr
//
this.bClr.BackColor = System.Drawing.SystemColors.Control;
this.bClr.ForeColor = System.Drawing.SystemColors.ControlText;
this.bClr.Location = new System.Drawing.Point(208, 64);
this.bClr.Name = "bClr";
this.bClr.Size = new System.Drawing.Size(32, 32);
this.bClr.TabIndex = 0;
this.bClr.Text = "AC";
//以下是要添加的代码
bClr.Click += new System.EventHandler(this.btn_clr);
//以上是添加的代码
//
// bDiv
//
this.bDiv.Location = new System.Drawing.Point(160, 160);
this.bDiv.Name = "bDiv";
this.bDiv.Size = new System.Drawing.Size(32, 32);
this.bDiv.TabIndex = 1;
this.bDiv.Text = "/";
//以下是要添加的代码
bDiv.Click += new System.EventHandler(this.btn_Oper);
//以上是添加的代码
//
// bSub
//
this.bSub.Location = new System.Drawing.Point(160, 64);
this.bSub.Name = "bSub";
this.bSub.Size = new System.Drawing.Size(32, 32);
this.bSub.TabIndex = 1;
this.bSub.Text = "-";
//以下是要添加的代码
bSub.Click += new System.EventHandler(this.btn_Oper);
//以上是添加的代码
//
// button8
//
this.button8.Location = new System.Drawing.Point(16, 64);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(32, 32);
this.button8.TabIndex = 0;
this.button8.Text = "7";
//以下是要添加的代码
button8.Click += new System.EventHandler(this.btn_clk);
//以上是添加的代码
//
// button9
//
this.button9.Location = new System.Drawing.Point(64, 64);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(32, 32);
this.button9.TabIndex = 0;
this.button9.Text = "8";
//以下是要添加的代码
button9.Click += new System.EventHandler(this.btn_clk);
//以上是添加的代码
//
// bEqu
//
this.bEqu.BackColor = System.Drawing.SystemColors.Control;
this.bEqu.ForeColor = System.Drawing.SystemColors.ControlText;
this.bEqu.Location = new System.Drawing.Point(160, 208);
this.bEqu.Name = "bEqu";
this.bEqu.Size = new System.Drawing.Size(80, 32);
this.bEqu.TabIndex = 1;
this.bEqu.Text = "=";
//以下是要添加的代码
bEqu.Click += new System.EventHandler(this.btn_equ);
//以上是添加的代码
//
// button10
//
this.button10.Location = new System.Drawing.Point(112, 64);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(32, 32);
this.button10.TabIndex = 0;
this.button10.Text = "9";
//以下是要添加的代码
button10.Click += new System.EventHandler(this.btn_clk);
//以上是添加的代码
//
// button4
//
this.button4.Location = new System.Drawing.Point(112, 160);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(32, 32);
this.button4.TabIndex = 0;
this.button4.Text = "3";
//以下是要添加的代码
button4.Click += new System.EventHandler(this.btn_clk);
//以上是添加的代码
//
// button5
//
this.button5.Location = new System.Drawing.Point(16, 112);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(32, 32);
this.button5.TabIndex = 0;
this.button5.Text = "4";
//以下是要添加的代码
button5.Click += new System.EventHandler(this.btn_clk);
//以上是添加的代码
//
// button6
//
this.button6.Location = new System.Drawing.Point(64, 112);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(32, 32);
this.button6.TabIndex = 0;
this.button6.Text = "5";
//以下是要添加的代码
button6.Click += new System.EventHandler(this.btn_clk);
//以上是添加的代码
//
// button7
//
this.button7.Location = new System.Drawing.Point(112, 112);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(32, 32);
this.button7.TabIndex = 0;
this.button7.Text = "6";
//以下是要添加的代码
button7.Click += new System.EventHandler(this.btn_clk);
//以上是添加的代码
//
// button1
//
this.button1.BackColor = System.Drawing.SystemColors.Control;
this.button1.ForeColor = System.Drawing.Color.Black;
this.button1.Location = new System.Drawing.Point(16, 208);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 32);
this.button1.TabIndex = 0;
this.button1.Text = "0";
//以下是要添加的代码
button1.Click += new System.EventHandler(this.btn_clk);
//以上是添加的代码
//
// button2
//
this.button2.Location = new System.Drawing.Point(16, 160);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(32, 32);
this.button2.TabIndex = 0;
this.button2.Text = "1";
//以下是要添加的代码
button2.Click += new System.EventHandler(this.btn_clk);
//以上是添加的代码
//
// button3
//
this.button3.Location = new System.Drawing.Point(64, 160);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(32, 32);
this.button3.TabIndex = 0;
this.button3.Text = "2";
//以下是要添加的代码
button3.Click += new System.EventHandler(this.btn_clk);
//以上是添加的代码
//
// calcForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(256, 261);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.txtCalc,
this.bEqu,
this.bDiv,
this.bMul,
this.bSub,
this.bPlus,
this.bDot,
this.bClr,
this.button10,
this.button9,
this.button8,
this.button7,
this.button6,
this.button5,
this.button4,
this.button3,
this.button2,
this.button1});
this.Name = "calcForm";
this.Text = "计算器";
this.ResumeLayout(false);

}
#endregion

//以下是要添加的代码
//小数点的操作
private void btn_clk(object obj,EventArgs ea){
if(blnClear)
txtCalc.Text="";
Button b3=(Button)obj;
txtCalc.Text+=b3.Text;
if(txtCalc.Text==".")
txtCalc.Text="0.";
dblSec=Convert.ToDouble(txtCalc.Text);
blnClear=false;
}

//程序开始点
private static void Main(){
Application.Run(new calcForm());
}

private void btn_Oper(object obj,EventArgs ea){
Button tmp=(Button)obj;
strOper=tmp.Text;
if(blnFrstOpen)
dblAcc=dblSec;
else
calc();
blnFrstOpen=false;
blnClear=true;
}

//等号运算
private void btn_equ(object obj,EventArgs ea){
calc();
}

//所有运算操作
private void calc(){
switch(strOper){
case "+":
dblAcc+=dblSec; //加号运算
break;
case "-":
dblAcc-=dblSec; //减号运算
break;
case "*":
dblAcc*=dblSec; //乘号运算
break;
case "/":
dblAcc/=dblSec; //除号运算
break;
}
strOper="="; //等号运算
blnFrstOpen=true;
txtCalc.Text=Convert.ToString(dblAcc);//将运算结果转换成字符类型,并输出
dblSec=dblAcc;//将运算数A的值放入运算数B中,以便后面运算
}

//清除按钮
private void btn_clr(object obj,EventArgs ea){
clear();
}

//清除按钮的操作
private void clear(){
dblAcc=0;
dblSec=0;
blnFrstOpen=true;
txtCalc.Text="";
txtCalc.Focus();//设置焦点为txtCalc
}
//以上是添加的代码
}

}

  以上只是一个简单的用C#开发的Windows Form程序,在vs.nt bate2+windows 2000专业版编译通过.向正在学习VS.net网友们抛砖引玉,其功能上还有很多不完善的地方,欢迎网友们将其完善。

2006-12-07 15:16
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
以下是引用jockey在2006-12-7 15:13:12的发言:

这样好像不能输入小数点了,LZ的要求是不能输入1个以上的小数点

if(cd.indexof('.')>-1 && (cd.indexof('.')==cd.lastindexof('.')))


[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-12-07 15:51
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
收藏
得分:0 
以下是引用chenjin145在2006-12-7 15:51:56的发言:

if(cd.indexof('.')>-1 && (cd.indexof('.')==cd.lastindexof('.')))

小数点后面的数咋办?输不进去了啊


2006-12-07 16:56
liu098
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-12-7
收藏
得分:0 

private void textBox3_TextChanged(object sender, EventArgs e)
{
int shu = 0;
for (int i = 0; i < textBox3.Text.Length; i++)
{

if ((textBox3.Text[i] < '0' || textBox3.Text[i] > '9') && textBox3.Text[i] != '.')
{

MessageBox.Show("只能输入数字!");
textBox3.Text = shishoujin;

}
if(textBox3.Text[i]=='.')
{
shu++;
if (shu > 1)
{
MessageBox.Show("注意输入的是否正确!");
textBox3.Text = shishoujin;
}
}
}
}

2006-12-07 17:10
shenba
Rank: 1
等 级:新手上路
威 望:1
帖 子:179
专家分:0
注 册:2006-9-17
收藏
得分:0 
以下是引用jockey在2006-12-5 13:53:01的发言:

水平不行,还是可以输入点:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != '\b')
{
if (e.KeyChar < '0' && e.KeyChar != '.' || e.KeyChar > '9' && e.KeyChar != '.')
{
e.Handled = true;
}
else

{
string cd = this.textBox1.Text.Trim();
int k = cd.Length;
int j = 0;
for (int i = 1; i <= k; i++)
{
if (cd.Substring(i-1, 1) == ".")
j++;
}

if (j > 1)
{

e.Handled = true; }
}
}

}

e.KeyChar != '\b' 是检查退格键
把判断是否有.的逻辑 改成 j>0 吧

[此贴子已经被作者于2006-12-7 18:55:09编辑过]


2006-12-07 18:34
快速回复:[求助]小数点按钮的事件代码该怎么写
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042422 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved