How can I use TEdits selStart and SelLength to select Text? - delphi

I try to make a new project add a TEdit and a TButton.
Set Edit1.Text to 'This is a test message'.
And add an event to the button:
procedure TForm7.Button1Click(Sender: TObject);
begin
Edit1.SelStart := 5;
Edit1.SelLength := 5;
end;
Nothing is selected when I click the button. Can someone explain why and how it should be done to select some part of the text ?
Regards
Roland

It works as expected, but since your button stealed the focus by clicking on it, you're then trying to focus that edit box back again. And by focusing an edit box, all of its text is selected by default. Here's a simple proof, that the text is selected if the edit box has focus while selecting:
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.SetFocus;
Edit1.SelStart := 5;
Edit1.SelLength := 5;
end;

Related

How to stop Screen.Cursor affects all controls on the form?

I will try to simplify my problem. If for example you drop 2 TSpeedButton and do:
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
Screen.Cursor := crHourGlass;
SpeedButton2.Cursor := crHandPoint; // note I'm setting other cursor than crDefault
end;
The SpeedButton2.Cursor remains showing Screen.Cursor which was set to crHourGlass.
I have looked into the TScreen.SetCursor code, and realize it sets the cursor for the entire form.
My question: is it somehow possible to use the Screen.Cursor for the entire form, BUT without impacting some control(s) which I want to set other cursor.
The same happens with a TButton. I don't mind placing the SpeedButton on a windowed control if I can somehow control it's cursor while Screen.Cursor is set to crHourGlass.
Thanks.
This is intentional behavior as explained in the documentation for TScreen.Cursor:
... When Cursor is crDefault, the individual objects determine the
cursor image. Assigning any other value sets the mouse cursor image
for all windows belonging to the application. The global mouse cursor
image remains in effect until the screen's Cursor property is changed
back to crDefault. ..
Windowed controls handle their cursors in TWinControl.WMSetCursor procedure, handler of WM_SETCURSOR message, where they explicitly set the screen cursor if it is anything other than crDefault and disregard their own cursor.
So to change the behavior you can handle the mentioned message. For a TButton interposer, an example could be:
procedure TButton.WMSetCursor(var Message: TWMSetCursor);
begin
if (Cursor <> crDefault) and (Message.HitTest = HTCLIENT) then begin
Message.Result := 1;
Windows.SetCursor(Screen.Cursors[Cursor]);
end else
inherited;
end;
Graphic controls' cursors are handled by their parent TWinControl. So to change the behavior of a speed button, you would still need to handle the same message on its parent. This would likely be impractical since the parent class might not be known beforehand.
Still, a very non-generalized implementation, for example for a graphic control placed directly on the form, might look like the below:
procedure TForm1.WMSetCursor(var Message: TWMSetCursor);
var
SmPt: TSmallPoint;
Control: TControl;
begin
DWORD(SmPt) := GetMessagePos;
Control := ControlAtPos(ScreenToClient(SmallPointToPoint(SmPt)), True);
if Assigned(Control) and Boolean(Control.Tag) then begin
Message.Result := 1;
Windows.SetCursor(Screen.Cursors[Control.Cursor])
end else
inherited;
end;
Above example would require the graphic control to have a non zero tag value. E.g.:
procedure TForm1.Button1Click(Sender: TObject);
begin
Screen.Cursor := crHourGlass;
SpeedButton1.Cursor := crHandPoint;
SpeedButton1.Tag := 1;
end;

TEdit with clear button [duplicate]

When use TEdit control on the right side stay small icon 'x'. How after click on icon clear TEdit box.
Tnx all!
Delphi provide TClearEditButton to clear the TEdit content. It can be added by right clicking and selecting AddItem - TClearEditButton from the popup menu. It also has a Click procedure overriden in FMX.Edit unit like:
procedure TClearEditButton.Click;
var
EditTmp: TCustomEdit;
begin
inherited Click;
EditTmp := GetEdit;
if EditTmp <> nil then
begin
if EditTmp.Observers.IsObserving(TObserverMapping.EditLinkID) then
if not TLinkObservers.EditLinkEdit(EditTmp.Observers) then
Exit; // Can't change
EditTmp.Text := string.Empty;
if EditTmp.Observers.IsObserving(TObserverMapping.EditLinkID) then
TLinkObservers.EditLinkModified(EditTmp.Observers);
if EditTmp.Observers.IsObserving(TObserverMapping.ControlValueID) then
TLinkObservers.ControlValueModified(EditTmp.Observers);
end;
end;
Which make you don't need to write OnClick event handler for the TClearEditButton unless you want to do some other job along side with clearing the edit.
If you are using a TEditButton then you should write the OnClick event handler like:
procedure TForm1.EditButton1Click(Sender: TObject);
begin
Edit1.Text:= EmptyStr;
end;

How to disable CheckListBox scroll when the last item is clicked?

If there are enough items in the CheckListBox, so it has a scrollbar, whenever the last item is clicked it scrolls to the next one, which results in undesired behaviour:
checking/unchecking the next item instead of the one I clicked
Tested it in both Delphi XE7 and a newer version, the 10.2 Tokyo and it does the same thing
Tried capturing the WM_LBUTTONDOWN message and track it, which resulted in finding the exact place things happen.
Vcl.Controls.pas
...
procedure TControl.WMLButtonDown(var Message: TWMLButtonDown);
begin
SendCancelMode(Self);
inherited; //there it scrolls by 1 line
if csCaptureMouse in ControlStyle then
MouseCapture := True;
if csClickEvents in ControlStyle then
Include(FControlState, csClicked);
DoMouseDown(Message, mbLeft, []); // it does the check/uncheck
end;
...
Code for reproduction: (and drop a TCheckListBox on the form)
...
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
CheckListBox1.Height := 145; //just so it's short enough
for i := 0 to 30 do
begin
CheckListBox1.Items.Add('Item'+IntToStr(i));
end;
end;
...
Then click the chechbox of item that shows on the bottom
Also tried to go trough the unit, which contains evertything about the CheckListBox (Vcl.CheckLst, but haven't found anything usable
EDIT
BrakNicku discovered he could only reproduce it with partial items, which seems to be the case for me as well
His solution: Set the IntegralHeight property to True, so it prevents this problem.

Mouseover Image On Buttons In FMX XE2

How to make a mouseover image for button ?
I used to make in FMX 2 buttons, and fill it with bitmap. But its owful .
I found property IsMouseOver
procedure TForm1.Button1Paint(Sender: TObject; Canvas: TCanvas;
const ARect: TRectF);
begin
if Button1.IsMouseOver then
begin
Button1.Text:='yes';
end
else
begin
Button1.Text:='nono';
end;
end;
But , i realy dont understand how to use containers, i only want to change fill ( my bitmap) by the method written before. Can someone give a simple code?
Or maybe its easier to make in VCL ?
Put two separate TImage controls on the button (drag them onto the button in the Structure View):
Size them to fit the button, and give each a separate image using the MultiResBitmap property editor.
Create an event handler for one of the TImage components for the OnMouseEnter and OnMouseLeave events, and then assign those handlers to both of the TImage components:
procedure TForm1.Image1MouseEnter(Sender: TObject);
begin
Image1.Visible := False;
Image2.Visible := True;
end;
procedure TForm1.Image1MouseLeave(Sender: TObject);
begin
Image1.Visible := True;
Image2.Visible := False;
end;

Disabled TEdit Font Colour

I have an application having one TEdit which is disabled when the application runs. After some calculations it will be enabled. My requirement is to set the Font.Color of this disabled TEdit as Blue instead of Grey (Disabled Font Color).
This is not supported by the standard TEdit. You could set the edit to ReadOnly instead of Disabled - this way the font color is preserved but user can't change the value of the edit. Ie to "disable" the edit
Edit1.ReadOnly := True;
Edit1.Font.Color := clBlue;
and to enable it again
Edit1.ReadOnly := False;
Edit1.Font.Color := clWindowText;
See Peter Below's two suggestions for accomplishing your objective on Torry's Delphi Pages at this link. Judging from your comment about what you Googled, his first suggestion will be simpler for you to implement. Drop a TPanel on a form and drag a TEdit onto the TPanel (i.e., TPanel is TEdit's parent. Then drop a Button on the form to simulate when your calculations are done.
procedure TForm1.btnToggleEnabledClick(Sender: TObject);
begin
if Panel1.Enabled then
begin
{Calcs are not done, so disable the TEdit}
Panel1.Enabled := false;
Edit1.Font.Color := clBlue;
Edit1.Text := 'Calcs not done';
end
else
begin
{Calcs are done, so enable the TEdit}
Panel1.Enabled := true;
Edit1.Font.Color := clWindowText;
Edit1.Text := 'Calcs all done';
end;
end;

Resources