| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1217 人关注过本帖
标题:[分享]我在编程中经常用的一些功能写成了类,希望能减少大家的写代码量
取消只看楼主 加入收藏
zhangbo0817
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-2-23
收藏
 问题点数:0 回复次数:1 
[分享]我在编程中经常用的一些功能写成了类,希望能减少大家的写代码量

using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;

namespace PublicClass
{
//数据验证类
public class DataCheck
{
public DataCheck() { }

/// <summary>
/// 验证文本框是否为空
/// </summary>
/// <param name="textBox">准备验证的文本框对象</param>
/// <returns>布尔值,当返回true时说明数据验证没有通过</returns>
public static bool CheckTextBoxIsNull(TextBox textBox)
{
if (textBox.Text.Trim() == "")
{
PublicClass.Msg.ShowWarning("请输入" + textBox.Tag.ToString());
textBox.Focus();
return true;
}

return false;
}

/// <summary>
/// 验证文本框是否能转换为数字
/// </summary>
/// <param name="textBox">准备验证的下拉框对象</param>
/// <returns>布尔值,当返回true时说明数据验证没有通过</returns>
public static bool CheckTextBoxIsDecimal(TextBox textBox)
{
if (textBox.Text.Trim() == "")
{
PublicClass.Msg.ShowWarning("请输入" + textBox.Tag.ToString());
textBox.Focus();
return true;
}

try
{
decimal temp = decimal.Parse(textBox.Text.Trim());
}
catch
{
PublicClass.Msg.ShowWarning(textBox.Tag.ToString() + "输入了非法字符");
textBox.Focus();
textBox.SelectAll();
return true;
}

return false;
}

/// <summary>
/// 验证文本框是否能转换为整型
/// </summary>
/// <param name="textBox"></param>
/// <returns>布尔值,当返回true时说明数据验证没有通过</returns>
public static bool CheckTextBoxIsInt(TextBox textBox)
{
if (textBox.Text.Trim() == "")
{
PublicClass.Msg.ShowWarning("请输入" + textBox.Tag.ToString());
textBox.Focus();
return true;
}

try
{
int temp = int.Parse(textBox.Text.Trim());
}
catch
{
PublicClass.Msg.ShowWarning(textBox.Tag.ToString() + "输入了非法字符");
textBox.Focus();
textBox.SelectAll();
return true;
}

return false;
}

/// <summary>
/// 验证组合框是否为空
/// </summary>
/// <param name="comboBox">准备验证的下拉框对象</param>
/// <returns>布尔值,当返回true时说明数据验证没有通过</returns>
public static bool CheckComboBoxIsNull(ComboBox comboBox)
{
if (comboBox.Text.Trim() == "")
{
PublicClass.Msg.ShowWarning("请输入" + comboBox.Tag.ToString());
comboBox.Focus();
return true;
}

return false;
}

/// <summary>
/// 验证组合框是否为整型
/// </summary>
/// <param name="comboBox"></param>
/// <returns></returns>
public static bool CheckComboBoxIsInt(ComboBox comboBox)
{
if (comboBox.Text.Trim() == "")
{
PublicClass.Msg.ShowWarning("请输入" + comboBox.Tag.ToString());
comboBox.Focus();
return true;
}

try
{
int temp = int.Parse(comboBox.Text.Trim());
}
catch
{
PublicClass.Msg.ShowWarning(comboBox.Tag.ToString() + "输入了非法字符");
comboBox.Focus();
comboBox.SelectAll();
return true;
}

return false;
}
}



//提示信息类
public class Msg
{
//构造函数
public Msg() { }


/// <summary>
/// 显示警告信息
/// </summary>
/// <param name="message">将要显示的警告信息</param>
public static void ShowWarning(string message)
{
MessageBox.Show(message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}


/// <summary>
/// 显示操作成功信息
/// </summary>
/// <param name="message">将要显示的操作成功信息</param>
public static void ShowInformation(string message)
{
MessageBox.Show(message, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}


/// <summary>
/// 显示询问信息
/// </summary>
/// <param name="message">将要显示的询问信息</param>
/// <returns>选择yes是返回true</returns>
public static bool ShowQuestion(string message)
{
bool flag = false;
if (MessageBox.Show(message, "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
flag = true;
}

return flag;
}
}
}

搜索更多相关主题的帖子: using System summary 
2007-03-08 02:08
zhangbo0817
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-2-23
收藏
得分:0 

当验证不能通过时会弹出提示,提示的内容是TextBox.Tag的内容,估计你没有设置该属性


2007-03-09 12:10
快速回复:[分享]我在编程中经常用的一些功能写成了类,希望能减少大家的写代码量 ...
数据加载中...
 
   



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

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