I'm using windows 10 theme in my project, and i've have noticed that: Panels that are positioned on edges of grids, they're shown under the grid scrollbar,
like this image:
I haven't changed any behavior of the VCL, or the grid or scroll behavior.
pas file:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Vcl.StdCtrls,
Datasnap.DBClient, Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
DbGrid: TDBGrid;
Panel2: TPanel;
ClientDataSet: TClientDataSet;
DataSource1: TDataSource;
ButtonAdd: TButton;
ShowPanel: TButton;
ClientDataSetname: TStringField;
ClientDataSetaddress: TStringField;
procedure ButtonAddClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ShowPanelClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ButtonAddClick(Sender: TObject);
begin
ClientDataset.Append;
ClientDataSetname.AsString := 'Test name';
ClientDataSetaddress.AsString := 'Test address';
ClientDataset.Insert;
end;
procedure TForm1.ShowPanelClick(Sender: TObject);
begin
if Panel2.Visible then
Panel2.Visible := False
else
Panel2.Visible := True;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ClientDataset.CreateDataSet;
end;
end.
dfm file:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 201
ClientWidth = 555
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Panel1: TPanel
Left = 460
Top = 0
Width = 95
Height = 201
Align = alRight
TabOrder = 0
object ButtonAdd: TButton
Left = 10
Top = 16
Width = 75
Height = 25
Caption = 'ButtonAdd'
TabOrder = 0
OnClick = ButtonAddClick
end
object ShowPanel: TButton
Left = 10
Top = 47
Width = 75
Height = 25
Caption = 'ShowPanel'
TabOrder = 1
OnClick = ShowPanelClick
end
end
object DbGrid: TDBGrid
Left = 0
Top = 0
Width = 460
Height = 201
Align = alClient
DataSource = DataSource1
TabOrder = 1
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'Tahoma'
TitleFont.Style = []
Columns = <
item
Expanded = False
FieldName = 'name'
Visible = True
end
item
Expanded = False
FieldName = 'address'
Visible = True
end>
end
object Panel2: TPanel
Left = 0
Top = 160
Width = 185
Height = 41
Caption = 'panel2'
TabOrder = 2
Visible = False
end
object ClientDataSet: TClientDataSet
Aggregates = <>
Params = <>
Left = 216
Top = 104
object ClientDataSetname: TStringField
FieldName = 'name'
Size = 50
end
object ClientDataSetaddress: TStringField
FieldName = 'address'
Size = 50
end
end
object DataSource1: TDataSource
DataSet = ClientDataSet
Left = 152
Top = 88
end
end
The bug happens after second click on ShowPanel.
You can Define DBgrid as parent of your panel.
procedure TForm1.FormShow(Sender: TObject);
begin
panel1.Parent := dbgrid1;
panel1.align := alBottom;
end;
I've found a way to "solve" this problem just changing grid seBorder to false, i used Notepad++ to locate in all project for ": TDBGrid"¹ and replace to ": TDBGrid StyleElements = [seFont, seClient]"¹. Isn't the better way to solve this problem i think, because changing seBorder to false will make the scrollbars style looks like the scrolls of your windows version.
¹ Ignore the quotes when you try to do it.
In fact, the only way is to work with the parent of the DBgrid.
This example work fine for me :
procedure TForm1.adjustPanelTo(const aPanel: TPanel; aWcontrol: TWinControl);
begin
if aPanel = nil then Exit;
if aWcontrol = nil then Exit;
if aWcontrol.Parent = nil then Exit;
aPanel.Parent := aWcontrol.Parent;
aPanel.Anchors := [akLeft, akTop];
aPanel.Left := aWcontrol.Left + 1;
aPanel.Top := aWcontrol.Top + aWcontrol.ClientHeight - aPanel.Height;
aPanel.BringToFront;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
adjustPanelTo(Panel1, DBGrid1);
end;
Related
I hate the title of this question. Anyway:
If you call TForm.Show with a custom theme (Windows10 Dark in this case), then close that form, then change the theme to the system Windows theme, then change back to the Windows10 Dark theme, and finally call TForm.Show on that form again, the border renders incorrectly and certain controls do not render properly, like a TComboBox.
I have a test project below, and a "fix" of sorts. But I do not like my fix and the reason for this question is that I do not really understand what is happening here that causes the form to render incorrectly only if it was hidden while the theme changed, and only if the theme is changed away from, and then back to, Windows10 Dark.
My fix is to track the theme change. If the condition I describe above occurs, I intercept the CM_SHOWINGCHANGED message, ignore it, then force the window to be recreated and then process the inherited CM_SHOWINGCHANGED the next time around. It is a very brittle fix and obviously not the way to go, so I am hoping someone can show me what is actually happening so I can fix it "for real."
Incidentally, I have submitted this as a bug to Embarcadero already. https://quality.embarcadero.com/browse/RSP-33977
Here is the test code. You'll need to add Windows10 Dark to the application's styles, obviously.
unit Unit22;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Unit23, Vcl.Themes;
type
TForm22 = class(TForm)
Panel1: TPanel;
ComboBox1: TComboBox;
RadioGroup1: TRadioGroup;
ButtonShow: TButton;
Memo1: TMemo;
procedure ButtonShowClick(Sender: TObject);
procedure RadioGroup1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
FAllowChange: Boolean;
public
{ Public declarations }
end;
var
Form22: TForm22;
implementation
{$R *.dfm}
procedure TForm22.ButtonShowClick(Sender: TObject);
begin
Form23.Show;
end;
procedure TForm22.FormShow(Sender: TObject);
begin
if StyleServices.Name = 'Windows10 Dark' then
RadioGroup1.ItemIndex := 1
else
RadioGroup1.ItemIndex := 0;
FAllowChange := True;
end;
procedure TForm22.RadioGroup1Click(Sender: TObject);
begin
if not FAllowChange then
exit;
if RadioGroup1.ItemIndex = 0 then
TStyleManager.SetStyle('Windows');
if RadioGroup1.ItemIndex = 1 then
TStyleManager.SetStyle('Windows10 Dark');
end;
end.
Unit 22 DPR:
object Form22: TForm22
Left = 0
Top = 0
ActiveControl = Memo1
Caption = 'Form22'
ClientHeight = 305
ClientWidth = 511
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object Panel1: TPanel
Left = 0
Top = 0
Width = 511
Height = 305
Align = alClient
BevelEdges = []
BevelOuter = bvNone
Caption = 'Panel1'
ShowCaption = False
TabOrder = 0
object ComboBox1: TComboBox
Left = 16
Top = 8
Width = 145
Height = 21
Style = csDropDownList
ItemIndex = 0
TabOrder = 0
Text = 'one'
Items.Strings = (
'one'
'two'
'three')
end
object RadioGroup1: TRadioGroup
Left = 16
Top = 48
Width = 185
Height = 105
Caption = 'RadioGroup1'
Items.Strings = (
'windows'
'dark')
TabOrder = 1
OnClick = RadioGroup1Click
end
object ButtonShow: TButton
Left = 16
Top = 159
Width = 75
Height = 25
Caption = 'ButtonShow'
TabOrder = 2
OnClick = ButtonShowClick
end
object Memo1: TMemo
Left = 207
Top = 8
Width = 274
Height = 281
Lines.Strings = (
'Always start in dark.'
''
'Steps to reproduce:'
'1.'#9'Click ButtonShow.'
'2.'#9'Close the window that opened.'
'3.'#9'Click Windows (change to system them).'
'4.'#9'Click Dark (change back to dark VCL style).'
'5.'#9'Click ButtonShow again. The controls are '
'not properly painted. Combobox text is black and form '
'is wrong until resize.'
''
'Hacky fix:'
'1.'#9'Click ButtonShow.'
'2.'#9'Check the '#8220'Fix'#8221' button in the window that '
'opened, then close it.'
'3.'#9'Click Windows (change to system)'
'4.'#9'Click Dark (change back to vcl dark)'
'5.'#9'Click ButtonShow. See comments in source.'
'')
ReadOnly = True
TabOrder = 3
end
end
end
Unit23:
unit Unit23;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Themes;
type
TForm23 = class(TForm)
Panel1: TPanel;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
ComboBox3: TComboBox;
Button1: TButton;
CheckBox1: TCheckBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
FFixing: Boolean;
FNeedFix: String;
FShowedStyle: String;
protected
procedure DoShow; override;
public
{ Public declarations }
procedure CMStyleChanged(var Message: TMessage); message CM_STYLECHANGED;
procedure CMShowingChanged(var Message: TMessage); message CM_SHOWINGCHANGED;
end;
var
Form23: TForm23;
implementation
{$R *.dfm}
procedure TForm23.Button1Click(Sender: TObject);
begin
PostMessage(Handle, CM_RECREATEWND, 0, 0);
end;
procedure TForm23.CMShowingChanged(var Message: TMessage);
var
DoFix: Boolean;
begin
if not Showing then
inherited
else
begin
// if the theme changed away from dark, then back to dark, while we were
// not visible, then we need to force the window to be recreated again
// before showing.
// This is a really bad hack but basically I am just preventing the
// normal response to CMShowingChanged and then setting up a message
// queue that will recreate the window and then process the CM_SHOWINGCHANGED
// message again. This will probably break the universe but it appears to work
// in this test.
FShowedStyle := StyleServices.Name;
Panel1.Caption := FShowedStyle;
DoFix := not FFixing and (FNeedFix <> '') and (FNeedFix = FShowedStyle);
FNeedFix := '';
if DoFix and CheckBox1.Checked then
begin
FFixing := True;
// SendMessage(Handle, WM_SETREDRAW, Winapi.Windows.WPARAM(LongBool(False)), 0);
PostMessage(Handle, CM_RECREATEWND, 0, 0);
// PostMessage(Handle, CM_SHOWINGCHANGED, Message.WParam, Message.LParam);
// do not allow inherited.
end else
begin
FFixing := False;
inherited;
end;
end;
end;
procedure TForm23.CMStyleChanged(var Message: TMessage);
begin
FNeedFix := FShowedStyle;
inherited;
end;
procedure TForm23.DoShow;
var
DoFix: Boolean;
begin
inherited;
exit;
end;
end.
Unit23 DPR:
object Form23: TForm23
Left = 0
Top = 0
Caption = 'Form23'
ClientHeight = 253
ClientWidth = 360
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Panel1: TPanel
Left = 0
Top = 0
Width = 360
Height = 253
Align = alClient
Alignment = taRightJustify
BevelEdges = []
BevelOuter = bvNone
Caption = 'Panel1'
TabOrder = 0
object ComboBox1: TComboBox
Left = 16
Top = 32
Width = 145
Height = 21
Style = csDropDownList
ItemIndex = 0
TabOrder = 0
Text = 'one'
Items.Strings = (
'one'
'two'
'three')
end
object ComboBox2: TComboBox
Left = 16
Top = 59
Width = 145
Height = 21
Style = csDropDownList
ItemIndex = 1
TabOrder = 1
Text = 'two'
Items.Strings = (
'one'
'two'
'three')
end
object ComboBox3: TComboBox
Left = 16
Top = 86
Width = 145
Height = 21
Style = csDropDownList
ItemIndex = 2
TabOrder = 2
Text = 'three'
Items.Strings = (
'one'
'two'
'three')
end
object Button1: TButton
Left = 16
Top = 136
Width = 75
Height = 25
Caption = 'RecreateWnd'
TabOrder = 3
OnClick = Button1Click
end
object CheckBox1: TCheckBox
Left = 16
Top = 167
Width = 273
Height = 17
Caption = 'Fix with CM_SHOWINGCHANGED hack'
TabOrder = 4
end
end
end
I have written a small test VCL application with a monitor PPI of 96.
The application has a TGridPanel on it with a absolute pixel sized column.
On that column I placed a TComboBox and aligned it alClient.
Here is the DFM code:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 182
ClientWidth = 514
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object GridPanel1: TGridPanel
Left = 0
Top = 0
Width = 514
Height = 182
Align = alClient
Caption = 'GridPanel1'
ColumnCollection = <
item
Value = 100.000000000000000000
end
item
SizeStyle = ssAbsolute
Value = 150.000000000000000000
end>
ControlCollection = <
item
Column = 0
Control = Button1
Row = 0
end
item
Column = 1
Control = ComboBox1
Row = 0
end
item
Column = 0
Control = Edit1
Row = 1
end>
RowCollection = <
item
Value = 50.000000000000000000
end
item
Value = 50.000000000000000000
end>
TabOrder = 0
object Button1: TButton
Left = 1
Top = 1
Width = 362
Height = 21
Align = alTop
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object ComboBox1: TComboBox
Left = 363
Top = 1
Width = 150
Height = 21
Align = alClient
TabOrder = 1
Text = 'ComboBox1'
end
object Edit1: TEdit
Left = 1
Top = 91
Width = 362
Height = 21
Align = alTop
TabOrder = 2
Text = 'Edit1'
end
end
end
and the PAS code:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;
type
TForm1 = class(TForm)
GridPanel1: TGridPanel;
Button1: TButton;
ComboBox1: TComboBox;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
PPI: Integer;
begin
PPI := Integer.Parse(Edit1.Text);
GridPanel1.ScaleForPPI(PPI);
end;
procedure TForm1.FormShow(Sender: TObject);
begin
Edit1.Text := Screen.PixelsPerInch.ToString;
end;
end.
I then changed the custom scaling factor to 125 in Windows 10 in the advanced scaling settings.
After signing off and signing on again when I run the application again the drop down button of the combo box is not visible any more.
How do you deal with this problem?
I tried to call GridPanel1.ScaleForPPI(96) which restores the drop down button on the combo box. This kind of defeats the purpose of PPI scaling though, doesn't it?
The problem is gone in Delphi 10.3.1.
So this is a bug in at least Delphi 10.1 (and possible other older versions).
i want to find the height that needs the TRichEdit control to render itself without showing the vertical scrollbar.
I use this code
function RichEditHeight(var RE : TRichEdit; aForm : TForm) : integer;
var fmtRange: TFormatRange;
begin
FillChar(fmtRange, SizeOf(fmtRange), 0);
with fmtRange do begin
hDC := aForm.canvas.Handle;
hdcTarget := hDC;
rc.left := 0;
rc.right := (RE.ClientWidth * 1440) div screen.pixelsPerInch;
rc.top := 0;
rc.Bottom := MaxInt;
rcPage := rc;
chrg.cpMin := 0;
chrg.cpMax := -1;
end;
RE.Perform(EM_FORMATRANGE, 0, Longint(#fmtRange));
result := round(fmtRange.rc.Bottom*screen.pixelsPerInch/1440);
RE.Perform(EM_FORMATRANGE, 0, 0);
end;
When the document has a few pages (<15 A4 portrait) the result is sufficient.
But with more pages, the rc.bottom seems to be trancated and the control needs the vertical scrollbar.
The question is : there are some limitations inside perform's code ?
If i increase the rc.bottom manually (approximately) then the control renders itself ok but this isn't the case i want.
PS. if matters, the richedit component, actually is a TjvRichEdit one.
Here is a minimal program that can reproduce the problem (for even 2 pages)
unit Unit32;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, winAPI.richedit, JvExStdCtrls, JvRichEdit, Vcl.ExtCtrls;
type
TForm32 = class(TForm)
ScrollBox1: TScrollBox;
Button1: TButton;
RichEdit1: TJvRichEdit;
panel1: TPanel;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var Form32: TForm32;
implementation
{$R *.dfm}
function RichEditHeight(var RE : TjvRichEdit; aForm : TForm) : integer;
var fmtRange: TFormatRange;
begin
FillChar(fmtRange, SizeOf(fmtRange), 0);
with fmtRange do begin
hDC := aForm.canvas.Handle;
hdcTarget := hDC;
rc.left := 0;
rc.right := (RE.ClientWidth * 1440) div screen.pixelsPerInch;
rc.top := 0;
rc.Bottom := 500000;
rcPage := rc;
chrg.cpMin := 0;
chrg.cpMax := -1;
end;
RE.Perform(EM_FORMATRANGE, 0, Longint(#fmtRange));
result := round(fmtRange.rc.Bottom*screen.pixelsPerInch/1440);
RE.Perform(EM_FORMATRANGE, 0, 0);
end;
procedure TForm32.Button1Click(Sender: TObject);
begin
with OpenDialog1 do
if execute then begin
RichEdit1.lines.LoadFromFile(filename);
panel1.height := RichEditHeight(RichEdit1,Form32);
end;
end;
end.
The form
object Form32: TForm32
Left = 0
Top = 0
Caption = 'Form32'
ClientHeight = 615
ClientWidth = 874
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object ScrollBox1: TScrollBox
Left = 0
Top = 0
Width = 874
Height = 615
Align = alClient
TabOrder = 0
ExplicitWidth = 885
ExplicitHeight = 583
object Button1: TButton
Left = 632
Top = 24
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object panel1: TPanel
Left = 0
Top = 0
Width = 457
Height = 561
Caption = 'panel1'
TabOrder = 1
object RichEdit1: TJvRichEdit
Left = 1
Top = 1
Width = 455
Height = 559
Align = alClient
Font.Charset = GREEK_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
ParentFont = False
ScrollBars = ssVertical
SelText = ''
TabOrder = 0
ExplicitHeight = 279
end
end
end
object OpenDialog1: TOpenDialog
Left = 656
Top = 88
end
end
and the result with a RTF document (just 2 pages)
Finally, the solution was very simple!
TRichedit has the event onResizeRequest where the actual height of the control is given by the rect parameter
Create a form and place a TValueListEditor. Set the doColumnTitles display option to False. Try and delete the first row. You will get a 'Grid Index out of Bounds' error. It is an ugly pig and so I am here to ask if using a try except block is the only way to deal with it?
Background:
Row is inherited from TCustomGrid via TCustomDrawGrid (Line 493 VCL.grids.pas) The property getter is private variable FCurrent.Y, the type of which is a value/field from a two value record. I cannot find where the FCurrent value is set by default (so it integer defaults to 0). It is not clear what values exist if the TValueListEditor has only one row and/or if there are no rows selected.
In any case, I get a Grid Index Out of Range Error - presumably raised from the TCustomGrid - but only if there is only one row and/or I attempt a delete on the first row in the TValueListEditor with doColumnTitles := false. Delete on other rows is fine. Regardless of whether this is a bug or not it seems somewhat stupid and inconsistent.
Is there a reason why the TValueList Editor constructor has inherited RowCount := 2;?
The error is thrown by TValueListEditor.DeleteRow according to:
try
if (Strings.Count = 0) or
(ARow < 1) or (ARow > RowCount - FixedRows) then
{$IF DEFINED(CLR)}
raise EInvalidGridOperation.CreateRes(SIndexOutOfRange);
{$ELSE}
raise EInvalidGridOperation.CreateRes(#SIndexOutOfRange);
{$ENDIF}
Strings.Delete(ARow - FixedRows);
finally
FDeleting := False;
end;
(See VCL.Valedit.pas at 804)
Is it a problem that there is an exception if (ARow < 1)?
Otherwise, perhaps it is that FixedRows is set in the constructor of the TCustomGrid:
constructor TCustomGrid.Create(AOwner: TComponent);
const
GridStyle = [csCaptureMouse, csOpaque, csDoubleClicks,
csNeedsBorderPaint, csPannable, csGestures];
begin
inherited Create(AOwner);
if NewStyleControls then
ControlStyle := GridStyle
else
ControlStyle := GridStyle + [csFramed];
FCanEditModify := True;
FColCount := 5;
FRowCount := 5;
FFixedCols := 1;
FFixedRows := 1;
...
and the comments in the header of VCL.grids.pas say "FixedRows The number of non-scrolling rows. This value must be at least one below RowCount." (Line 138).
Methinks that there ends up being a problem with these values when the titles row is removed from the TValueListEditor
Anyway. My question is just - Does this require the use of an exception handler - or is there a more elegant way of un-kludging it?
Here is some basic no seatbelts code:
unit vleDebug;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Buttons, Vcl.StdCtrls, Vcl.Grids, Vcl.ValEdit;
type
TForm1 = class(TForm)
vleWhoCaresNerd: TValueListEditor;
Button1: TButton;
BitBtn1: TBitBtn;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
vleWhoCaresNerd.DeleteRow(vleWhoCaresNerd.Row);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
vGUID: TGUID;
begin
vleWhoCaresNerd.InsertRow('id', GUIDToString(vGUID), True);
end;
end.
Here's the .dfm:
object Form1: TForm1
Left = 0
Top = 0
Caption = ':P'
ClientHeight = 568
ClientWidth = 633
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 120
TextHeight = 16
object Label1: TLabel
Left = 295
Top = 162
Width = 242
Height = 16
Caption = 'Select and delete the first row if you dare.'
end
object Label2: TLabel
Left = 63
Top = 190
Width = 393
Height = 16
Caption =
'Delete other rows if you like. Make some with the Add GUID But' +
'ton.'
end
object vleWhoCaresNerd: TValueListEditor
Left = 8
Top = 8
Width = 617
Height = 145
DisplayOptions = [doAutoColResize, doKeyColFixed]
Strings.Strings = (
'jsonrpc=2.0')
TabOrder = 0
ColWidths = (
150
461)
end
object Button1: TButton
Left = 8
Top = 159
Width = 75
Height = 25
Caption = 'Add GUID'
TabOrder = 1
OnClick = Button1Click
end
object BitBtn1: TBitBtn
Left = 550
Top = 159
Width = 75
Height = 25
Caption = 'bbDelete'
TabOrder = 2
OnClick = BitBtn1Click
end
end
I have 2 controls on a form, TCheckBox and TEdit.
I want to use Live Binding to perform this:
When TCheckBox.Checked = True, set TEdit.PasswordChar = *
When TCheckBox.Checked = False, set TEdit.PasswordChar = #0
How may I write ControlExpression to achieve this? It would be great if I can avoid register custom method.
Here's a simple example. I couldn't find a boolean expression evaluator so I registered a new one, and also a string-to-char converter (seems to be missing, too).
The form:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 282
ClientWidth = 418
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object CheckBox1: TCheckBox
Left = 24
Top = 24
Width = 97
Height = 17
Caption = 'CheckBox1'
TabOrder = 0
OnClick = CheckBox1Click
end
object Edit1: TEdit
Left = 24
Top = 56
Width = 121
Height = 21
TabOrder = 1
Text = 'Edit1'
end
object BindingsList1: TBindingsList
Methods = <>
OutputConverters = <>
UseAppManager = True
Left = 212
Top = 13
object BindExpression1: TBindExpression
Category = 'Binding Expressions'
ControlComponent = Edit1
SourceComponent = CheckBox1
SourceExpression = 'iif(Checked, '#39'*'#39', '#39#39')'
ControlExpression = 'PasswordChar'
NotifyOutputs = True
Direction = dirSourceToControl
end
end
end
and the code:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Data.Bind.EngExt, Vcl.Bind.DBEngExt, System.Rtti,
Vcl.Bind.Editors, Data.Bind.Components, System.Bindings.Outputs;
type
TForm1 = class(TForm)
CheckBox1: TCheckBox;
Edit1: TEdit;
BindingsList1: TBindingsList;
BindExpression1: TBindExpression;
procedure CheckBox1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
System.TypInfo,
System.Bindings.EvalProtocol,
System.Bindings.Methods;
resourcestring
sIifArgError = 'Expected three variables for Iif() call';
sIifExpectedBoolean = 'First argument to Iif() must be a boolean';
function MakeIif: IInvokable;
begin
Result := MakeInvokable(
function(Args: TArray<IValue>): IValue
var
V: IValue;
B: Boolean;
begin
if Length(Args) <> 3 then
raise EEvaluatorError.Create(sIifArgError);
V := Args[0];
if (V.GetType^.Kind <> tkEnumeration) or (V.GetType^.Name <> 'Boolean') then
raise EEvaluatorError.Create(sIifExpectedBoolean);
B := V.GetValue.AsBoolean;
if B then
Result := TValueWrapper.Create(Args[1].GetValue)
else
Result := TValueWrapper.Create(Args[2].Getvalue);
end
);
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
BindingsList1.Notify(CheckBox1, 'Checked');
end;
initialization
TBindingMethodsFactory.RegisterMethod(TMethodDescription.Create(MakeIif, 'iif', 'iif', '', True, '', nil));
TValueRefConverterFactory.RegisterConversion(TypeInfo(string), TypeInfo(Char),
TConverterDescription.Create(
procedure(const I: TValue; var O: TValue)
var
S: string;
begin
S := I.AsString;
if Length(S) = 1 then
O := S[1]
else
O := #0;
end,
'StringToChar', 'StringToChar', '', True, '', nil));
finalization
TValueRefConverterFactory.UnRegisterConversion('StringToChar');
TBindingMethodsFactory.UnRegisterMethod('iif');
end.