Jump to content
COMFIED

Expand Combobox using code

Recommended Posts

Hi

Is there a way to trigger a combobox to expand  and show the list without clicking on it - by using code under another visual component like a button?

Share this post


Link to post
Share on other sites
2 hours ago, COMFIED said:

Hi

Is there a way to trigger a combobox to expand  and show the list without clicking on it - by using code under another visual component like a button?

Looks like this is not possible in javascript.

Share this post


Link to post
Share on other sites

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;
begin
  inherited;
  // this is a good place to initialize components

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

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

  var W3Button1 : TW3Button := TW3Button.Create(W3Panel1);
  W3Button1.Caption := 'MyButton';
  W3Button1.SetBounds(0,0,250,30);

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

 

works on Chrome and other browsers. Works on mobile too

Demo

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

 

Share this post


Link to post
Share on other sites

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

×