簡體   English   中英

單擊按鈕后在文本框中顯示復選框的文本

[英]Show the text of checkBox in a textBox after clicking a button

單擊按鈕后,光標應將位置更改為另一個 TextBox

private void btnOk_Click(object sender, EventArgs e)
{
    if (checkBox1.Checked)
        txtAffiche.Text = txtAffiche.Text + Environment.NewLine + checkBox1.Text;
    else if (checkBox2.Checked)
        txtAffiche.Text = txtAffiche.Text + Environment.NewLine + checkBox2.Text;
    else if (checkBox3.Checked)
        txtAffiche.Text = txtAffiche.Text + Environment.NewLine + checkBox3.Text;
    else if (checkBox4.Checked)
        txtAffiche.Text = txtAffiche.Text + Environment.NewLine + checkBox4.Text;

}

我猜您想根據選中的復選框將文本添加到txtAffiche TextBox框? 如果是這種情況,那么在使用CheckBoxes時 if/else 語句有些混亂。 CheckBoxes用於選中/取消選中單個項目。 當有超過 1 個CheckBox (s) 時,它們不像單選按鈕那樣作為一組工作,因此當超過 1 個時,只能選擇一 (1) 個單選按鈕。因此可以同時檢查多個CheckBox時間。

if/else/if/else 語句基本上只設置一 (1) 個checkbox.text名稱,即使所有復選框都被選中。 此 if/else/if/else 語句僅顯示 FIRST 復選框。 如果檢查了多個CheckBox這似乎很奇怪。 我猜你可能想顯示當前選中的所有CheckBox es。 我希望下面的代碼可以幫助。

private void button1_Click(object sender, EventArgs e) {
  StringBuilder sb = new StringBuilder();
  if (checkBox1.Checked)
    sb.Append(checkBox1.Text + " ");
  if (checkBox2.Checked)
    sb.Append(checkBox2.Text + " ");
  if (checkBox3.Checked)
    sb.Append(checkBox3.Text + " ");
  if (checkBox4.Checked)
    sb.Append(checkBox4.Text + " ");
  txtAffiche.Text = txtAffiche.Text + Environment.NewLine + sb.ToString();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM