Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delphi combobox Clear; creates blank fillers

Posted on 2011-03-16
6
Medium Priority
?
2,438 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 24

Accepted Solution

by:
jimyX earned 500 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Suggested Courses

972 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