解决方案 »
- 请大侠们帮助解决ssl vpn环境下的HttpWebRequest连不上服务器的问题
- !c#.net中dataGridView点击某一行,然后,弹出新窗口,然后在新窗口中修改数据,提交保存
- 如何获取iframe内网页的目录树控件点击时的值
- 请教一个treeview无刷新遍历以及获取选中值
- 高分求高人帮忙,如何通过代码实现重启本地连接
- 高分请教,C#怎么用代码清楚IE的临时文件!!!在线等待!!!!
- C#的类中如何使用ActiveX控件
- 如何将从Socket接受的非托管字节流转化成托管结构,还有就是如何将托管结构体转成非托管字节流从Socket发送
- 拖动改变控件大小,怎么实现的?类似Fiddler
- 關于在WEB應用程式中調用EXE可執行文件的問題
- C#如何向excel文件中插入另一个excel表
- combox.selectindex=-1 不能初始化下拉框
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace suzu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
button1.Enabled = false;
} private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == "")
button1.Enabled = false;
else
button1.Enabled = true;
foreach (char c in textBox1.Text)
{
if (c >= '0' && c <= '9' || c == '.' || c == '|')
{
//控制除数字以外可以输入"|","."
}
//else if (!IsDigit(textBox1.Text)) //这里不调用也可以但是如果大量文本框需要控制是否为数字就需要调用
//{
// MessageBox.Show("输入不能包含与非计算无关的字符", "错误提示");
// textBox1.Text = textBox1.Text.Substring(0, textBox1.TextLength - 1);
// textBox1.Focus();
//}
else
{
MessageBox.Show("输入不能包含与非计算无关的字符", "错误提示");
textBox1.Text = textBox1.Text.Substring(0, textBox1.TextLength - 1);
textBox1.Focus();
}
}
////textBox1.Text=textBox1.Text.Trim(); 貌似是去掉空格用的。这个程序里并未用到
} /// <summary>
/// 这里便是调用检查文本框是否为数字(这里的数字是指“整型”的数字)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public bool IsDigit(string str)
{
string subs = "";
for (int i = 0; i < str.Length; i++)
{
subs = str.Substring(i, 1);
if (!char.IsNumber(char.Parse(subs)))
{
return false;
}
}
return true;
} private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入成绩按回车结束或者点击计算按钮");
}
else
{
string[] mnber = textBox1.Text.Split('|');
int n = 0;
for (int i = 0; i < mnber.Length; i++)
{
try
{
if (double.Parse(mnber[i]) < 60)
{
n++;
}
}
catch
{
MessageBox.Show("运算出现非法字符", "错误");
textBox1.Text = "";
textBox1.Focus();
break;
}
}
label2.Text = "不及格的人数是:" + n.ToString() + "个";
}
} private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Convert.ToInt32(e.KeyChar) == 13)
{
if (textBox1.Text == "")
{
MessageBox.Show("请你输入成绩按回车结束或者点击计算按钮");
} else
{
string[] mnber = textBox1.Text.Split('|');
int n = 0;
for (int i = 0; i < mnber.Length; i++)
{
try
{
if (double.Parse(mnber[i]) < 60)
{
n++;
}
}
catch
{
MessageBox.Show("运算出现非法字符","错误");
textBox1.Text = "";
textBox1.Focus();
break;
}
}
label2.Text = "不及格的人数是:" + n.ToString() + "个";
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace suzu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
button1.Enabled = false;
} private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == "")
button1.Enabled = false;
else
button1.Enabled = true;
foreach (char c in textBox1.Text)
{
if (c >= '0' && c <= '9' || c == '.' || c == '|')
{
//控制除数字以外可以输入"|","."
}
//else if (!IsDigit(textBox1.Text)) //这里不调用也可以但是如果大量文本框需要控制是否为数字就需要调用
//{
// MessageBox.Show("输入不能包含与非计算无关的字符", "错误提示");
// textBox1.Text = textBox1.Text.Substring(0, textBox1.TextLength - 1);
// textBox1.Focus();
//}
else
{
MessageBox.Show("输入不能包含与非计算无关的字符", "错误提示");
textBox1.Text = textBox1.Text.Substring(0, textBox1.TextLength - 1);
textBox1.Focus();
}
}
////textBox1.Text=textBox1.Text.Trim(); 貌似是去掉空格用的。这个程序里并未用到
} /// <summary>
/// 这里便是调用检查文本框是否为数字(这里的数字是指“整型”的数字)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public bool IsDigit(string str)
{
string subs = "";
for (int i = 0; i < str.Length; i++)
{
subs = str.Substring(i, 1);
if (!char.IsNumber(char.Parse(subs)))
{
return false;
}
}
return true;
} private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入成绩按回车结束或者点击计算按钮");
}
else
{
string[] mnber = textBox1.Text.Split('|');
int n = 0;
for (int i = 0; i < mnber.Length; i++)
{
try
{
if (double.Parse(mnber[i]) < 60)
{
n++;
}
}
catch
{
MessageBox.Show("运算出现非法字符", "错误");
textBox1.Text = "";
textBox1.Focus();
break;
}
}
label2.Text = "不及格的人数是:" + n.ToString() + "个";
}
} private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Convert.ToInt32(e.KeyChar) == 13)
{
if (textBox1.Text == "")
{
MessageBox.Show("请你输入成绩按回车结束或者点击计算按钮");
} else
{
string[] mnber = textBox1.Text.Split('|');
int n = 0;
for (int i = 0; i < mnber.Length; i++)
{
try
{
if (double.Parse(mnber[i]) < 60)
{
n++;
}
}
catch
{
MessageBox.Show("运算出现非法字符","错误");
textBox1.Text = "";
textBox1.Focus();
break;
}
}
label2.Text = "不及格的人数是:" + n.ToString() + "个";
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace suzu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
button1.Enabled = false;
} private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == "")
button1.Enabled = false;
else
button1.Enabled = true;
foreach (char c in textBox1.Text)
{
if (c >= '0' && c <= '9' || c == '.' || c == '|')
{
//控制除数字以外可以输入"|","."
}
//else if (!IsDigit(textBox1.Text)) //这里不调用也可以但是如果大量文本框需要控制是否为数字就需要调用
//{
// MessageBox.Show("输入不能包含与非计算无关的字符", "错误提示");
// textBox1.Text = textBox1.Text.Substring(0, textBox1.TextLength - 1);
// textBox1.Focus();
//}
else
{
MessageBox.Show("输入不能包含与非计算无关的字符", "错误提示");
textBox1.Text = textBox1.Text.Substring(0, textBox1.TextLength - 1);
textBox1.Focus();
}
}
////textBox1.Text=textBox1.Text.Trim(); 貌似是去掉空格用的。这个程序里并未用到
} /// <summary>
/// 这里便是调用检查文本框是否为数字(这里的数字是指“整型”的数字)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public bool IsDigit(string str)
{
string subs = "";
for (int i = 0; i < str.Length; i++)
{
subs = str.Substring(i, 1);
if (!char.IsNumber(char.Parse(subs)))
{
return false;
}
}
return true;
} private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入成绩按回车结束或者点击计算按钮");
}
else
{
string[] mnber = textBox1.Text.Split('|');
int n = 0;
for (int i = 0; i < mnber.Length; i++)
{
try
{
if (double.Parse(mnber[i]) < 60)
{
n++;
}
}
catch
{
MessageBox.Show("运算出现非法字符", "错误");
textBox1.Text = "";
textBox1.Focus();
break;
}
}
label2.Text = "不及格的人数是:" + n.ToString() + "个";
}
} private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Convert.ToInt32(e.KeyChar) == 13)
{
if (textBox1.Text == "")
{
MessageBox.Show("请你输入成绩按回车结束或者点击计算按钮");
} else
{
string[] mnber = textBox1.Text.Split('|');
int n = 0;
for (int i = 0; i < mnber.Length; i++)
{
try
{
if (double.Parse(mnber[i]) < 60)
{
n++;
}
}
catch
{
MessageBox.Show("运算出现非法字符","错误");
textBox1.Text = "";
textBox1.Focus();
break;
}
}
label2.Text = "不及格的人数是:" + n.ToString() + "个";
}
}
}
}
}
错误是substring下标越界,不过你的代码太烂了,废话好多,bug成群,直接用我在另一个帖给你的代码吧。
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text.Trim(), @"^(\d+(\.\d+)?(\||$))+$"))
{ }
else
{
MessageBox.Show("输入不能包含与非计算无关的字符", "错误提示");
textBox1.Text = "";
}
}
textBox1.Text = textBox1.Text.Substring(0, textBox1.TextLength - 1);
//1、外层使用了foreach,textBox1.Text就变成ReadOnly了,不能改动。
//2、如果textBox1.TextLength == 0,textBox1.TextLength - 1则会报数组越界的错误。
MessageBox.Show("输入不能包含与非计算无关的字符", "错误提示");
textBox1.Text = textBox1.Text.Substring(0, textBox1.TextLength - 1);
textBox1.Focus();
就算你这段代码合理,既然都已经出现错误了,就应该跳出来了,为什么还要继续?况且你这段代码写的也不合理,假如字符串长度为0,你这个就报错了个人见解
{
return (Regex.IsMatch(str, @"^[0-9]+$"));
}
根本原因已经告诉你了,foreach循环不能对集合惊醒删除操作,你程序运行的时候又没进行删除操作当然不会报错你把foreach循环换了就好了,其他代码都不用改
下面是一个限制只能输入数字、正负符号的控件代码供参考 /// <summary>
/// 数字输入文本框,仅可输入整数或小数
/// </summary>
[Description("数字输入文本框,仅可输入整数或小数")]
[ToolboxBitmap("NumericTextBox.bmp")]
public class NumericTextBox: TextBox
{
private int _dotCount = 0; //小数位数
private bool _positive = false; //是否允许输入负数 private bool _isValidChar = false; //输入的是否是合法字符
private string _validStr = "0123456789."; //合法的字符数组字符串(默认不许输入负数) /// <summary>
/// 小数部分的最大长度,如果小于 0,表示可以输入任何字符
/// </summary>
[Description("小数部分的最大长度,如果小于 0,表示可以输入任何字符"), Browsable(true), DefaultValue(0)]
public int DotCount
{
get { return _dotCount; }
set
{
if (value < -1 || value > this.MaxLength)
throw new Exception("小数部分长度必须介于 -1 到 " + this.MaxLength.ToString() + " 之间。"); _dotCount = value;
if (!Validate(this.Text))
this.Text = "";
}
} /// 是否允许输入负数
/// <summary>
/// 是否允许输入负数
/// </summary>
[Description("是否允许输入负数"), Browsable(true), DefaultValue(false)]
public bool Positive
{
get { return _positive; }
set
{
_positive = value;
if (_positive)
_validStr = "0123456789-.";
else
_validStr = "0123456789.";
}
} /// 验证数据是否合法
/// <summary>
/// 验证数据是否合法
/// </summary>
/// <param name="str">要验证的字符串</param>
/// <returns></returns>
private bool Validate(string str)
{
//如果 _DotCount == -1,或字符串为空,不再验证,可以输入任何字符
if (_dotCount <= -1 || str == "")
return true; string str1 = this.Text.Substring(0, this.SelectionStart);
string str2 = this.Text.Substring(this.SelectionStart, this.Text.Length - this.SelectionLength - this.SelectionStart);
str = str1 + str + str2; //如果结果仅仅是“+”或“-”,允许输入
if (str == "+" || str == "-")
return true; //判断是否可以转换成数字
double doubleValue = 0;
if (!double.TryParse(str, out doubleValue))
return false; //如果小数位数为 0,表示只能输入整数
if (_dotCount == 0)
return str.IndexOf('.') < 0; //判断小数位数是否超过了指定的长度
int dotIndex = str.IndexOf('.');
if (dotIndex >= 0 && str.Length - dotIndex - 1 > _dotCount)
return false; return true;
} /// 重写 OnKeyDown 函数,处理按键
/// <summary>
/// 重写 OnKeyDown 函数,处理按键
/// </summary>
/// <param name="e"></param>
protected override void OnKeyDown(KeyEventArgs e)
{
_isValidChar = false; //系统默认快捷键的处理
if ((e.Control && e.KeyCode == Keys.C) //复制
|| (e.Control && e.KeyCode == Keys.Z) //撤销
|| (e.Control && e.KeyCode == Keys.V) //粘贴
|| (e.Control && e.KeyCode == Keys.X) //剪切
|| e.KeyCode == Keys.Back) //退格
_isValidChar = true;
else
_isValidChar = false; base.OnKeyDown(e);
} /// 重写的 OnkeyPress 函数,处理按键
/// <summary>
/// 重写的 OnkeyPress 函数,处理按键
/// </summary>
/// <param name="e"></param>
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (!_isValidChar)
{
if (_dotCount > -1 && _validStr.IndexOf(e.KeyChar) < 0)
e.Handled = true;
else
e.Handled = !Validate(e.KeyChar.ToString());
} base.OnKeyPress(e);
} /// 重写粘贴消息,非法的字符不能粘贴
/// <summary>
/// 重写粘贴消息,非法的字符不能粘贴
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0302)
{
string clipStr = Clipboard.GetText();
if(!Validate(clipStr))
return;
} base.WndProc(ref m);
} }