2011-06-01 32 views
1
private void frmSearch_Load(object sender, EventArgs e) 
{ 
    // TODO: This line of code loads data into the 'bookdatabaseDataSet.Dist_Year' table. You can move, or remove it, as needed. 
    this.dist_YearTableAdapter.Fill(this.bookdatabaseDataSet.Dist_Year); 
    // TODO: This line of code loads data into the 'bookdatabaseDataSet.Dist_Auth' table. You can move, or remove it, as needed. 
    this.dist_AuthTableAdapter.Fill(this.bookdatabaseDataSet.Dist_Auth); 
    // TODO: This line of code loads data into the 'bookdatabaseDataSet.Book' table. You can move, or remove it, as needed. 
    this.bookTableAdapter.Fill(this.bookdatabaseDataSet.Book); 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    Form f4 = new Confirm(); 
    f4.Show(); 
    Hide(); 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    if (MessageBox.Show("Are you sure you want to Exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) 
    { 
     Application.Exit(); 
    } 
} 

我的問題是: 如果我沒有檢查任何複選框,我希望從窗體給我錯誤消息。它的正確代碼是什麼?我應該在哪裏對它?並且非常感謝您的關心。 form of windows application關於windows窗體複選框的問題

+2

C#語言沒有任何複選框。也許你在談論Windows窗體複選框,或ASP.NET複選框,或WPF複選框,或Silverlight複選框或其他複選框? – 2011-06-01 18:54:36

+0

是的,我正在談論Windows窗體checkBox!感謝你的關注! :) – 2011-06-02 18:28:26

回答

0

我想你想確保在點擊button1時至少檢查一個複選框,對不對?如果是這樣,請將其放在button1_Click事件的開始處。

private void button1_Click(object sender, EventArgs e) 
{ 
    if (!checkbox1.Checked && !checkbox2.Checked && !checkbox100.Checked) 
    { 
     MessageBox.Show("Please select a checkbox.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); 
     checkbox1.Focus(); 
    } 
    else 
    { 
     Form f4 = new Confirm(); 
     f4.Show(); 
     Hide(); 
    } 
} 
+0

是的,沒錯!十分感謝!我會嘗試這個代碼.. – 2011-06-02 18:30:13

2

對於每個複選框,執行checkbox.Checked測試(布爾AND)並顯示一個消息框。

如果您想防止應用程序關閉,那麼您必須處理關閉事件並在此情況下將CANCEL設置爲true。

void HandleFormClosing (object sender, CancelEventArgs args) 
{ 
    if (checkbox1.Checked && checkbox2.Checked) 
     return; 

    MessageBox.Show ("Need to check all boxes"); 
    args.Cancel = true; 
} 
+0

好的!我會嘗試這個代碼!非常感謝! ;) – 2011-06-02 18:29:07

0

你可以使用custom validation來做到這一點。

但你也可以擺脫複選框,並假設如果用戶鍵入文本框,然後他們想要在該字段上進行搜索。

從禁用搜索按鈕開始,只有當至少有一個字段中有文本時才啓用它。

+0

我會看看這種方式是否有效!謝謝你的答案.. :) – 2011-06-02 18:31:15