比方说,我在一个form上有3个textbox和1个button,我需要对textbox里面的内容进行限制,比方说不能为空。在我按下这个button的时候,如果其中某一个textbox的内容为空,就让这个控件获得焦点。可能说的不太清楚,大概就是这个意思,谢谢!!!

解决方案 »

  1.   

    if( textBox1.Text == "" )
    {
      textBox1.Focus();
      return ;
    }
      

  2.   

    楼主正解:)
    用控件的Focus()
      

  3.   

    这个方法我知道的,是我提问的方法不对,不好意思!!!
    这个方法对于控件数量比较少的时候是很好用的,但是我如果有10几个textbox,那我不是要每个都if一遍???
    有没有一种办法可以自动查询到是哪个控件???
      

  4.   

    在aspx页面
    function check()
    {      
    if (document.Form1.userid2.value =="")
    {
    alert("用户帐号不能为空!");
    document.Form1.userid2.focus ();
    return false;
    }
    if (document.Form1.paswd1.value =="")
    {
    alert("密码不能为空!");
    document.Form1.paswd1.focus ();
    return false;
    }
    }
    在.cs页面
    this.ImageButton5.Attributes.Add ("onclick","return check();");
      

  5.   

    这个好说,用一个循环就可以了:
    foreach (Control con in this.Controls)

    TextBox _tb = con as TextBox;
    if (_tb != null && _tb.Text.Trim()==string.Empty)
    {
    _tb.Focus();
    break;
    }
    }
      

  6.   

    foreach( Control c in this.Controls )
    {
           if( c is TextBox )
            //your work
    }
      

  7.   

    foreach(System.Windows.Forms.Control ct in this.Controls)
    {
              if(ct.GetType() == typeof(System.Windows.Forms.TextBox))
    {
                 if(ct.Text.Trim().Length ==0)
                {
                  ct.Focus();
                  return;
                 }
                    
    }
    }
      

  8.   

    你的界面上的textbox可能有多个空的,你准备把焦点给哪个?
      

  9.   

    你的10几个textbox是不是都放在form,还是放在form的panel上,
    foreach(System.Windows.Forms.Control ct in this.Controls)这个this这里指的是容纳textbox的容器,你的“对象引用设置到对象的实例”应该是这个ct为被定义~~~
    {
         if(ct.GetType() == typeof(System.Windows.Forms.TextBox))
         {
                if(ct.Text.Trim().Length ==0)
                {
                  ct.Focus();
                  return;
                 }
         }
    }
      

  10.   

    我后来改用了ComboBox代码如下                foreach (Control Ctrl in this.Controls)
                    {
                        ComboBox Cb = Ctrl as ComboBox;
                        if (Cb.SelectedText == null)
                        {
                            Cb.Focus();
                            break;
                        }
                    }还是上面那个错我现在有3个ComboBox,都放在同一个GroupBox里面!!!
      

  11.   

    private void button1_Click(object sender, EventArgs e)
            {
                foreach (Control tmp in this.Controls)
                {
                    TextBox textbox = tmp is TextBox ? (TextBox)tmp : null;
                    if (textbox != null & tmp.Text == "") tmp.Focus();
                }
            }
    给分!
      

  12.   

    你的窗体里都有什么样的控件呢?
    是不是只有一个Button和三个TextBox呢?
      

  13.   

    2005,我有满多控件的,现在不用textbox,用comboBox了只有三个comboBox,跟buton一起放在一个groupBox里面!!!
      

  14.   

    private void button1_Click(object sender, EventArgs e)
    {
    foreach (Control tmp in this.groupBox1.Controls)
    {
    if (tmp is ComboBox)
    {
    ComboBox cb = (ComboBox)tmp;
    if (cb.Text.Trim() == string.Empty)
    {
    cb.Focus();
    break;
    }
    }
    }
    }
      

  15.   

    在你的Button的Click事件中这样写。
    private void button1_Click(object sender, EventArgs e)
    {
        //获得被点击的Button
        Button btn1 = sender as Button;
        //如果转换没有成功,就退出
        if(btn1 == null)
        {
            return;
        }
        //因为你说你的Button和Combox控件放在同一个容器中
        //所以我们获得Button的父容器,然后遍历父容器中的控件
        foreach (Control ctrl in btn1.Parent.Controls)
        {
            //在这里声明可以避免多次声明
            //使用as转换,可以保证不抛出异常
            ComboBox cb = ctrl as ComboBox;
            //如果转换没有成功,就继续
            if(cb == null)
            {
                continue;
            }
            //使用Length判断可以提高效率。
            //本质上cb.Text.Trim() == String.Empty最终判断的还是两个字符串长度是否相等。
            if (cb.Text.Trim().Length == 0)
            {
                cb.Focus();
                //只设定第一个找到的长度为0的ComboBox控件的焦点
                break;
            }
        }
    }