Solved

Delphi combobox Clear; creates blank fillers

Posted on 2011-03-16
6
1,964 Views
Last Modified: 2012-08-14
with stdCalc do
    begin
    Clear;
    Items.add('n = None');
    if (mbm.mbmid <> '') AND (prt.basetype='F') then
      begin
      if portLen > 1 then
        Items.add('1 = 1 Year');
    end;
  end;

Open in new window


The Clear; function deletes everything and creates a blank item and then adds the other items below the blank item.

How can I modify this to not have the box area fill in blank?

Thanks,
0
Comment
Question by:keats88
6 Comments
 
LVL 8

Expert Comment

by:BdLm
ID: 35148187
what do you like inside the box ?
0
 
LVL 2

Expert Comment

by:RezaSadigh
ID: 35148246
Hi my friend,
if you want to have the selected Item,
you can get it before clear and than after clear add it again. for example
.....
s:= text;
Clear;
Items.Add(s)
......

maybe i cant understand your mean, if so please explain mor clear.
Best regards

0
 
LVL 8

Expert Comment

by:BdLm
ID: 35148306
draw the combox box black :

see code below :


You need to set the property Style to "csOwnerDrawFixed".


This causes a call of OnDrawItem for each item in your combobox.
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
     combobox1.items.Add(IntToStr(combobox1.Items.Count));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
    combobox1.items.Clear;

    combobox1.text := 'NEW';
end;





procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
     with Control as TComboBox,Canvas do

  begin

    // fill the rectangle first with white

    Brush.Color := clBlack;

    FillRect(Rect);

    // then reduce it and fill it with the color

    InflateRect(Rect,-2,-2);

    Brush.Color := StrToInt(Items[Index]);

    FillRect(Rect);

end;

end;

end.

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Accepted Solution

by:
jimyX earned 125 total points
ID: 35148893
You need to set the ItemIndex:

with stdCalc do
    begin
      Clear;
      Items.add('n = None');
      if (mbm.mbmid <> '') AND (prt.basetype='F') then
       begin
        if portLen > 1 then
         Items.add('1 = 1 Year');
       end;
      itemIndex := 0;
    end;

Open in new window

0
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 125 total points
ID: 35150568
try
var
  CurrentItem: string;
......
    with stdCalc do
    begin
      CurrentItem := stdCalc.Text;
      Clear;
      Items.add('n = None');
      if (mbm.mbmid <> '') AND (prt.basetype='F') then
      begin
        if portLen > 1 then
          Items.add('1 = 1 Year');
      end;
      if (Trim(CurrentItem) <> '') and (Items.IndexOf(CurrentItem) >= 0) then
         ItemIndex := Items.IndexOf(CurrentItem) 
      else
         ItemIndex := 0; 
    end;

Open in new window

0
 

Author Closing Comment

by:keats88
ID: 35151149
These worked perfectly. Thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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