Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 04-21-2016, 01:13 AM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Location: Sheffield, Tasmania (Aust)
Posts: 76
Default Style change for ComboBox

I have a form with a large number of combo boxes - some with text and one with 'numbers' in the lists. I needed to have the 'numbers' list right justified otherwise it looks messy. I have found some code that achieves this with an OnDrawItem method but to do this you have to set the style to OwnerDrawFixed. This makes that combobox look very different from all the others (DropDownList).

Can anyone give me some hints or code to make the drawn box look like all the others?

Unit Code:-
Code:
unit MainUnit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm8 = class(TForm)
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    ComboBox3: TComboBox;
    procedure ComboBox3DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form8: TForm8;

implementation

{$R *.dfm}

procedure TForm8.ComboBox3DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
   x: Integer;
   txt: String;
begin
   with ComboBox3 do
   begin
     Canvas.FillRect(Rect) ;
     txt := Items[Index];
     x := Rect.Right - Canvas.TextWidth(txt) - 4;
     Canvas.TextOut(x, Rect.Top, txt) ;
   end;
end;
end.
Form Code:-
Code:
object Form8: TForm8
  Left = 0
  Top = 0
  Caption = 'Form8'
  ClientHeight = 201
  ClientWidth = 447
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ComboBox1: TComboBox
    Left = 48
    Top = 32
    Width = 145
    Height = 21
    Style = csDropDownList
    TabOrder = 0
    Items.Strings = (
      '135.70'
      '200.52'
      '1.35'
      '159064.50'
      '.50')
  end
  object ComboBox2: TComboBox
    Left = 48
    Top = 72
    Width = 145
    Height = 21
    Style = csDropDownList
    TabOrder = 1
    Items.Strings = (
      '135.70'
      '200.52'
      '1.35'
      '159064.50'
      '.50')
  end
  object ComboBox3: TComboBox
    Left = 48
    Top = 112
    Width = 145
    Height = 21
    Style = csDropDownList
    TabOrder = 2
    Items.Strings = (
      '135.70'
      '200.52'
      '1.35'
      '159064.50'
      '.50')
  end
end
Reply With Quote
  #2  
Old 06-28-2016, 12:19 PM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,059
Default

Set the "Style" prporty to "csOwnerDrawFixed" and then use "OnDrawItem" event like this:

Code:
procedure TForm6.ComboBox2DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
          sText : String;
begin
          sText := Combobox2.Items[Index];

          ComboBox2.Canvas.FillRect(Rect);

          Dec(Rect.Right,2);
          ComboBox2.Canvas.TextRect(Rect,sText,[tfRight]);
This should make it very similar to the normal drawing except the alignment which will be right as requested.
__________________
Regards,
Abdulaziz Jasser
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 01:04 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.