非映射的形式检查TextBox,ComboBox控件的值是否为空(是否被选中)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Effects;


namespace WpfApplication1
{
    //拖了3个TextBox控件,2个ComboBox控件到窗体
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           // comboBox1.Effect = new DropShadowEffect { Color = Colors.Red };
        }

        /// <summary>
        /// 检查TextBox控件的值是否为空。
        /// </summary>
        /// <param name="isOK">如果发现TextBox.Text的值为空,则将isOK设为false</param>
        /// <param name="textBox">要检查的TextBox集合</param>
        private void CheckTextBoxNotEmpty(ref bool isOK, params TextBox[] textBoxs)
        {
            foreach (TextBox txtBox in textBoxs)
            {
                if (txtBox.Text.Length <= 0)
                {
                    isOK = false;
                    //将当前的TextBox的背景设为红色
                    txtBox.Background = Brushes.Red;
                }
                else
                {
                    //否则将TextBox的背景色设为默认值
                    txtBox.Background = null;
                }
            }
        }
        /// <summary>
        /// 检查ComboBox控件的下拉菜单是否有选中值
        /// </summary>
        /// <param name="isOK">如果没选中就将isOK设为false</param>
        /// <param name="comboBoxs">要检查的控件</param>
        private void CheckComboBoxNotEmpty(ref bool isOK, params ComboBox[] comboBoxs)
        {
            foreach (ComboBox combo in comboBoxs)
            {
                if (combo.SelectedIndex < 0)
                {
                    isOK = false;
                    //将ComboBox控件的背景色设为红色
                    combo.Effect = new DropShadowEffect() { Color = Colors.Red };
                }
                else
                {
                    //将ComboBox控件的背景色设为默认
                    combo.Effect = null;
                }
               
            }
        }

        private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            //数据检查是否为空,默认通过的(也就是说默认是不为空,当用ref的形式将要检查的数据传给CheckTextBoxEmpty方法检查,如果检查到其中的某个控件的值为空的话,就将isOK设为false,然后给那个控件的背景色设为红色)
            bool isOK = true;

            //检查TextBox控件的值是否为空。
            CheckTextBoxNotEmpty(ref isOK,textBox1,textBox2,textBox3);

            //检查ComboBox控件的下拉菜单是否有选中值
            CheckComboBoxNotEmpty(ref isOK, comboBox1, comboBox2);
            
            //如果没有通过数据检查合法性(如果通过检查isOK为false的时候)则不保存
            if(!isOK)
            {
                return;
            }

                
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值