2

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

10
  • 2
    Are you sure that your users will never need to copy a part of the text in the memo? Commented Oct 26, 2011 at 18:43
  • @Andreas Rejbrand No because it is ReadOnly Commented Oct 26, 2011 at 18:43
  • 1
    Setting enable to false will prevent selection, but also stop scrollbars from working. Commented Oct 26, 2011 at 18:52
  • 1
    But if I disable the memo, font color changes. Commented Oct 26, 2011 at 18:57
  • 2
    TLabel may be suitable if you change its font and background color. Commented Oct 26, 2011 at 19:15

4 Answers 4

4

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; 
Sign up to request clarification or add additional context in comments.

2 Comments

I can see the selected text while the mouse isn't released, but still thanks.
@Robrok: I can't. (Don't know what could cause this difference. OS version, perhaps.)
2

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; 

1 Comment

Never change the focus during a focus-changing event (such as OnEnter).
1

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 -

Comments

0

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; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.