I wrote this code to handle the up down control handle it is associated with an edit box and populates a valuelist editor, all I want to do is ask a question if the user presses the down button on the up down control which deletes a row in the valuelist editor if yes delete row if not dont do anything. It seems to work fine but if you answer the question with a cancel then clicking anywhere on the form causes the up down control to be clicked again....Why???
void __fastcall TForm1::UpDown1Click(TObject *Sender, TUDBtnType Button) { //we are clicking up if Button is 0 if(Button == 0) { //insert new row with group number ValueListEditor2->Strings->Add("Group " + AnsiString(Edit1->Text) + "="); //we must be clicking down if Button is 1 } else { //user must press ok for this code to execute if(MessageBox(0, (LPSTR)"Do you really want to erase the group?", "ERROR", MB_ICONWARNING | MB_OKCANCEL) == IDOK) { ValueListEditor2->DeleteRow(ValueListEditor2->RowCount-1); //user must have pressed no } else { Edit1->Text = ValueListEditor2->RowCount-1; } } if(Edit1->Text == "0") ValueListEditor2->Visible = false; else ValueListEditor2->Visible = true; }
This behavor is usually appeared when there is process of other control before exiting the event handler... In a very few words works like a "buffer" of user clicks. When a process is made inside 2 brackets and nothing else needed add a return to get out of the function... If something else needed use a goto to go to the next process block...
void __fastcall TForm1::UpDown1Click(TObject *Sender, TUDBtnType Button) { //we are clicking up if Button is 0 if(Button == 0) { //insert new row with group number ValueListEditor2->Strings->Add("Group " + AnsiString(Edit1->Text) + "="); //we must be clicking down if Button is 1 ////if nothing else needed here add return... ////EXAMPLE: we have to go to other process later on code: ////goto Out; } else { //user must press ok for this code to execute if(MessageBox(0, (LPSTR)"Do you really want to erase the group?", "ERROR", MB_ICONWARNING | MB_OKCANCEL) == IDOK) { ////code came back from outside the function click... MAYBE interfere with the event handler... ValueListEditor2->DeleteRow(ValueListEditor2->RowCount-1); //user must have pressed no } else { Edit1->Text = ValueListEditor2->RowCount-1; } } Out: if(Edit1->Text == "0") ValueListEditor2->Visible = false; else ValueListEditor2->Visible = true; }
Thanks very much i tried something similiar using return but no such luck....what do you mean by ////code came back from outside the function click... MAYBE interfere with the event handler...
You may setting something used inside the event handler by calling a function and when code come back has to check again.... I made this particular "mistake" too many times...
All the above was to point out that in many places you may have a "bug" not by your mistake most of the times...
What I'm suggesting now: Place a breakpoint in thr first line of the event handler and see if it is triggered more times than those you designed originaly... If it is triggered then follow the code using F7 and see where it is happening...
This Error Ocurres because unlike other components, UpDown triggers it's OnClick event before MouseUp event, happens. To avoid this you, instead of using onmouseclick, put your code in OnMouseUp Event.