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;
}
}
}