Why Text do not change in Onclick Event of ComboBox


procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Items.Add('One');
ComboBox1.Items.Add('Two');
ComboBox1.Items.Add('Three');
ComboBox1.Items.Add('Four');
end;

procedure TForm1.ComboBox1Click(Sender: TObject);
begin
ComboBox1.Text:='Text Changed';
end;

This code is clear that whenever you Click the ComboBox Items,
the Text of the ComboBox should be 'Text Changed'.

But why the Click event code do not workout?
My need is to change the Combo Text in the Click Event of ComboBox.

I tried with OnSelect,OnChange Events also but did not work.
Also tried with SendMessge(Handle,CB_SETITEMDATA,0,LPARAM('Data'));

Great thanks for your comments.


Comments

  • :
    : procedure TForm1.FormCreate(Sender: TObject);
    : begin
    : ComboBox1.Items.Add('One');
    : ComboBox1.Items.Add('Two');
    : ComboBox1.Items.Add('Three');
    : ComboBox1.Items.Add('Four');
    : end;
    :
    : procedure TForm1.ComboBox1Click(Sender: TObject);
    : begin
    : ComboBox1.Text:='Text Changed';
    : end;
    :
    : This code is clear that whenever you Click the ComboBox Items,
    : the Text of the ComboBox should be 'Text Changed'.
    :
    : But why the Click event code do not workout?
    : My need is to change the Combo Text in the Click Event of ComboBox.
    :
    : I tried with OnSelect,OnChange Events also but did not work.
    : Also tried with SendMessge(Handle,CB_SETITEMDATA,0,LPARAM('Data'));
    :
    : Great thanks for your comments.
    :
    :
    :
    I think (but am not sure) that the text does change, but is immediately overwritten by the CB_SetItemData message, which is posted in the message queue due to the click. The same problem occurs with SendMessage(), which causes the message to be directly handled by the control. You could try to use PostMessage() instead, which adds the change to the message queue instead of directly handling the message.
  • : :
    : : procedure TForm1.FormCreate(Sender: TObject);
    : : begin
    : : ComboBox1.Items.Add('One');
    : : ComboBox1.Items.Add('Two');
    : : ComboBox1.Items.Add('Three');
    : : ComboBox1.Items.Add('Four');
    : : end;
    : :
    : : procedure TForm1.ComboBox1Click(Sender: TObject);
    : : begin
    : : ComboBox1.Text:='Text Changed';
    : : end;
    : :
    : : This code is clear that whenever you Click the ComboBox Items,
    : : the Text of the ComboBox should be 'Text Changed'.
    : :
    : : But why the Click event code do not workout?
    : : My need is to change the Combo Text in the Click Event of ComboBox.
    : :
    : : I tried with OnSelect,OnChange Events also but did not work.
    : : Also tried with SendMessge(Handle,CB_SETITEMDATA,0,LPARAM('Data'));
    : :
    : : Great thanks for your comments.
    : :
    : :
    : :
    : I think (but am not sure) that the text does change, but is immediately overwritten by the CB_SetItemData message, which is posted in the message queue due to the click. The same problem occurs with SendMessage(), which causes the message to be directly handled by the control. You could try to use PostMessage() instead, which adds the change to the message queue instead of directly handling the message.
    :

    Even without CB_SETITEMDATA and SendMessage(),just only the code alone
    is NOT working.


  • : : :
    : : : procedure TForm1.FormCreate(Sender: TObject);
    : : : begin
    : : : ComboBox1.Items.Add('One');
    : : : ComboBox1.Items.Add('Two');
    : : : ComboBox1.Items.Add('Three');
    : : : ComboBox1.Items.Add('Four');
    : : : end;
    : : :
    : : : procedure TForm1.ComboBox1Click(Sender: TObject);
    : : : begin
    : : : ComboBox1.Text:='Text Changed';
    : : : end;
    : : :
    : : : This code is clear that whenever you Click the ComboBox Items,
    : : : the Text of the ComboBox should be 'Text Changed'.
    : : :
    : : : But why the Click event code do not workout?
    : : : My need is to change the Combo Text in the Click Event of ComboBox.
    : : :
    : : : I tried with OnSelect,OnChange Events also but did not work.
    : : : Also tried with SendMessge(Handle,CB_SETITEMDATA,0,LPARAM('Data'));
    : : :
    : : : Great thanks for your comments.
    : : :
    : : :
    : : :
    : : I think (but am not sure) that the text does change, but is immediately overwritten by the CB_SetItemData message, which is posted in the message queue due to the click. The same problem occurs with SendMessage(), which causes the message to be directly handled by the control. You could try to use PostMessage() instead, which adds the change to the message queue instead of directly handling the message.
    : :
    :
    : Even without CB_SETITEMDATA and SendMessage(),just only the code alone
    : is NOT working.
    :
    :
    :
    The OnClick() event does get called and the text does change. Both can be seen while debugging and watching the Text property. The problem is that in the message queue there are still one or more unhandled messages, which change the Text again. The CB_SETITEMDATA message is not the way, because that is only used to change a string in the Items. The message to change the Text is the WM_SETTEXT message followed by a CM_TEXTCHANGED message.
    Here is a possible elegant solution: You might try to remove all the messages to the combobox from the queue, which might stop the change back to the selected value.
    A sure solution is to wait a short time before making the change. As a test I implemented it using a timer with an interval fo 50 ms. The OnClick() enabled this timer, while the OnTimer() event performed the actual change. This allowed all pending messages to be handled before making the change.
  • This is too easy!

    Instead of listening for the "change" event try "close" ;) It worked for me OR you could go [link=http://www.flashwebtools.com/default.asp?id=115]here[/link] for more combo box listners.

    Cheers!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories