Jump to content

Expand Combobox using code

Recommended Posts

  • Moderators

Basically the <select> element where the combobox is based on, is under control of the browser. And can't be manipulated from pascal/js.

There is a hack (there is a hack for everything) to fake it :

procedure TForm1.InitializeForm;
  // this is a good place to initialize components

  var W3ComboBox1 : Tw3ComboBox := TW3ComboBox.Create(self);
  W3ComboBox1.Add('line 1');
  W3ComboBox1.Add('line 2');
  W3ComboBox1.Add('line 3');
  W3ComboBox1.handle.style['opacity'] := 0;

  var W3Panel1 : TW3Panel := TW3Panel.Create(self);
  W3Panel1.handle.style['z-index'] := 0;
  W3Panel1.handle.setAttribute('class','');  //get rid of all styling

  var W3Button1 : TW3Button := TW3Button.Create(W3Panel1);
  W3Button1.Caption := 'MyButton';

  W3ComboBox1.OnChanged := procedure (Sender: TObject)
    W3Button1.Caption := W3ComboBox1.Items[W3ComboBox1.SelectedIndex];


works on Chrome and other browsers. Works on mobile too


Wouldn't recommend it though ....

edited :

as per @IElite suggestion: it would also be possible to make the combobox invisible, read its items on the click of a button and display them in a listbox or memo.

Better, in my opinion and if really necessary, is to create a dedicated control which does exactly what you want, based on a listbox rather than on a <select> htmlelement


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...