请解决这些if语句. [英] plz solve these if statements.

查看:64
本文介绍了请解决这些if语句.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (txtaccount1.Text == "")                

                label17.Text = "<<";
                label31.Text = "All Fields are Required";
            if(txtaccount1.Text!="")
           
                label17.Text = "";
                label31.Text = "";

            if (txtaccounttitle.Text == "")
            
                label18.Text = "<<";
                label31.Text = "All Fields are Required";

            if (txtaccounttitle.Text != "")
            
                label18.Text = "";
                label31.Text = "";
            
 if (txtinitial.Text == "")
            
                label19.Text = "<<";
                label31.Text = "All Fields are Required";
            
       if (txtinitial.Text != "")
            
                label19.Text = "";
                label31.Text = "";

             if (cboaccounttype.SelectedIndex == -1)
            
                label27.Text = "<<";
                label31.Text = "All Fields are Required";
            
            if (cboaccounttype.SelectedIndex != -1)
            
                label27.Text = "";
                label31.Text = "";

            if (txtaccountnature.Text == "")
            
                label28.Text = "<<";
                label31.Text = "All Fields are Required";
            
            if (txtaccountnature.Text != "")
            
                label28.Text = "";
                label31.Text = "";

        if (txtpostal.Text == "")
            
                label25.Text = "<<";
                label31.Text = "All Fields are Required";
            
         if (txtpostal.Text != "")
            
                label25.Text = "";
                label31.Text = "";

        if (cbodistrict.SelectedIndex == -1)
            
                label24.Text = "<<";
                label31.Text = "All Fields are Required";
            
       if (cbodistrict.SelectedIndex != -1)
            
                label24.Text = "";
                label31.Text = "";

            if (cbocity.SelectedIndex == -1)
            
                label23.Text = "<<";
                label31.Text = "All Fields are Required";
            
          if (cbocity.SelectedIndex != -1)
            
                label23.Text = "";
                label31.Text = "";

           if (txtname1.Text == "")
            
                label20.Text = "<<";
                label31.Text = "All Fields are Required";
        if (txtname1.Text != "")
            
                label20.Text = "";
                label31.Text = "";            
            
      if (txtfather1.Text == "")
            
                label21.Text = "<<";
                label31.Text = "All Fields are Required";
            
             if (txtfather1.Text != "")
            
                label21.Text = "";
                label31.Text = "";

         if (txthusband1.Text == "")
            
                label22.Text = "<<";
                label31.Text = "All Fields are Required";
            
             if (txthusband1.Text != "")
            
                label22.Text = "";
                label31.Text = "";

           if (txtoccupation1.Text == "")
            
                label30.Text = "<<";
                label31.Text = "All Fields are Required";
            
        if (txtoccupation1.Text != "")
            
                label30.Text = "";
                label31.Text = "";

             if (txtmobile1.Text == "")
            
                label26.Text = "<<";
                label31.Text = "All Fields are Required";
            
          if (txtmobile1.Text != "")
            
                label26.Text = "";
                label31.Text="";

             if (txtcnic1.Text == "")
            
                label29.Text = "<<";
                label31.Text = "All Fields are Required";
            
             if (txtcnic1.Text != "")
            
                label29.Text = "";
                label31.Text = "";

            if (radioButton1.Checked == false && radioButton2.Checked == false)
            
                label33.Text = "<<";
                label31.Text = "All Fields are Required";
            
          if (radioButton1.Checked != false && radioButton2.Checked != false)
            
                label33.Text = "";
                label31.Text = "";
        
            if (radioButton1.Checked == true)
            
                a = radioButton1.Text;
            
        if (radioButton2.Checked == true)
            
                a = radioButton2.Text;

                String query = "insert into Accounts(Account_No,Account_title,Entry_Date,Initial_Deposite,Account_type,Account_Nature,Postal_Code,District,City,Name,Father_name,Husband_name,Occupation,Mobile,CNIC,Gender)values(@Account_No,@Account_title,@Entry_Date,@Initial_Deposite,@Account_type,@Account_Nature,@Postal_Code,@District,@city,@Name,@Father_name,@Husband_name,@Occupation,@Mobile,@CNIC,@Gender)";
                SqlCommand cmd = new SqlCommand(query, cn);
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@Account_No", txtaccount1.Text);
                //cmd.Parameters.Add("@Account_No", System.Data.SqlDbType.NVarChar).Value = txtaccount1.Text;
                cmd.Parameters.AddWithValue("@Account_title", txtaccounttitle.Text);
                // cmd.Parameters.Add("@Account_title", System.Data.SqlDbType.NVarChar).Value = txtaccounttitle.Text;
                cmd.Parameters.AddWithValue("@Entry_Date", dateTimePicker1.Value.Date);
                cmd.Parameters.AddWithValue("@Initial_Deposite", txtinitial.Text);
                cmd.Parameters.AddWithValue("@Account_type", cboaccounttype.SelectedItem);
                cmd.Parameters.AddWithValue("@Account_Nature", txtaccountnature.Text);
                //cmd.Parameters.AddWithValue("@Account_type", cboaccounttype.SelectedValue);
                //cmd.Parameters.AddWithValue("@Account_type", cboaccounttype.SelectedValue ?? DBNull.Value);
                cmd.Parameters.AddWithValue("@Postal_code", txtpostal.Text);
                // cmd.Parameters.AddWithValue("@City", cbocity.SelectedValue ?? DBNull.Value);
                // cmd.Parameters.AddWithValue("@District", cbodistrict.SelectedValue ?? DBNull.Value);
                cmd.Parameters.AddWithValue("@District", cbodistrict.SelectedItem);
                cmd.Parameters.AddWithValue("@City", cbocity.SelectedItem);
                cmd.Parameters.AddWithValue("@Name", txtname1.Text);
                cmd.Parameters.AddWithValue("@Father_name", txtfather1.Text);
                cmd.Parameters.AddWithValue("@Husband_name", txthusband1.Text);
                cmd.Parameters.AddWithValue("@Occupation", txtoccupation1.Text);
                cmd.Parameters.AddWithValue("@Mobile", txtmobile1.Text);
                cmd.Parameters.AddWithValue("@CNIC", txtcnic1.Text);
                cmd.Parameters.AddWithValue("@Gender", a);
                cmd.ExecuteNonQuery();
                MessageBox.Show("record stored");
            }
        }
    }
}

推荐答案

这很简单:缩进不能控制执行流程.
当您编写如下代码时:
It''s pretty simple: indentation does not control execution flow.
When you write code like:
if (txtaccount1.Text == "")
    label17.Text = "<<";
    label31.Text = "All Fields are Required";

对条件求值,如果为true,则执行 next语句.
如果不正确,则下一条语句不会执行,执行将直接传递到其后的语句.

不幸的是,

The condition is evaluated and if true, the next statement is executed.
if it is not true, then the next statement is not executed and execution passes directly to the statement after it.

Unfortunatly for you,

label17.Text = "<<";

是下一条语句,所以

is the next statement, so

label31.Text = "All Fields are Required";

将始终执行 ,无论条件是true还是false.
如果要通过if条件执行多个语句,则必须使用花括号将它们设置为一个语句块:

will always be executed, regardless of the if condition being true or false.
If you want to execute more than one statement as a result of the if condition, you must make them a statement block, using curly braces:

if (txtaccount1.Text == "")
    {
    label17.Text = "<<";
    label31.Text = "All Fields are Required";
    }





我要应用表单验证.如果任何字段为空,则必须填写所有字段",例如msg,而<<"必须显示在未填充的文本框中. msg"所有字段均为必填,并用符号"<<在空文本框中必须消失..."


好的.这是一个很正常的要求.我的处理方式似乎很复杂,但是请相信我,我已经做了很长时间了...:笑:

首先,更改标签的名称-不要为此使用VS默认值,因为它会使代码更难阅读,并且会鼓励错误的产生! :laugh:
代替"label31",将其称为"labFieldRequired"或类似名称.
代替"label17",将其命名为"labRequiredAccountNo"或类似名称.
很难读取数字并将它们与正在发生的事情联系起来,因此可以更容易地在不注意的情况下出错.

其次,为什么要更改标签文本以指示问题?为什么不修复文本,而改为更改Visible属性?这样,您可以在设计表单时看到表单上的标签,并确保它们对齐并看起来整洁.

第三,为什么要设定如此多次的整体结果?只需使用一个名为"validationFailed"的布尔值,将其预设为false,并在每次失败时将其设置为true.
然后,您只需检查一下,然后一次设置总体结果即可.

第四,为什么不使用单独的if语句,为什么不使用方法呢?





"i want to apply form validations. if any field is empty, "All fields are required" such msg must show and "<<" must show on such text box, which is not filled. when it is filled msg ""All fields are required" and sign "<<" on empty text box must vanish..."


OK. That''s a fairly normal requirement. The way I would do it may seem complex, but trust me, I''ve been doing this a long time...:laugh:

Firstly, change the names of your labels - don''t use the VS defaults for this as it make code harder to read, and encourages bugs to breed! :laugh:
Instead of "label31" call it "labFieldRequired" or similar.
Instead of "label17" call it "labRequiredAccountNo" or similar.
It''s difficult to read numerics and tie them to what is going on, so it makes it easier to get something wrong without noticing.

Second, why change the text of a label to indicate the problem? Why not fix the text, and change the Visible property instead? That way, you can see the labels on your form when you are designing them, and ensure that they line up well and look tidy.

Third, why set the overall result so many times? Just have a bool called "validationFailed", preset it to false, and set it to true each time you get a failure.
Then you just check it in the end, and set the overall result the once.

Fourth, instead of having individual if statement, why not use a method?

private bool Validate(bool condition, Label indicator)
   {
   indicator.Visible = !condition;
   return condition;
   }


然后,您的代码将变得更易于阅读:


Then your code becomes easier to read:

validationFailed |= Validate(!string.IsNullOrWhiteSpace(txtaccount1.Text), labRequiredAccountNo) ;
validationFailed |= Validate(!string.IsNullOrWhiteSpace(txtaccounttitle.Text), labRequiredAccountTitle);


(请注意,我不是在检查空字符串-IsNullOrWhiteSpace方法检查空值,空字符串和空格以确保数据确实存在!)
在此处使用"| ="表示如果任何一个或多个验证失败,它将始终将validationFailed 保留为true.


(Note that I''m not checking against an empty string - the IsNullOrWhiteSpace method checks for nulls, empty strings, and spaces to ensure that the data is really present!)
Using "|=" here means that if any one or more validation fails, it will always leave validationFailed as true.


在块中编写代码.

Write code in block.

if (txtaccount1.Text == "")
{
   label17.Text = "<<";
   label31.Text = "All Fields are Required";
}
if(txtaccount1.Text!="")
{
   label17.Text = "";
   label31.Text = "";
}
if (txtaccounttitle.Text == "")
{
    label18.Text = "<<";
    label31.Text = "All Fields are Required"
}
.
.
.
.

And then tell us your problem


这篇关于请解决这些if语句.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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