Why the child panel do not assume its color? - c++builder

Now I am struggling to set the color of a child panel placed on another one.
The goal is to represent fractions of time with certain properties over the whole period, represented by a TPanel on the background(Lime Green).
I do not understand, the code I am using has already worked, but now the child panel keeps the same color of the parent panel.
int IntSize = SecondsBetween( CurrInterrup->TerminoInt, CurrInterrup->InicioInt );
int dura = SecondsBetween( Indisp->DtHoraFim, Indisp->DtHoraIni);
int left = SecondsBetween( Indisp->DtHoraIni, CurrInterrup->InicioInt);
int width = RoundTo(((double)dura /(double) IntSize) *(double) PnGreen->Width, 0);
left = RoundTo(((double)left/(double)IntSize) * (double)PnGreen->Width, 0);
TPanel *pn = new TPanel(this);
pn->ParentColor = false;
pn->BorderStyle = bsSingle;
pn->BevelKind = bkSoft;
pn->Color = clRed;
pn->Left = left;
pn->Width = width;
pn->Height = PnGreen->Height -5;
pn->Parent = PnGreen; // A Panel instantiated at design time of color clLime
pn->Update();
The panel "pn" is shown with the correct properties of left, height and width and position, but the color is clLime instead of clRed.
Is there any error on the code?
Thank you very much.
Kind Regards.

Try setting the panel's ParentBackground property to false.
If ParentBackground is True, the control uses the parent's theme background to draw its own background.
If ParentBackground is False, the control uses its own properties, such as Color, to draw its background.

Related

TLabel.AutoSize not working when Label.Align = alTop

In a Delphi 10.4.2 32-bit Delphi VCL Application, I have a TLabel set on top of a TCard:
object lblColorTransparencyInfo: TLabel
AlignWithMargins = True
Left = 5
Top = 37
Width = 156
Height = 20
Margins.Left = 5
Margins.Top = 5
Margins.Right = 5
Margins.Bottom = 5
Align = alTop
Caption =
'Pick a color in the image to make that color transparent in the ' +
'whole image'
Color = clInfoBk
ParentColor = False
Transparent = False
WordWrap = True
ExplicitTop = 0
end
Label.Color is set to clInfoBk, so you can visually check the Label's size.
However, despite the Label.AutoSize is set to True, the Label's HEIGHT is much higher than its text height, despite Label.AutoSize = True:
Is this a bug in TLabel.AutoSize?
How can I set the Label Height to its correct text-height? (Please note that the Label's width could dynamically change during run-time which would also dynamically change the text-height at run-time).
This is taken from the documentation for the TCustomLabel.AutoSize property:
When AutoSize is False, the label is fixed in size. When AutoSize is True, the size of the label readjusts whenever the text changes. The size of the label is also readjusts [sic] when the Font property changes.
When WordWrap is True, the width of the label is fixed. If AutoSize is also True, changes to the text cause the label to change in height. When AutoSize is True and WordWrap is False, the font determines the height of the label, and changes to the text cause the label to change in width.
It only promises to change the size when the text or font is changed -- not when the label is resized due to its parent being resized. So one could argue that there is no bug here:
But in any case, one very quick and dirty solution is to tell the label to autosize when it is resized. Using an interposer class,
type
TLabel = class(Vcl.StdCtrls.TLabel)
protected
procedure Resize; override;
end;
implementation
{ TLabel }
procedure TLabel.Resize;
begin
inherited;
AdjustBounds;
end;
we can make it work (almost):
Of course, you could make your own TLabelEx control with this addition so you can use it as easily as the standard label.

How to render a UIButton in Xamarin.iOS

How do I render a UIButton in Xamarin.iOS? See the current Code for the full list.
This is the code I'm using to create and add the button to the Xamarin.Forms.Platform.iOS.CellTableViewCell cell. I cannot get the button to display anything.
With the use of a Foundation.NSMutableAttributedString, it shows a cut-off section of text in the top left corner, regardless of anything I try (alignments, insets, bounds, various constraints, etc). I'm currently trying things from Xamarin.Forms.Platform.iOS.Renderers.ButtonRenderer, but still can't get anything to display at all, no text, no button, or its outline.
If you could fork the repo and fix it or post the solution here, I would be very grateful.
protected override void SetUpContentView()
{
var insets = new UIEdgeInsets(SVConstants.Cell.PADDING.Top.ToNFloat(), SVConstants.Cell.PADDING.Left.ToNFloat(), SVConstants.Cell.PADDING.Bottom.ToNFloat(), SVConstants.Cell.PADDING.Right.ToNFloat());
_Button = new UIButton(UIButtonType.RoundedRect)
{
AutoresizingMask = UIViewAutoresizing.All,
HorizontalAlignment = UIControlContentHorizontalAlignment.Center,
VerticalAlignment = UIControlContentVerticalAlignment.Center,
ContentEdgeInsets = insets,
// TitleEdgeInsets = insets
};
DefaultFontSize = _Button.TitleLabel.ContentScaleFactor;
DefaultTextColor = _Button.TitleLabel.TextColor;
_Recognizer = new UILongPressGestureRecognizer(RunLong);
_Button.TouchUpInside += OnClick; // https://stackoverflow.com/a/51593238/9530917
_Button.AddGestureRecognizer(_Recognizer); // https://stackoverflow.com/a/6179591/9530917
ContentView.AddSubview(_Button);
_Button.CenterXAnchor.ConstraintEqualTo(ContentView.CenterXAnchor).Active = true;
_Button.CenterYAnchor.ConstraintEqualTo(ContentView.CenterYAnchor).Active = true;
_Button.WidthAnchor.ConstraintEqualTo(ContentView.WidthAnchor).Active = true;
_Button.HeightAnchor.ConstraintEqualTo(ContentView.HeightAnchor).Active = true;
UpdateConstraintsIfNeeded();
LayoutIfNeeded();
}
Found out that you can't subclass it. Any button added to the view must be native (UIButton) or custom rendered, such as Xamarin.Forms.Platform.iOS.ButtonRenderer; It doesn't show up otherwise.

C++ Builder TStringGrid row colors

The TStringGrid component I'm using in a C++ Builder project seems to alternate row colors by defaults. Even rows have a gray background while odd rows have a white background. How can I disable this alternate coloring so that all rows have the same background color?
Here's what it looks like:
And here is the corresponding entry in the DFM:
object StringGrid1: TStringGrid [0]
Left = 0
Top = 0
Width = 744
Height = 300
Align = alClient
FixedCols = 0
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
Options = [goFixedHorzLine, goVertLine, goHorzLine, goColSizing, goRowSelect]
ParentFont = False
TabOrder = 0
OnSelectCell = StringGrid1SelectCell
ColWidths = (
64
64
64
64
64)
end
I don't know which version of VCL you are using, but back in time, when I had been using great and very useful TStringGrid component, I did exactly the same thing, related to custom coloring cells, as you want to do. It is change related to behavior, not defined as property, I think.
You need to:
overload TStringGrid's virtual function for drawing cells in your particular derived class from TStringGrid
or,
on your TForm instance containing TStringGrid you want to modify, change behaviour of that particular TStringGrid.
So, basically you have choice, to make new class derived from TStringGrid and change behavior by overriding function for cell drawing, or to change existing, particular instance of TStringGrid by overriding just that grid's behavior with a help of great ObjectInspector, if it is still called like that.
It is better of course, to make new class and derive it from TStringGrid, and use it on Form(Parent container) directly which you can reuse later, whenever you want,
but it contains some additional work. Maybe, as start point, it is better to change behavior of particular TStringGrid instance you already dropped on ParetContainer(probably TForm) with using object inspector. If these directions are not enough for you, I can place here, some code samples that will be more helpful for you.
You should use event OnDrawCell. Here is working example:
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
if( ( ARow % 2 ) == 0 )
{
if( ARow == 0 )
StringGrid1->Canvas->Brush->Color = clBlue;
else
StringGrid1->Canvas->Brush->Color = clGray;
StringGrid1->Canvas->FillRect( Rect );
}
}

How do I add Components dynamically during runtime in Builder XE6?

I want to add new panels to my form during runtime, but I have the problem that, when aligning them to the top, they are not displayed in the order that I created them.
I followed the hints from this post with the DisableAlign() and EnableAlign()
How to dynamically create controls aligned to the top but after other aligned controls?
This works for the initial four panels I add.
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TPanel * test;
Panel1->DisableAlign();
for(int i = 0; i<4; i++){
test = new TPanel(Panel1);
test->Caption = i;
test->Parent = Panel1;
test->Align = alTop;
}
Panel1->EnableAlign();
}
But then I want to add another panel when clicking the button:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Panel1->DisableAlign();
TPanel * test;
test = new TPanel(Panel1);
test->Caption = 5;
test->Parent = Panel1;
test->Align = alTop;
Panel1->EnableAlign();
}
and this comes up:
Is there any way to get the alignment to do what I want without messing around with the Top Settings or without rebuilding the whole form?
It's quite simple. You must set Top to an appropriate value before setting Align. Set Top to be the coordinate of the bottom of the bottom panel.

Add a combobox in to listview control in c++ builder

I want to create a listview with 2 columns. in the first column it must be the row number and in the second number it should contains a combobox. I write the following code, but second column just show "combo" string. it does not show any combo box. what is the wrong?
for (int i = 0; i < 10; i++) {
TListItem *items;
items= this->ListView1->Items->Add();
items->Caption=IntToStr(i);
items->SubItems->AddObject("combo"+IntToStr(i),(TObject *)this->ComboBox1);
}
It does not show a TComboBox because you have not actually set the TComboBox to be a child control of the TListView. All you have done is store the TComboBox pointer as a user-defined value associated with the TListItem. That has no effect on the UI, so get rid of it:
for (int i = 0; i < 10; i++)
{
TListItem *items = ListView1->Items->Add();
items->Caption = IntToStr(i);
items->SubItems->Add("combo"+IntToStr(i));
}
To actually show a TComboBox inside of the TListView, you have to assign the TListView as the Parent of the TComboBox, and then use the SetBounds() method to position and size the TComboBox whenever you need to show it:
ComboBox1->Parent = ListView1;
...
RECT rect = {0};
ListView_GetSubItemRect(ListView1->Handle, SomeListItem->Index, 1, LVIR_BOUNDS, &rect);
ComboBox1->SetBounds(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
// update ComboBox1->Items as needed...
ComboBox1->Visible = true;
...
ComboBox1->Visible = false;
With that said, what you are attempting to do is better handled using the TValueListEditor component instead. Add items to it as needed, then use its ItemProps property to set each item's TItemProp.EditStyle property to esPickList, and then use the TValueListEditor.OnGetPickList event or the TItemProp.PickList property to manage the ComboBox strings as needed.

Resources