Jump to content
Ian Branch

Prevent editing in a TComboBoxEx..

Recommended Posts

Hi Team,

I need to prevent Users from actually editing, including deleting, a TComboBoxEx whilst still allowing the contents to be changed via the drop-down.

The component's OnChange event fires whenever they do something in the field.

The component doesn't have a Read-Only property.  This might get in the road of the drop-down working, so what do I do?

 

Regards & TIA,

Ian

 

Share this post


Link to post
Guest

try this my sample: THIS WORKS BETTER!

procedure TForm1.ComboBoxEx1KeyPress(Sender: TObject; var Key: Char);
begin
  if not(Key = #0) then // if any char was typed by user!
    Key := #0; // imiting "Read-Only" effect!
end;

procedure TForm1.ComboBoxEx1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if not(Key = 0) then // to keys like DEL...
    Key := 0;
end;

Another Observations:

  • when user use "CLICK Mouse", then, "OnClick" is raised
  • for all changes, "OnBeginEdit" and "OnEndEdit" is raised any way
  • then, you can use a "var" for to knows raise your changes, for example!
  • another keys, like "DEL", "BackSpace" can fail  in test (if not lWhoStartEditWasClick then  or If Key='') - it's necessary more verification!

 

some like this:  (BUT IT FAILS IF THE VAR GOES TO = TRUE ALWAYS)  - RE-EDITED

image.thumb.png.30db45d9c7f2a8327dfe9bee15d722be.png

 

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  lWhoStartEditWasClick: boolean; // false

procedure TForm1.ComboBoxEx1BeginEdit(Sender: TObject);
begin
  Memo1.Lines.Add('beginedit ' + datetimetostr(now));
  lWhoStartEditWasClick := false;
end;

procedure TForm1.ComboBoxEx1Change(Sender: TObject);
begin
  Memo1.Lines.Add('Change ' + datetimetostr(now));
  lWhoStartEditWasClick := false; // forcing new avaliation!
end;

procedure TForm1.ComboBoxEx1Click(Sender: TObject);
begin
  Memo1.Lines.Add('Click ' + datetimetostr(now));
  lWhoStartEditWasClick := true;
end;

procedure TForm1.ComboBoxEx1EndEdit(Sender: TObject);
begin
  Memo1.Lines.Add('endedit ' + datetimetostr(now));
end;

procedure TForm1.ComboBoxEx1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if not(Key = 0) then  // keys as DEL
    Key := 0;
end;

procedure TForm1.ComboBoxEx1KeyPress(Sender: TObject; var Key: Char);
begin
  //if not(Key = #0) then // chars
  if not lWhoStartEditWasClick then
    Key := #0;
  //

end;

end.

 

hut

Edited by Guest

Share this post


Link to post

Hi Emailx45,

Oh, that is excellent!  Thank you.

I do like the added logging functionality.

I am going to save that as a code snippet for the future.

 

Regards,

Ian

Share this post


Link to post

I did read them and they were noted.

FYI - This is what I now have in place..

procedure TCustomersForm.IndexOrdKeyPress(Sender: TObject; var Key: Char);
begin
  if not(Key = '') then // if any char was typed by user!
  begin
    MessageBeep(MB_ICONERROR);
    TaskMessageDlg('Editing not permitted!', 'You are not permitted to edit this entry.  Please select from the drop-down options!', mtError, [mbOK], 0);
    IndexOrd.Text := 'Customer #';
    Key := #0; // imiting "Read-Only" effect!
    end;
end;

Regards & Tks again,

Ian

Edited by Ian Branch

Share this post


Link to post
Guest

look again about the use of "key" as DEL, BackSpace ...

try never use Dialogs on Events like this!!! you can have a effect non-desired and crash your apps

Edited by Guest

Share this post


Link to post

I saw the observation about the DEL/Backspace, I am prepared to accept the risk att.

OK.  Noted on not using dialogs in this case.  Unfortunate.  I will revise.

Share this post


Link to post
Guest

for hints, use "Hint / ShowHits" properties

Share this post


Link to post

100% agree, but in this case the Customer's Users are not the quickest and unless something is in their face, it doesn't get much attention/credence. 😞

Share this post


Link to post
Guest
13 hours ago, Lajos Juhász said:

I would just set the property Style to csExDropDownList.

 

who dont read all options, dont see it!!!   :classic_biggrin:

It's a shame by me! Sorry!  :classic_blush:

... uhhhhhh, shame on the moon!.... by Bob Seager!

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×