Solved

Delphi combobox Clear; creates blank fillers

Posted on 2011-03-16
6
1,724 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now