Analytic Lunatic
Analytic Lunatic

Reputation: 3944

Call DateTimePicker.ValueChanged event in C#?

I have created the below code to let my users see how many records have been printed of the total that will be returned when a search is run using current form selections.

I've got my code functioning for when the value changes for my DateTimePicker control (different conditions, new query, new record count). Now I'm trying to call and run this same code event when my combobox control named cmbLetterType has it's selected value changed.

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();
}

When I type dtpDate.ValueChanged(); however, I receive: The Event 'System.Windows.Forms.DateTimePicker.ValueChanged' can only appear on the left hand side of += or -=? Can anyone help me with this; pretty sure I'm overlooking something very basic.

I suppose absolute worst case I could completely copy the code, but that just seems like a bad idea when it comes to possible future maintenance.

Upvotes: 0

Views: 2600

Answers (1)

Jeff B
Jeff B

Reputation: 8982

Easy!

Just take everything inside your event-handler dtpDate_ValueChanged and put it in another function... give it a good, descriptive name. Now call it from both places! ;)

private void dtpDate_ValueChanged(object sender, EventArgs e)
{
    DoTheStuff();
}

private void cmbLetterType_SelectedValueChanged(object sender, EventArgs e)
{
    DoTheStuff();
}

private void DoTheStuff()
{
    // Code goes here...
}

Upvotes: 1

Related Questions