我创建了下面的代码,让我的用户看到当使用当前表单选择运行搜索时,将返回的总数中已经打印了多少条记录。
当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
中的所有内容都放入另一个函数中。。。给它取一个好的、描述性的名字。现在从两个地方调用它!)
private void dtpDate_ValueChanged(object sender, EventArgs e)
{
DoTheStuff();
}
private void cmbLetterType_SelectedValueChanged(object sender, EventArgs e)
{
DoTheStuff();
}
private void DoTheStuff()
{
// Code goes here...
}