Question

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

Was it helpful?

Solution

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;

OTHER TIPS

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;

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 -

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top