0

In my application I use TTabControl with 3 tabs: Main Tab (TabItem1) with 3 buttons: Button1 will take me to TabItem2, Button2 to TabItem3, and Button 3 to TabItem3.

I handle FormKeyUp Event to Control the Navigation and:

If the key pressed is vkHardwareBack then if the ActiveTab is the TabItem1, I popup a message asking if the user wants to quit the application. If the answer is yes, I close the application and if not nothing happens. this part is working just fine!

But if the ActiveTab is TabItem2 or TabItem3 then I want the application to go back to the Main Tab (TabItem1) by firing TTabChangeItem standard action with properties:

  • Tab: TabItem1
  • Direction: tdReversed
  • Transaction: ttSlide

But this is not happening. When the user is pressing the vkHardwareBack while the ActiveTab is TabItem2 or TabItem3 the application goes to background and the home screen is shown.

Any Idea what am I doing wrong?

And here is a the FormKeyUp procedure:

procedure TfMain.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char;
  Shift: TShiftState);
begin
  if Key = vkHardwareBack then
    if TabControl.ActiveTab = TabItem1 then
      if MessageDlg('Are you sure you want to Exit?', TMsgDlgType.mtWarning,
           [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], 0) = mrYes then
        MainActivitiy.finish
     else
       ChangeTabAction1.Execute; // I tried here also: TabControl.ActiveTab := TabItem2; but still the same results
end;
Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
Oussama Al Rifai
  • 323
  • 1
  • 6
  • 16
  • Please show your actual code. – Remy Lebeau Oct 28 '13 at 22:29
  • Here is FormKeyUp procedure: procedure TfMain.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState); begin if Key = vkHardwareBack then if TabControl1.ActiveTab = TabItem1 then if MessageDlg('Are you sure you want to Exit?', TMsgDlgType.mtWarning, [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], 0) = mrYes then MainAcitivity.finish else ChangeTabAction.Execute else end; Of course I tried explicit change of the Tab (TabControl.ActiveTab := TabItem1) but the result is the same anyway. – Oussama Al Rifai Oct 29 '13 at 16:25
  • Please update your original question to show code, do not put it in comments. And you might want to think about putting some `begin/end` blocks in the code to make it clearer which `if` statements each `else` statement goes with. – Remy Lebeau Oct 29 '13 at 19:29
  • Try now Remy. I added the code block. – Oussama Al Rifai Oct 30 '13 at 17:10

1 Answers1

0

Maybe try something like this (untested):

procedure TfMain.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
  if Key = vkHardwareBack then
  begin
    Key := 0;
    if TabControl.ActiveTab = TabItem1 then
    begin
      if MessageDlg('Are you sure you want to Exit?', TMsgDlgType.mtWarning, [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], 0) = mrYes then
      begin
        MainActivitiy.Finish;
      end;
    end else
    begin
      ChangeTabAction1.Execute;
    end;
  end;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks Remi, Addint `Key := 0; \\ Not Key := #0;` at the end of the procedeure code stopped showing the Home page. the application still active but it didn't lead to chage the tab to TabItem1. I wonder why! – Oussama Al Rifai Oct 31 '13 at 18:51
  • I even added the following code: `If ChangeTabAction1.Execute then ShowMessage('OK');`. I ran it step by step, it goes to ShowMessage() but it doesn't change the tab & doesn't shoe the "Ok" message! – Oussama Al Rifai Oct 31 '13 at 19:04
  • Did you try `MessageDlg()` instead of `ShowMessage()`? Aside from that, did you step into `ChangeTabAction` itself to see what the RTL is actually doing? I an not familiar with FireMonkey and do not know why you would have to use an `Action` to change the tab. In VCL, a Tab control has properties to change tabs directly. Does FireMonkey really not have the same thing? – Remy Lebeau Oct 31 '13 at 19:22
  • Well... `MessageDlg()` or `ShowMessage()` doesn't give any difference. Anyway, it works now after changing changing the Key Value to 0 immediately after the firt `if` and using `ChangeTabAction1.ExecuteTarget(self)`. Thanks Remy. It was useful discussion. – Oussama Al Rifai Nov 02 '13 at 07:46