1

我创建了一个CheckboxlistButton两个TextBoxes

在选择checkboxes和按下的第一个值时,button我想在其中一个中显示它,textboxes当我取消选中时,checkboxes我希望该值从该值中消失textboxes

if (CheckBoxPersonalInfo.Items[0].Selected)
            {
                LabelFirstName.Text = CheckBoxPersonalInfo.Items[0].Text;
            }
            else
            {
                LabelFirstName.Text = "";
            }

            if (CheckBoxPersonalInfo.Items[1].Selected)
            {
                LabelLastName.Text = CheckBoxPersonalInfo.Items[1].Text;
            }
            else
            {
                LabelLastName.Text = "";
            }

此代码工作正常,但取消选中 LabelFirstName.Text & LabelLastName.Text = ""; 不要空着

更新

private void ButtonOKCheckBoxes()
    {
        EMPLOYEE theEmpl;
        using (var db = new knowitCVdbEntities())
        {
            theEmpl = (from p in db.EMPLOYEES
                       where p.username == strUserName
                       select p).FirstOrDefault();
        }

        if (theEmpl != null)
        {
            PanelFullCv.Visible = true;
            LabelPleaseRegister.Visible = false;
            //CheckBoxPersonalInfo.Items[0].Text;
            if (CheckBoxPersonalInfo.Items[0].Selected)
            {
                LabelFirstName.Text = theEmpl.firstname;
            }
            else
            {
                LabelFirstName.Text = "";
            }
            //CheckBoxPersonalInfo.Items[1].Text;
            if (CheckBoxPersonalInfo.Items[1].Selected)
            {
                LabelLastName.Text = theEmpl.lastname;
            }
            else
            {
                LabelLastName.Text = "";
            }
        }

    }
4

4 回答 4

0

您可以使用该CheckedListBox.ItemCheck事件来监视 Item 的 Checked 和 Unchecked 状态。

private void Form1_Load(object sender, EventArgs e)
{
    checkedListBox1.ItemCheck += CheckedListBox1_ItemCheck;

}

private void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (e.NewValue == CheckState.Unchecked)
    {
        textBox1.Text = string.Empty;
        textBox2.Text = string.Empty;
    }
}        

private void button1_Click_1(object sender, EventArgs e)
{
    if (checkedListBox1.SelectedIndex > -1)
    {
        // set the data on text box
    }
}
于 2013-04-16T17:39:47.307 回答
0

首先,您应该真正尝试遵循一些命名准则。像类一样命名控件是一种非常糟糕的做法。

你可以这样尝试:

displayButton.Click += (s,e) => { 
displayTxtBox.Text = checkBoxPersonalInfo.SelectedItem.ToString(); 
};

checkBoxPersonalInfo_ItemCheck += (s,e) => { 
if (e.NewValue == CheckState.Unchecked)
displayTxtBox.Clear();
};

把这两个事件都放在你的form_Load里面,他们会做的是:

  • displayButton.Click- 单击按钮时,所选项目将显示在文本框中。
  • checkBox.CheckedChanged- 取消选中复选框时,文本框将被清除。
于 2013-04-16T17:41:38.140 回答
0

在此方法中使用 !IsPostBack

void page-load()
    {
    if(!IsPostBack)
    {
         //Call your CheckBoxList Binding method   
    }
}
于 2013-04-16T19:00:36.187 回答
-1

if 条件下的 -1 和 0 确实令人困惑。代码的最后一部分是否应该嵌套在 if 部分中?

也许你需要类似的东西:

if (CheckBoxPersonalInfo.Items[0].selected)
{
   LabelFirstName.Text = theEmpl.firstname;
}
else 
{
    LabelFirstName.Text = "";
}
if (CheckBoxPersonalInfo.Items[1].selected)
{
    LabelLastName.Text = theEmpl.lastname;
}
else 
{
    LabelLastName.Text = "";
}
于 2013-04-16T17:39:38.023 回答