我创建了Checkboxlist
,Button
和两个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 = "";
}
}
}
答案 0 :(得分:0)
您可以使用CheckedListBox.ItemCheck
事件来监控Checked&未经检查的物品状态。
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
}
}
答案 1 :(得分:0)
首先,你应该真的尝试关注一些Naming Guildelines。像类这样的命名控件是一种非常糟糕的做法。
你可以这样试试:
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
- 取消选中此复选框后,文本框将被清除。答案 2 :(得分:0)
在此方法中使用!IsPostBack
void page-load()
{
if(!IsPostBack)
{
//Call your CheckBoxList Binding method
}
}
答案 3 :(得分:-1)
你的-1和0 in if条件确实令人困惑。并且代码的最后部分应该嵌套在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 = "";
}