Solved

Delphi combobox Clear; creates blank fillers

Posted on 2011-03-16
6
1,853 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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:ewangoya
ewangoya 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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