Adding image to TTabItem in Firemonkey


I have a TTabControl with a single tab that displays an image on the tab when a field in the tab has contents. I’ve assigned an ImageList to the TTabControl. When I add additional TTabItems to the TTabControl, I am unable to assign an ImageIndex later in the code. Here is how I’m adding the new tab:

procedure TfrmAddEditItem.LoadCustomFields;
NewTab: TTabItem;

NewTab := TTabItem.Create(tabPictures);
NewTab.Name := ‘tab2’;
NewTab.Text := FieldByName(‘FieldName’).AsString;
NewTab.Visible := True;
NewTab.Enabled := True;
NewTab.Tag := tabPictures.TabCount + 1;
NewTab.Parent := tabPictures;


But in another function in the form, when I try to set the image on the tab I get an Access violation exception:

procedure TfrmAddEditItem.LoadDataFields;
FormFieldName: string;
DBFieldName: string;

FormFieldName := ‘tab’ + FieldByName(‘FieldID’).AsString;
DBFieldName := ‘Field’ + FieldByName(‘FieldID’).AsString;
Tab := TTabItem(Self.FindComponent(FormFieldName));
if FieldByName(DBFieldName).IsNull then
Tab.ImageIndex := -1
Tab.ImageIndex := 0;


How can I set the ImageIndex value programatically on a TTabItem I added within the code? (Note that there are other events where I will also want to adjust the image based on the value in a database field.)

Comments are closed.