我加了一个textbox和两个button,在一个button里给textbox赋值,另一个判断代码如下:
namespace shiyan
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void textBox1_TextChanged(object sender, EventArgs e)
        {        }        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = null;
        }        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == null)
                MessageBox.Show("T");
        }
    }
}运行后依次点击button1和button2,没有任何结果。如果在button赋值任意一个其他的,再在button2做相应的变动后,运行后就会弹出“T”的文本。大家说说这是,为什么啊,我刚学的c#,大家多多指教。

解决方案 »

  1.   

    你这样试试:
     private void button1_Click(object sender, EventArgs e) 
            { 
                textBox1.Text = “”; 
            }         private void button2_Click(object sender, EventArgs e) 
            { 
                if (textBox1.Text == “”) 
                    MessageBox.Show("T"); 
            } 
    看看能不能显示T文本
      

  2.   

    textBox1.Text =“”;
    设置成空就可以。别设置为null.
      

  3.   

        private void button1_Click(object sender, EventArgs e)
            {
                textBox1.Text = null;
            }        private void button2_Click(object sender, EventArgs e)
            {
                if (textBox1.Text == null)
                    MessageBox.Show("null");
                else if (textBox1.Text == "")
                    MessageBox.Show("empty");
            }
      

  4.   

     private void button1_Click(object sender, EventArgs e) 
            { 
                textBox1.Text = null; 
            }         private void button2_Click(object sender, EventArgs e) 
            { 
                if (textBox1.Text.Equals(""))
                    MessageBox.Show("T"); 
            } 
      

  5.   

     if (string.IsNullOrEmpty(textBox1.Text )==true )
                {
                    MessageBox.Show("text is null");
                }
      

  6.   

    你调度一下就知道,其实TEXT是""
      

  7.   

    微软推荐的方法是string.IsNullOrEmpty(textBox1.Text);
      

  8.   

    嗯,差不多就是这样,简化一下!if (string.IsNullOrEmpty(textBox1.Text ))
                 {
                     MessageBox.Show("text is null");
                 }
      

  9.   

    textbox的text 是个属性//
            // 摘要:
            //     获取或设置 System.Windows.Forms.TextBox 中的当前文本。
            //
            // 返回结果:
            //     在控件中显示的文本。
            public override string Text { get; set; }
            估计是判断过的,如果你是null,set里直接付成empty
      

  10.   

    如楼上所说,*.text获得的值类型就是string 那你得到的一定是“”
    和null得概念是不同的
      

  11.   

    null表示不引用任何对象,而对于 string 类型== 比较字符串的值,所以把null放在==的右边,是不对的