2013-04-16 35 views
1

我已經創建了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時= 「」;沒得到空

UPDATE

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

@Tianyun玲當我選擇第一個複選框,當我點擊兩個複選框都值disepears – Kriistiian

+0

你檢查我的回答修改它顯示? –

回答

-1

你-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 = ""; 
} 
+0

你的*答案*真的很混亂。 – phadaphunk

+0

@Tanyunun Ling。我在複選框列表中顯示名字和姓氏,當用戶選擇複選框名字顯示在文本框中或者如果用戶取消選中複選框,則文本框應該是空格,如果用戶同時選擇,則顯示在文本框中,以及如果用戶不選中這兩個複選框不顯示在文本框中的值 – Kriistiian

+0

@Tanyunun Ling是的,但我只是指出,我不應該使用= themEmply.lastname;我應該在checkboxlist中使用綁定值的索引,我該怎麼做? – Kriistiian

0

可以使用CheckedListBox.ItemCheck事件監視項目的經過&未選中狀態。

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 
    } 
} 
+0

尼斯將工作,但我個人更喜歡緊湊版本的小事件:) – phadaphunk

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 - 當複選框未選中時,文本框將被清除。
0

使用!的IsPostBack在這種方法

void page-load() 
    { 
    if(!IsPostBack) 
    { 
     //Call your CheckBoxList Binding method 
    } 
} 
+0

我得到索引超出範圍。必須是非負數且小於集合的大小。 參數名稱:索引 – Kriistiian

+0

se代碼在UPDATE – Kriistiian

+0

見我更新的答案! –