我已經創建了下面的代碼,讓我的用戶可以查看當使用當前表單選擇運行搜索時將返回的總記錄數。在C#中調用DateTimePicker.ValueChanged事件?
我有我的代碼功能,當我的DateTimePicker控制(不同的條件,新的查詢,新的記錄計數)的值發生變化。現在我試圖調用並運行這個相同的代碼事件,當我的名爲cmbLetterType
的組合框控件將其選定值更改時。
private void dtpDate_ValueChanged(object sender, EventArgs e)
{
// If not first run when form loads and sets dtpDate to current value.
string qryCmd = "";
OdbcDataReader dr;
if (cnt > 1)
{
switch (cmbLetterType.SelectedIndex)
{
case 0:
docType = "oldAddr";
qryCmd = buildSearchQuery(docType);
break;
case 1:
docType = "newAddr";
qryCmd = buildSearchQuery(docType);
break;
case 2:
docType = "nameChg";
qryCmd = buildSearchQuery(docType);
break;
}
var newQry = qryCmd.Replace(qryCmd.Substring(0, qryCmd.IndexOf("FROM") - 1), "SELECT COUNT(*) AS COUNT");
var orderByIndex = newQry.IndexOf("ORDER BY");
newQry = newQry.Replace(newQry.Substring(orderByIndex, newQry.Length - orderByIndex), "");
dr = mdl.GetData(newQry);
while (dr.Read())
{
lblNumPrinted.Text = "# out of " + dr["COUNT"].ToString() + " printed";
}
// NEED TO PERFORM ANOTHER QUERY COMBINED WITH BAC000PF to see how many records have not been printed.
mdl.closeConn();
lblNumPrinted.Visible = true;
}
cnt++;
}
private void cmbLetterType_SelectedValueChanged(object sender, EventArgs e)
{
// ERROR ~~~~~~~~~
dtpDate.ValueChanged();
}
當我鍵入dtpDate.ValueChanged();
不過,我得到:The Event 'System.Windows.Forms.DateTimePicker.ValueChanged' can only appear on the left hand side of += or -=
?誰能幫我這個;很確定我忽略了一些非常基本的東西。
我想絕對是最壞的情況,我可以完全複製代碼,但是當涉及到將來可能的維護時,這似乎是個壞主意。
您是否嘗試過將事件連接起來? 'dtpDate.ValueChanged + =新的EventHandler(dtpDate_ValueChanged);' – James