Is there any way to disable selecting of text in a memo control because it's very anoying.
The memo is Read Only.

- 2,419
- 9
- 37
- 67
-
2Are you sure that your users will never need to copy *a part of* the text in the memo? – Andreas Rejbrand Oct 26 '11 at 18:43
-
@Andreas Rejbrand No because it is ReadOnly – Little Helper Oct 26 '11 at 18:43
-
Yes, I understand that it is read only, but I do not understand what that has to do with *reading* the contents of the memo. Anyhow, if the answer is actually *no*, as you say, then you have just acknowledged yourself that it is bad to do what you suggest. – Andreas Rejbrand Oct 26 '11 at 18:45
-
1Setting enable to false will prevent selection, but also stop scrollbars from working. – LU RD Oct 26 '11 at 18:52
-
memo.Enabled is the setting I meant. – LU RD Oct 26 '11 at 18:55
-
`Memo1.Enabled := false` – Andreas Rejbrand Oct 26 '11 at 18:55
-
1But if I disable the memo, font color changes. – Little Helper Oct 26 '11 at 18:57
-
Yes, that's the downside. I usually set the background to clInfoBk to make it clear that the text is for information only. – LU RD Oct 26 '11 at 19:00
-
@LU RD: That is so Windows 3.1 if you ask me! :) – Andreas Rejbrand Oct 26 '11 at 19:01
-
2TLabel may be suitable if you change its font and background color. – Roman Yankovsky Oct 26 '11 at 19:15
4 Answers
I think you should rethink. I realise that your control is used in read-only mode, but still, what if the end user wishes to copy a part of the text? Then he needs to be able to select the part in question.
Still, if you are certain that you need to disable every kind of selection, the easiest approach is to use a TRichEdit
instead of the TMemo
, and do simply
procedure TForm1.RichEdit1SelectionChange(Sender: TObject);
begin
RichEdit1.SelLength := 0;
end;

- 105,602
- 8
- 282
- 384
-
I can see the selected text while the mouse isn't released, but still thanks. – Little Helper Oct 26 '11 at 19:02
-
@Robrok: I can't. (Don't know what could cause this difference. OS version, perhaps.) – Andreas Rejbrand Oct 26 '11 at 19:09
You could also use the onMouseUp event
procedure TForm1.Memo1MouseUp(Sender: TObject: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Memo1.SelLength > 0 then
Memo1.SelLength := 0;
end;
But, that doesn't stop selecting with the keyboard..
or you could also use the onEnter, and just change the focus to another control on your form.
procedure TForm1.Memo1Enter(Sender: TObject);
begin
Edit1.SetFocus;
end;

- 7,181
- 7
- 63
- 111

- 774
- 5
- 25
-
1Never change the focus during a focus-changing event (such as `OnEnter`). – Rob Kennedy Nov 02 '11 at 22:45
I played around with TRichEdit and TMemo until I was bored to tears. Yes, you can do a few tricks with event handling on the object, but it still is not the desired effect - and the cursor winds up blinking somewhere. So the best thing I could find was to use TLabel. I'm using Borland C++ Builder 6, and the \n is translated correctly with inline text strings for TLabel. So,
Label1->Caption = "this is a test of the emergency\n"
"broadcast station, this is only\n"
"a test. If this had been an\n"
"actual emergency, blah blah blah...\n";
Works just fine. I haven't tried to read in from a file, but I'm certain that if the stream were exactly as seen it would also work. Since you are going to have to enter or read in the text you want displayed anyway - this should work well instead of using a bunch of TLabels for each line. If you have a concern for word wrapping, you will have to process that portion separately. If it static, then just do it by hand like I did in the example. I sure hope this helps or at least gives an idea...
- atomkey -

- 55
- 5
As i understand you would like to use memo as label actually (and sometimes it really have sense). When i need to use TcxMemo (memo component from DeveloperExpress) as label i use such simple procedure:
procedure ShowMemoAsLabel(m: TcxMemo);
begin
m.Enabled := False;
m.Properties.ReadOnly := True;
// AH: Unfortunately it doesn't copy some important properties, maybe it will
// be fixed in future versions of DEX, but at moment we do some job ourselves.
m.StyleDisabled := m.Style;
m.StyleDisabled.BorderColor := m.Style.BorderColor;
m.StyleDisabled.BorderStyle := m.Style.BorderStyle;
m.StyleDisabled.Color := m.Style.Color;
m.StyleDisabled.Edges := m.Style.Edges;
m.StyleDisabled.Shadow := m.Style.Shadow;
m.StyleDisabled.TextColor := m.Style.TextColor;
m.StyleDisabled.TextStyle := m.Style.TextStyle;
m.StyleDisabled.TransparentBorder := m.Style.TransparentBorder;
end;

- 3,322
- 2
- 24
- 38