Use the OnChanging
page control event.
procedure TForm1.PageControl1Changing(Sender: TObject; var AllowChange: Boolean); begin if (self.PageControl1.TabIndex= 1)and (NotAllowUser = 'SomePerson') then begin AllowChange:= False; ShowMessage('Person not allow for this Tab'); end; end;
Ok, PageControle1.TabIndex is the activepageindex, not the one I want to select. How can I get the page clicked.
procedure TForm1.PageControl1Changing(Sender: TObject; var AllowChange: Boolean); var P: TPoint; NewTabIndex: Integer; begin P := PageControl1.ScreenToClient(Mouse.CursorPos); NewTabIndex := PageControl1.IndexOfTabAt(PX, Py); if (NewTabIndex= 1) then begin AllowChange:= false; Beep end; end;
New attempt
TMyPageControl = Class(TPageControl) private FNewTabSheet: TTabSheet; FOnMyChanging: TMyTabChangingEvent; procedure SetOnMyChanging(const Value: TMyTabChangingEvent); procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY; procedure CMDialogKey(var Message: TCMDialogKey); message CM_DIALOGKEY; protected function CanChange: Boolean; Override; public property OnMyChanging: TMyTabChangingEvent read FOnMyChanging write SetOnMyChanging; End; { TMyPageControl } function TMyPageControl.CanChange: Boolean; begin Result := True; if Assigned(FOnMyChanging) then FOnMyChanging(Self, FNewTabSheet ,Result); end; procedure TMyPageControl.CMDialogKey(var Message: TCMDialogKey); begin if (Focused or Windows.IsChild(Handle, Windows.GetFocus)) and (Message.CharCode = VK_TAB) and (GetKeyState(VK_CONTROL) < 0) then begin FNewTabSheet := FindNextPage(ActivePage, GetKeyState(VK_SHIFT) >= 0,True); SelectNextPage(GetKeyState(VK_SHIFT) >= 0); Message.Result := 1; end else inherited; end; procedure TMyPageControl.CNNotify(var Message: TWMNotify); var P: TPoint; NewTabIndex: Integer; begin with Message do case NMHdr.code of TCN_SELCHANGE: Change; TCN_SELCHANGING: begin Result := 1; P := self.ScreenToClient(Mouse.CursorPos); NewTabIndex := self.IndexOfTabAt(PX, Py); FNewTabSheet:= self.Pages[NewTabIndex]; if CanChange then Result := 0; end; end; end; procedure TMyPageControl.SetOnMyChanging(const Value: TMyTabChangingEvent); begin FOnMyChanging := Value; end;
source share