选中CheckBoxList的值放到TextBox中,再次选中从textBox中删除

当选中checkboxlist中的值,直接放到文本框中,在checkboxlist的SelectedIndexChanged事件下执行方法,

      //将选中的值放到文本框中
                for (int i = 0; i < cblSupport.Items.Count; i++)
                {
                    if (cblSupport.Items[i].Selected)
                    {
                        //如果文本框中不存在选中的内容,则在文本框中添加选中内容
                        if (txtSupport.Text.IndexOf(cblSupport.Items[i].Text) < 0)
                        {
                            this.txtSupport.Text += "," + cblSupport.Items[i].Text + ",";//可提供的支持
                        }
                        else
                        {//如果文本框中存在选中的内容,则在文本框中去掉选中内容
                            this.txtSupport.Text = this.txtSupport.Text.Replace(cblSupport.Items[i].Text , "");//可提供的支持
                        }
                    }
                }
                this.txtSupport.Text = this.txtSupport.Text.TrimStart(',');//去掉第一个逗号
                this.txtSupport.Text = this.txtSupport.Text.TrimEnd(',');//去掉最后一个逗号
                //将值放入文本框成功后,取消勾选,以防重复添加
                for (int i = 0; i < cblSupport.Items.Count; i++)
                {
                    if (cblSupport.Items[i].Selected)
                    {
                        this.cblSupport.Items[i].Selected = false;
                    }
                }

posted @ 2013-07-11 14:26  带着蜗牛去散步  阅读(729)  评论(0编辑  收藏  举报