如果没有检查条件 [英] if condition is not checked

查看:49
本文介绍了如果没有检查条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,
这是我的代码

Hello friends,
this is my code

private void button1_Click(object sender, EventArgs e)
       {
           if (textBox1.Text=="")
           {
               MessageBox.Show("please enter the school Id");
           }
           else
           {
              // int m = Convert.ToInt32(textBox1.Text);
               sb.SCHOOLID = int.Parse(textBox1.Text);
           }
           if (textBox2.Text == "")
           {
               MessageBox.Show("please ente the school name");
           }
           else
           {
               sb.SCHOOLNAME = textBox2.Text.ToString();
           }
           sb.CREATEDDATEANDTIME = DateTime.Now;


当我将文本框留为空白时,如果条件不佳,则执行此代码时.可能是问题所在?


when executing this code if condition is chcking when i left the textbox as an empty.What could be the problem?

推荐答案

使用以下代码,这将对您有所帮助:

Use the following code, this will help you:

private void button1_Click(object sender, EventArgs e)
{
           if (textBox1.Text.Trim() == String.Empty)
           {
               MessageBox.Show("please enter the school Id");
           }
           else
           {
              // int m = Convert.ToInt32(textBox1.Text);
               sb.SCHOOLID = int.Parse(textBox1.Text);
           }
           if (textBox2.Text.Trim() == String.Empty)
           {
               MessageBox.Show("please ente the school name");
           }
           else
           {
               sb.SCHOOLNAME = textBox2.Text.ToString();
           }
           sb.CREATEDDATEANDTIME = DateTime.Now;
}


好吧,如果您的文本框中没有任何内容(甚至没有空格),那么如果条件为true并显示MessageBox.
Well, if there is nothing in your textbox (not even a space) then if condition will be true and MessageBox will be displayed.


private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                MessageBox.Show("please enter the school Id");
                return;
            }
            else if (string.IsNullOrEmpty(textBox2.Text.Trim()))
            {
                MessageBox.Show("please ente the school name");
                return;
            }
            else
            {
                // int m = Convert.ToInt32(textBox1.Text);
                sb.SCHOOLID =  Convert.ToInt32(textBox1.Text);
                sb.SCHOOLNAME = textBox2.Text.ToString();
                sb.CREATEDDATEANDTIME = DateTime.Now;
            }
        }


这篇关于如果没有检查条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆