Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Delphi combobox Clear; creates blank fillers

Posted on 2011-03-16
6
1,919 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Title # Comments Views Activity
Using idhttp to login to instagram 2 106
how to send memory stream from ics Client To ics server ? 11 134
Base1 Encode/Decode 3 81
How to build JSON File in Delphi 6 3 45
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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

809 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