this.Bchk9 = new System.Windows.Forms.CheckBox();
            this.Bchk8 = new System.Windows.Forms.CheckBox();
            this.Bchk7 = new System.Windows.Forms.CheckBox();
            this.Bchk6 = new System.Windows.Forms.CheckBox();
            this.Bchk5 = new System.Windows.Forms.CheckBox();
            this.Bchk4 = new System.Windows.Forms.CheckBox();
            this.Bchk3 = new System.Windows.Forms.CheckBox();
            this.Bchk2 = new System.Windows.Forms.CheckBox();
            this.Bchk1 = new System.Windows.Forms.CheckBox();
            this.Bchk0 = new System.Windows.Forms.CheckBox();
            if (Bchk0.Checked == true)
            {
                bai.Add(Bchk0.Text);
            }
            if (Bchk1.Checked == true)
            {
                bai.Add(Bchk1.Text);
            }
            if (Bchk1.Checked == true)
            {
                bai.Add(Bchk1.Text);
            }            .......            if (bai.Count > 1)
            {                textBox1.Text =Bchk0.Text+Bchk1.Text+......+ "复式";
            }
            else
            {
                textBox1.Text = Bchk0.Text + "单式";
            }            这样写那么麻烦,怎么循环获取到“Bchk”的值,并赋给TextBox啊?
            checkbox的值分别是0,1,2,3,4,5,6,7,8,9            如果选择的是0,1,2,3,textbox里面的值就是0123

解决方案 »

  1.   

    foreach(Control al in Controls)
    {
    if(al is CheckBox)
    {
     if(((CheckBox)(al)).Checked)
    {
    textBox.Text+=......
    }
    }
    }
      

  2.   

    for(int i=0;i<10;i++)
      bai.Add((this.Controls["Bchk"+i] as CheckedBox).Text);
      

  3.   


    foreach(Control al in Controls) 

    if(al is CheckBox) 

    if(((CheckBox)(al)).Checked==true) 

    textBox.Text+=...... 


    }
      

  4.   

    foreach(Control control in this.controls)
    {
        if(control is checkbox)
        {
             if(control.checked) 
             {       
                textBox1.Text += control.Text; 
              }
         }
    }大概这个意思
      

  5.   

    为什么初学者往往会写这么诡异的语法呢?if (Bchk0.Checked == true) Bchk0.Checked本来就是一个bool 值,为什么要跟true作相等运算呢?如果你不嫌麻烦,可以:if (Bchk0.Checked == true==true==true==true...一直到天亮) 
      

  6.   


    怎么把这个取到的值,给textbox呢?
    10个checkbox,哪个选中了,就把哪个的text给textbox
      

  7.   

    主要是要循环遍历每一个checkbox,选择了则把他的text值赋给textbox,这个就如同楼主的思路,实现可以有其他的方法,如:foreach (Control c in this.Controls)
                {
                    if (c.GetType().ToString() == "System.Windows.Forms.CheckBox")
                    {
                        System.Windows.Forms.CheckBox aCheckBox = (System.Windows.Forms.CheckBox)c;
                        if(aCheckBox.Checked)
                            aTextBox.Text += aCheckBox.Text;
                    }
                }
      

  8.   

    this.Controls种的this必须是你checkbox的父类控件
      

  9.   


    foreach(Control al in Controls) 

    if(al is CheckBox) 

    if(((CheckBox)(al)).Checked) 

    textBox.Text+=al.Text


    }
    清楚了吗?
      

  10.   


    执行不到if(alis CheckBox) 这一步
      

  11.   

    本人初学者
    写了一个代码 
    自己运行了下 有结果public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                char [] array1=new char [5];
                foreach (System.Windows.Forms.CheckBox tb in panel1.Controls)
                {
                    if (tb.Checked == true)
                    {
                        array1 = tb.Name.ToCharArray();
                        text_show.Text += array1[4].ToString();
                    }
                }
            }
        }
      

  12.   

    哦 上面我是把所有的checkbox都放在一个panel1里面的
    个人习惯这样做
      

  13.   


    执行到if(alis CheckBox) ,就直接跳出循环,没往下执行了
      

  14.   


    哦 上面我是把所有的checkbox都放在一个panel1里面的
    个人习惯这样做if (tb.Checked == true) //这个也是习惯啊
                    {
                        array1 = tb.Name.ToCharArray();
                        text_show.Text += array1[4].ToString();
                    }
      

  15.   

    你试下 
    把我写的这个事件先放自己的程序里去
    先在界面上拖一个panel
    然后把以上的8个checkbox拖到panel上面去
    然后选中这8个checkbox
    在事件的CheckedChanged事件里面选择check_checkbox
    当然 你最好把我那段代码里的text_show换成你自己的文本框名字
      

  16.   

    foreach(Control control in this.controls) 

        if(control is checkbox) 
        { 
            if(control.checked) 
            {      
                textBox1.Text += control.Text; 
              } 
        } 
      

  17.   


    private void check_checkbox(object sender, EventArgs e)
            {
                text_show.Text = "";
                foreach (System.Windows.Forms.CheckBox tb in panel1.Controls)
                {
                    if (tb.Checked)
                    {
                        text_show.Text = tb.Name[4]+text_show.Text.ToString();
                    }
                }
            }
    怎么粘贴错了  - -
      

  18.   

    if (tb.Checked)
    这样就得了
      

  19.   

    没执行到 if (tb.Checked)
                    {
                        textBox1.Text = tb.Name[4] + textBox1.Text.ToString();
                    }
      

  20.   

    foreach (System.Web.UI.WebControls.GridViewRow dlm in this.GridView1.Rows)
    {
    CheckBox chk = (CheckBox)dlm.FindControl("CheckBox1");
    if (chk.Checked == true)
    {InsertorUpdate del = new InsertorUpdate();
    string sql = "delete from login where id in (" + int.Parse(dlm.Cells[0].Text) + ")";
    del.InsertorUpdatetable(sql);
    }
    }
    看看这个也许对你有帮助
      

  21.   


             for (int i = 0; i < 10; i++)
                {
                    bai.Add((this.Controls["Bchk" + i] as CheckBox).Text);
                    if (bai.Count > 1)
                    {
                        txtAll.Text = this.Controls["Bchk" + i].Text + "aa";
                    }            }这样写有什么问题吗?
    怎么不行呢
      

  22.   


    没执行到 if (tb.Checked)
                    {
                        textBox1.Text = tb.Name[4] + textBox1.Text.ToString();
                    }
    [/Quote]
    我怀疑你的panel1里面包含了其他控件,比如label,button什么的
    你执行不到if那句
    异常是不是出现在foreach (System.Windows.Forms.CheckBox tb in panel1.Controls)这句话里?
      

  23.   

    我怀疑你的panel1里面包含了其他控件,比如label,button什么的
    你执行不到if那句
    异常是不是出现在foreach (System.Windows.Forms.CheckBox tb in panel1.Controls)这句话里?
    [/Quote]没有呀.....
    646334933  帮我看一下......
      

  24.   


    没有呀.....
    646334933  帮我看一下......
    [/Quote]
    对方拒绝添加
    -.-
      

  25.   

    对方拒绝添加
    -.-
    [/Quote]不好意思,郁闷!
    可以了
      

  26.   

    public void AddChb()
            {
                int distance = 0;
                int xChb;
                int yChb;
                CheckBox chb;
                string txt1;
                string txt2;            for (int i = 0; i < 24; i++)
                {
                    if (i < 10)
                    {
                        txt1 = "0" + i;
                        if (i == 9)
                        {
                            txt2 = "10";
                        }
                        else
                        {
                            txt2 = "0" + (i + 1);
                        }
                    }
                    else
                    {
                        txt1 = i.ToString();
                        txt2 = (i + 1).ToString();
                    }
                    int i7 = i % 8;
                    if (i7 == 0 && i != 0)
                    {
                        distance += 20;
                    }
                    xChb = 5 + i7 * 55;
                    yChb = 15 + distance;                chb = new CheckBox();
                    chb.Name = txt2;
                    chb.AutoSize = true;
                    chb.Location = new Point(xChb, yChb);
                    chb.Text = txt1 + "-" + txt2;
                    chb.UseVisualStyleBackColor = true;
                    this.panel1.Controls.Add(chb);                chb.Click += new EventHandler(chb_Click);            }
            }        void chb_Click(object sender, EventArgs e)
            {
                textBox1.Text = "";
                foreach (CheckBox chbtemp in this.panel1.Controls)
                {
                    if (chbtemp.Checked)
                    {
                        textBox1.Text += chbtemp.Name + ",";
                    }
                }
            }不知道这个是你想实现的效果吗?
    添加一个panel1,一个textbox1