Shrinavat 16 Posted December 25, 2018 (edited) Hi, How to correctly check if the parent menu item has at least one child item with Checked = True? Is it good way? function HasCheckedSubItems(AMenuItem: TMenuItem): Boolean; var i: integer; begin Result := False; for i := 0 to AMenuItem.Count - 1 do if AMenuItem.Items[i].Checked then Exit(True); end I'd appreciate any help you can give me. Thank you. Edited December 25, 2018 by Shrinavat solved? Share this post Link to post
dummzeuch 1505 Posted December 25, 2018 Looks good. I wouldn't mix result with exit(..), but that's probably a matter of taste. 1 Share this post Link to post
Bill Meyer 337 Posted December 28, 2018 On 12/25/2018 at 4:45 PM, dummzeuch said: Looks good. I wouldn't mix result with exit(..), but that's probably a matter of taste. The compiler encourages that mix, as it warns of the Result not having been initialized otherwise. 1 Share this post Link to post
Guest Posted December 30, 2018 The following function HasCheckedSubItems(AMenuItem: TMenuItem): Boolean; var I: integer; begin Result := False; for I := 0 to AMenuItem.Count - 1 do if AMenuItem.Items[I].Checked then begin Result := True; Exit; end; end; produces no warnings, no hints for me with 10.3 Tokyo. Share this post Link to post
Guest Posted January 1, 2019 On 12/30/2018 at 1:35 PM, Ondrej Kelle said: produces no warnings, no hints for me with 10.3 Tokyo. I meant 10.3 Rio, sorry. 🙂 Share this post Link to post
dummzeuch 1505 Posted January 1, 2019 On 12/30/2018 at 1:35 PM, Ondrej Kelle said: The following function HasCheckedSubItems(AMenuItem: TMenuItem): Boolean; var I: integer; begin Result := False; for I := 0 to AMenuItem.Count - 1 do if AMenuItem.Items[I].Checked then begin Result := True; Exit; end; end; produces no warnings, no hints for me with 10.3 Rio. Yes, but it does not use Exit(True) any more but requires an additional line: Result := True; Exit; And of course the begin / end block to allow for two statements.  This would be my preferred solution: function HasCheckedSubItems(AMenuItem: TMenuItem): Boolean; var I: integer; begin for I := 0 to AMenuItem.Count - 1 do if AMenuItem.Items[I].Checked then Exit(True); Exit(False); end; (But I rarely use any Delphi version that supports Exit() with a return value.) 1 Share this post Link to post