当前位置:编程学习 > C#/ASP.NET >>

一个简单的问题,为什么读不了ELSE里的内容

代码如下。
 private void button1_Click(object sender, EventArgs e)
        {
                     if (IsAllTextBoxEmpty()){

                        DialogResult a = MessageBox.Show("如果没有查询内容,将会显示所有车辆,是否继续", "提示信息", MessageBoxButtons.OKCancel);

                        if (a == DialogResult.OK)
                        {
                            Form3 form3 = new Form3();

                            form3.ShowDialog();

                        }
                    
            }
                        
                        else
                    {
                        Form3 form3 = new Form3();
                        string sql = "SELECT TOP 50 * FROM [clcp$]";
                        SqlDataAdapter dataAapter = new SqlDataAdapter(sql, DBhelper.connection);
                        DBhelper.connection.Open();
                        dataAapter.Fill(dataset, "clcp$");
                        DataGridView dataGridView1 = new DataGridView();
                        dataGridView1.DataSource = dataset.Tables["clcp$"];
                        DBhelper.connection.Close();
                        form3.ShowDialog();
    }   
                    
                
           }
  bool IsAllTextBoxEmpty()
        {
        TextBox tb;
        foreach (Control c in this.groupBox1.Controls)
        {
            tb = null;
            try { tb = (TextBox)c as TextBox; }
            catch { }

            if (tb != null)
            {
                if (tb.Text != "") 
                    return false;
            }
        }
    return true;


        }
          
这个else里的内容无论如何都读取不了,为什么,求大师解答
--------------------编程问答-------------------- 因为bool IsAllTextBoxEmpty()
总返回true的.你跟踪一下 --------------------编程问答--------------------  foreach (Control c in this.groupBox1.Controls)
  {
if( c is TexBox)
...
} --------------------编程问答-------------------- 我试了下你的代码没有问题。

我猜问题可能是你的groupBox1里没有东西,或者那些TextBox看起来在groupBox上面,但是实际上不是隶属于groupBox1 --------------------编程问答-------------------- 可能是你的textbox的值等于空 --------------------编程问答-------------------- 或者是3楼所说的 textbox不在groupBox上 --------------------编程问答-------------------- this.groupBox1.Controls.Add(this.TextBox1)有木有?! --------------------编程问答-------------------- 给LZ挑个毛病
tb = null;
  try { tb = (TextBox)c as TextBox; }
  catch { }

在try块中你用了两种强转方式,一、强制转换  二、利用as操作符

如果你利用第一种方式强制转换,可以添加异常处理模块 try...catch()
如果你利用第二种方式 as操作符的话,就不用添加异常处理模块了。因为其本身是不会发生异常的。如果转换失败的话,它会给一个NULL值。
--------------------编程问答-------------------- LZ说的ELSE部分不执行,那是因为你的IsAllTextBoxEmpty()方法老是为true --------------------编程问答--------------------
引用 8 楼 hamber_bao 的回复:
LZ说的ELSE部分不执行,那是因为你的IsAllTextBoxEmpty()方法老是为true

直接原因

断点试试是什么原因使IsAllTextBoxEmpty()方法老是为true
--------------------编程问答--------------------

        bool IsAllTextBoxEmpty()
        {
            TextBox tb = null;
            foreach (Control c in this.groupBox1.Controls)
            {
                tb = c as TextBox;
                if (tb == null)
                {
                    return false;
                }
                else
                {
                    return tb.Text == null;
                }
            }
            return false;
        }
--------------------编程问答--------------------

  tb = null;
  try
 {
       tb = (TextBox)c as TextBox;
 }
  catch { }
//我是没见过这样用as的。。。。。。
--------------------编程问答-------------------- 不好意思,刚写的不对,改了下

        bool IsAllTextBoxEmpty()
        {
            TextBox tb = null;
            bool b = true;
            foreach (Control c in this.groupBox1.Controls)
            {
                if (c is TextBox)
                {
                    tb = (TextBox)c;
                    if (String.IsNullOrEmpty(tb.Text))
                    {
                        b = false;
                    }

                }
            }
            return b;
        }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,