?
Solved

Change combobox text

Posted on 2003-02-21
8
Medium Priority
?
3,556 Views
Last Modified: 2010-07-27
I am trying to change the combobox text on selection.

What I am doing is the following:

I fill the list with a set of strings read out from an ini file.
On selection I want to replace the string with a value associated with the string.

For example the ini file has the entry:

power cycle=a5 00 05 50

The the list shows "power cycle", when the user selects the entry the edit box is supposed to show "a5 00 05 50"

I am using the "OnSelect" event in Delphi to change the text property but the change does not show up. When I trace through the code all variables are fine and perform correctly. I assume that another event is happening that I am not aware of that resets the text property back to the list entry.

I am using Delphi 6
0
Comment
Question by:gsteup
8 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7994311
what combobox-style do you use?
0
 
LVL 3

Expert Comment

by:sfock
ID: 7994315
use OnChange or probably onClick and code

if cb.itemIndex >= 0 then
  cb.text := privAssociatedTextTo(cb.items[]);
0
 
LVL 3

Expert Comment

by:sfock
ID: 7994329
uups
i meant
if cb.itemIndex >= 0 then
 cb.text := privAssociatedTextTo(cb.items[cb.itemIndex]);

and yes kretzschmar is right the syle should not be csDropDownList
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:gsteup
ID: 7994511
Ok,

kretzschmar:
the style is csDropDown

sfock:
I used OnChange
and even hardcoded a string:

procedure TForm1.ComboBox3Change(Sender: TObject);

begin
  combobox3.Text:='test';
end;

The edit box still shows the selected entry in the drop down list, no matter what I do to the text at OnChange.
0
 
LVL 3

Accepted Solution

by:
sfock earned 150 total points
ID: 7994862
well i just tried and saw you are right. Sorry for the bad tip!
the on change event works, if you type a letter, but this is not what you want.
It does also work if you assign it from "out of the List Select"

Anyhow the only surlely working solution i found jet is to place a TEdit over the Combobox EditFiled and assign your related text there ... sorry for now
0
 
LVL 2

Author Comment

by:gsteup
ID: 7996188
Yes, that thought had crossed my mind ones or twice and you are right, it is a workaround.

What I said earlier is still troubling me though. The onChange event gets called and the text property is changed during that.
There must be some other event later on that is reversing this change. Is there a way to trace all the events that happen to the combobox?
Or maybe that is a bug in Delphi that hasn't been adressed yet, who knows...
0
 
LVL 17

Expert Comment

by:geobul
ID: 7996663
Hi,

1. Load the items from the ini file as they are written there:
ComboBox1.Items[x] should be 'power cycle=a5 00 05 50'

2.Set the Style property to csOwnerDrawFixed and write the following OnDrawItem event handler:

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  if (Control is TComboBox) then begin
    with (Control as TComboBox) do begin
      Canvas.FillRect(Rect);
      if odComboBoxEdit in State then begin
        Canvas.TextOut(Rect.Left + 2, Rect.Top+2, Items.Values[Items.Names[Index]]);
      end else begin
        Canvas.TextOut(Rect.Left + 2, Rect.Top+2, Items.Names[Index]);
      end;
    end;
  end;
end;

Regards, Geo
0
 
LVL 2

Author Comment

by:gsteup
ID: 7999694
geobul:

Thanks for your solution. It works great, unfortunately I need the edit box to complete or fill in the commands after I select a template out of the list. So it doesn't work for me in this particuar case.

Sfock:

I implemented the overlaying edit box and it work fine. I will exept your comment as an answer.

Thanks everybody!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question