1

I am creating a form in which search option is provided like this

I am working in a MDI form application.

My code is :

 private void btnSearch_Click(object sender, EventArgs e)
 {

    string query = null;
    if (txtBillNo.Enabled && txtBillNo.Text.Trim().Length != 0)
    {
        query = "Select BillNo,PartyName,City,State,FORMAT(BillDt,'dd-mm-yyyy')as Dt from BillMaster where BillNo=" + Convert.ToInt32(txtBillNo.Text.Trim());
        FillGrid(query);
    }
    else if (txtName.Enabled && txtName.Text.Trim().Length != 0)
    {
            query = "Select BillNo,PartyName,City,State,FORMAT(BillDt,'dd-mm-yyyy')as Dt from BillMaster where UCASE(PartyName) like '" + txtName.Text.Trim().ToUpper() + "'";
            FillGrid(query);
    }

    else
    {
        MessageBox.Show("Enter Value First");

        if (rdbName.Enabled)
        {
            txtName.Focus();

        }
        else if (rdbBillNo.Enabled)
        {
           txtBillNo.Focus();

        }

    }                 

}

The conditions are for checking whether the text boxes are left or not. If it is left blank then I am giving the message that user must enter the value. But when I am running the program it prompts twice, then I debugged the program and found that the Click event is called twice.

What should I do to tackle this problem.
Please help.

5
  • 1
    Did you register btnSearch.Click event more than one? Apr 18, 2013 at 11:27
  • Do you call btnSearch_Click method when app loaded ? Apr 18, 2013 at 11:28
  • maybe you registered to the event via dessiner and via your code?
    – Rémi
    Apr 18, 2013 at 11:30
  • Sir, I have registered the event in my load() event but not at design time.. @im_a_noob
    – Mohemmad K
    Apr 19, 2013 at 6:42
  • No I have registered the btnSearch_Click() once only in the Load() of form @DavutGürbüz
    – Mohemmad K
    Apr 19, 2013 at 6:43

4 Answers 4

2

In WPF you have to set e.Handled to True after FillGrid(query)
In win forms, delete and dont register btnSearch.Click in Load()

1
  • I am not finding Handled in my Button_Click() @Mustafa Ekici
    – Mohemmad K
    Apr 23, 2013 at 7:25
0

In Visual Studio Open CallStack and find out which function call Click event while debugging.

Debug->Windows->Call Stack

2
  • i think duplicated events are overwrited not added Apr 18, 2013 at 11:22
  • The click event works without saying its handled. I think it could be double registration for click event or an other call from FillGrid. The only way is inspecting Call Stack. Apr 18, 2013 at 11:25
0

If it's a WPF project make sure you don't have the click Handler registered in XAML and the project's C# code. I had the same issue and it was due to this reason.

0

I think the method signature might need to be changed to use RoutedEventArgs e

Then, at the end of the method body, you can set e.Handled to True

     private void btnSearch_Click(object sender, RoutedEventArgs e)
 {
    // do some stuff  
    e.Handled = True

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.