Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to know when a TMemo's control has had text selected?

Posted on 2010-08-25
11
391 Views
Last Modified: 2012-05-10

I am trying to enable/disable a button & menu item based on whether or not a TMemo's text has been selected

I dont see an event that triggers when text is selected...so how can i trap for it?
0
Comment
Question by:Looking_4_Answers
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 33526647
To know, you just need to check the SelLength property, but I don't know of an event/message off hand
0
 

Author Comment

by:Looking_4_Answers
ID: 33526696
ya, i know that part, but i need something that kicks it off
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 33526903
Try the  mouse up event

procedure TForm1.Memo1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  button1.Enabled := (Sender as TMemo).SelLength;
end;
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 32

Accepted Solution

by:
ewangoya earned 250 total points
ID: 33526946
procedure TForm1.Memo1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  button1.Enabled := (Sender as TMemo).SelLength > 0;
end;
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 33526972
whenever something happens in a delphi form a procedure UpdateActions gets called
you can make your own event with that:
type
  TForm1 = class(TForm)
  private
    fSelLength: integer;
    procedure MemoSelLengthChanged(aMemo: TMemo; OldSelLength, NewSelLength: integer);
  protected
    procedure UpdateActions; override;
  public
    constructor Create(AOwner: TComponent); override;  
  end;

constructor TForm1.Create(AOwner: TComponent); 
begin
  inherited Create(AOwner);
  fSelLength := 0;
end;

procedure TForm1.UpdateActions; 
var CurSelLength: Integer;
begin
  inherited UpdateActions;
  CurSelLength := Memo1.SelLength;
  if fSelLength <> CurSelLength then 
  begin
    MemoSelLengthChanged(Memo1, fSelLength, CurSelLength);
    fSelLength := CurSelLength;
  end;
end;

procedure TForm1.MemoSelLengthChanged(aMemo: TMemo; OldSelLength, NewSelLength: integer);
begin
  // Do Your code here  
end;

Open in new window

0
 
LVL 25

Expert Comment

by:epasquier
ID: 33527581
Geert : That is excellent. I never thought of the use of UpdateActions in this way.
for information, that is a method introduced in TComponent

I would have used a procedure that is called by keyPressed, Key Up & mouse Up events, so like ewangoya but including all events that can change the selection.

I would need to test how often UpdateActions is called, and if there are cases where the 'mouse&key events' method fails (like if the selection is changed programmatically) before choosing which method I'll keep, but considering that there is not much load in a simple test and a button state change, why not being extensive ?

0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 33529133
>>epasquier ugh ?
you'r last sentence is not very clear (at least to me, and i haven't had a beer yet ...)

The simplicity of using UpdateActions is somewhat frightening.
It doesn't go very well for debugging, because it get's called very often.
If anybody intends to use this procedure then anything in it must be blazing fast
Don't use any queries in this procedure directly ... it will slow down your delphi app
Maybe i should write a article about UpdateActions ... the ins and outs

I have found that setting enable/visible in this procedure is the easiest to do.
You don't have to provide anything to call this procedure, Delphi does it for you

However if you need to check things like enable/visible which are set in UpdateActions
In the same procedure/function in 2 different places then you may have to call it explicitely.
0
 
LVL 14

Expert Comment

by:systan
ID: 33532843
The simpliest;
procedure TForm1.Memo1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
if memo1.SelText <> '' then showmessage(memo1.SelText + '  was selected');
end;

Open in new window

0
 
LVL 25

Expert Comment

by:epasquier
ID: 33539711
@Geert : Well my last sentence was just expressing concerns about precisely what you said after : it is called too often for big code, but in this case that looks one helluva easy way
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 33540072
there goes the effort again ... way too much
0
 
LVL 25

Expert Comment

by:epasquier
ID: 33543298
> there goes the effort again ... way too much
???
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 221
LAN or WAN ? 11 95
DBGrid or StringGrid ? 6 104
How to remove Recent Projects from Embarcadero C++ builder XE10. Berlin 2 66
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question