Board index » delphi » Remove text selection in combobox

Remove text selection in combobox

Hi,

I have some ComboBoxes on my form which are filled from
textfiles. The first entry is selected (ItemIndex = 0).
The code in FormShow looks like this:

  with Combobox1 do begin
    Items.LoadFromFile('test.txt');
    if Items.Count>0 then ItemIndex:=0;
  end;

All ComboBoxes have Style = csDropDown. Therefore the text
is editable. Problem: All text in all boxes is selected.
Setting SeLLength:=0 does not help. How can I remove the
selection on all boxes? (I even tried to post a message and
to set the selection to zero when that message arrives after
the form has been shown, but it did not help.)

Regards,
WL

 

Re:Remove text selection in combobox


Quote
Werner Lehmann wrote:
> I have some ComboBoxes on my form which are filled from
> textfiles. The first entry is selected (ItemIndex = 0).
> The code in FormShow looks like this:

>   with Combobox1 do begin
>     Items.LoadFromFile('test.txt');
>     if Items.Count>0 then ItemIndex:=0;
>   end;

> All ComboBoxes have Style = csDropDown. Therefore the text
> is editable. Problem: All text in all boxes is selected.
> Setting SeLLength:=0 does not help. How can I remove the
> selection on all boxes? (I even tried to post a message and
> to set the selection to zero when that message arrives after
> the form has been shown, but it did not help.)

The problem is that whenever the combo box receives the focus, it
automatically selects all the text, regardless of what was
selected previously. If this case, you're setting the selection
correctly, but as soon as the form turns the focus over to the
control, it gets reselected.  Depending on what you really want
to do, this may help:

  PostMessage(ComboBox1.Handle, CB_SETEDITSEL, -1, 0);

Put this in the OnShow event handler for the form to have the
selection changed once at startup. If you want to do it on a more
general basis, put it in the OnEnter event handler for the combo
box.

Good luck.

Kurt

Re:Remove text selection in combobox


Kurt,

Quote
"Kurt Barthelmess (TeamB)" wrote:

> The problem is that whenever the combo box receives the focus, it
> automatically selects all the text, regardless of what was
> selected previously. If this case, you're setting the selection

I would not complain if only the first combobox (=ActiveControl)
would have all its text selected. But all comboboxes (only 2 but...)
have their text selected!

Quote
> correctly, but as soon as the form turns the focus over to the
> control, it gets reselected.  Depending on what you really want

The second combo should not have the focus when the form is shown.
The cursor/caret is in the first one anyway.

Quote
>   PostMessage(ComboBox1.Handle, CB_SETEDITSEL, -1, 0);

> Put this in the OnShow event handler for the form to have the
> selection changed once at startup. If you want to do it on a more
> general basis, put it in the OnEnter event handler for the combo
> box.

I tried that. It did not help. I even found out that right at the
end of OnShow (in which I add the combo items from the textfiles)
the selection length is zero! Without me setting it to zero. This
must be changed at a later time, but where and why and what to do
against it?

Regards,
WL

Re:Remove text selection in combobox


Quote
Werner Lehmann wrote:
> > The problem is that whenever the combo box receives the focus, it
> > automatically selects all the text, regardless of what was
> > selected previously. If this case, you're setting the selection

> I would not complain if only the first combobox (=ActiveControl)
> would have all its text selected. But all comboboxes (only 2 but...)
> have their text selected!

Must be an OS issue - doesn't happen on Win95.

Quote
> > correctly, but as soon as the form turns the focus over to the
> > control, it gets reselected.  Depending on what you really want

> The second combo should not have the focus when the form is shown.
> The cursor/caret is in the first one anyway.

See above.

Quote
> >   PostMessage(ComboBox1.Handle, CB_SETEDITSEL, -1, 0);

> > Put this in the OnShow event handler for the form to have the
> > selection changed once at startup. If you want to do it on a more
> > general basis, put it in the OnEnter event handler for the combo
> > box.

> I tried that. It did not help. I even found out that right at the
> end of OnShow (in which I add the combo items from the textfiles)
> the selection length is zero! Without me setting it to zero. This
> must be changed at a later time, but where and why and what to do
> against it?

I used that code in my OnShow method and did not have a problem.
SelLength is indeed zero as you say. Note that the code given
does not set SelLength; it removes the selection completely.
(SelLength happens to get set to 0 in the process.) Setting
SelLength to zero at this time in the app will not work.

Good luck.

Kurt

Re:Remove text selection in combobox


Quote
"Kurt Barthelmess (TeamB)" wrote:

> Must be an OS issue - doesn't happen on Win95.

Kurt,

thanks for bothering. Well, I was using two or three custom
components and could not really rule the possibility out that
one of these caused the problem. Until, tonight, I could isolate
the problem with standard components in a very small test
project.

The problem appears if the comboboxes are resized at runtime. I
have posted some details to *.objectpascal together with that
test project with subject "Combobox resize selects text! (bug?)".

I am at a point where I already have looked for strange workarounds,
without success so far :(

Regards,
WL

Re:Remove text selection in combobox


Quote
Werner Lehmann wrote:
> The problem appears if the comboboxes are resized at runtime. I
> have posted some details to *.objectpascal together with that
> test project with subject "Combobox resize selects text! (bug?)".

See my response there. You won't like it. Sorry.

Kurt

Other Threads