如何选择复选框并在文本框中显示值 [英] How to select checkbox and display values in textboxes

查看:171
本文介绍了如何选择复选框并在文本框中显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Checkboxlist,按钮和2个文本框



当选中复选框中的第一个值并按下按钮时,我想在其中一个文本框中显示它当我取消选中复选框时,我希望值从文本框中消失

I have created a Checkboxlist,button and 2 textboxes

When selecting the first value in the checkboxes and press button i want to display it in one of the textboxes and when i uncheck the checkboxes i want the value to disepear from the textboxes

if (CheckBoxPersonalInfo.SelectedIndex==0)
                {
                    LabelFirstName.Text = theEmpl.firstname;
                    

                    Label1.Text = CheckBoxPersonalInfo.SelectedItem.Value;
                   

                }
                else if (CheckBoxPersonalInfo.SelectedIndex != -1)
                {
                    LabelFirstName.Text = string.Empty;
                }
                if(CheckBoxPersonalInfo.SelectedIndex==1)
                {
                    LabelLastName.Text = theEmpl.lastname;
                }
                else if (CheckBoxPersonalInfo.SelectedIndex != -1)
                {
                    
                }
                {
                    LabelLastName.Text = string.Empty;

                }

推荐答案

这对您有所帮助,

你可以以下列方式获取复选框的值,



This may be helpfull to you,
you can get values of check box in following way,

bool didYouCheckTheBox = myCheckbox.IsChecked;  // returns true or false





现在剩下的就是那个,

只需按下按钮即可处理它。

所以你需要的完整代码可能是,



Now remaining is that,
Just handle it working on button click.
So full code you required may be like,

buttonClickFunction()
{
     bool didYouCheckTheBox = myCheckbox.IsChecked;  // returns true or false
     if(didYouCheckTheBox)
     {
         TextBox1.Text = "Checked";
         TextBox2.Text = "";
     }
     else
     {
        TextBox1.Text = "";
        TextBox2.Text = ""Un Checked";
     }
}


private void checkBox_CheckedChanged(object sender, EventArgs e)
{
    var checkboxes = from c in groupBox1.Controls.OfType<CheckBox>()
                        where c.Checked
                        orderby int.Parse(c.Name.Substring(8))
                        select c;

    sb.Clear();

    foreach (var cb in  checkboxes)
    {
        sb.Append(string.Format("This is checkbox {0}", cb.Name.Substring(8)));
        sb.Append(Environment.NewLine);
    }

    textBox1.Text = sb.ToString();
}


这篇关于如何选择复选框并在文本框中显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆