Solved

Puting an image inside an edit box

Posted on 2008-06-10
6
2,198 Views
Last Modified: 2012-06-27
I would like to insert a small picture <icon> inside an edit box to give a nicer edit box and indicate the function of the edit box. I have attached a picture that represent this. Can anyone kindly provide code to that this.

I have tried setting the edit box as a parent to a timage but that doesn't work.

Note that typing inside the textbox starts just where the image ends i.e. typing doesn't occur above the image.

Thanks.
picinsideeditbox.JPG
0
Comment
Question by:venussc
6 Comments
 
LVL 4

Expert Comment

by:tobjectpascal
ID: 21749361
Might be easier to not use TEdit and use something else to do it instead like Richedit and insert the image then the text, either that or overlay an edit box, kinda messy.
0
 

Author Comment

by:venussc
ID: 21749720
Any other suggestions Please?
0
 
LVL 14

Expert Comment

by:SteveBay
ID: 21750145
You will need a custom component. This discusion show a couple of ways to do it.
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_10200599.html
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:venussc
ID: 21752458
Thanks for the link!! Will have a look at it and give feedback later!
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 250 total points
ID: 21754217
Source code provided below

Regards,
Russell

----

unit EditEx;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

////////////////////////////////////////////////////////////////////////////////
//   TEditEx
////////////////////////////////////////////////////////////////////////////////
type
  TEditEx           =  class(TEdit)
  private
     // Private declarations
     FSize:         Integer;
     FPicture:      TPicture;
     FImage:        TImage;
  protected
     // Protected declarations
     procedure      Change; override;
     procedure      CreateWnd; override;
     procedure      PictureChanged(Sender: TObject);
     procedure      SetMargin;
     procedure      SetPicture(Value: TPicture);
     procedure      WMKillFocus(var Message: TMessage); message WM_KILLFOCUS;
     procedure      WMSetFocus(var Message: TMessage); message WM_SETFOCUS;
     procedure      WMSize(var Message: TWMSize); message WM_SIZE;
  public
     // Public declarations
     constructor    Create(AOwner: TComponent); override;
     destructor     Destroy; override;
  published
     // Published declarations
     property       Anchors;
     property       AutoSelect;
     property       AutoSize;
     property       BiDiMode;
     property       BorderStyle;
     property       CharCase;
     property       Color;
     property       Constraints;
     property       Ctl3D;
     property       DragCursor;
     property       DragKind;
     property       DragMode;
     property       Enabled;
     property       Font;
     property       HideSelection;
     property       ImeMode;
     property       ImeName;
     property       MaxLength;
     property       OEMConvert;
     property       ParentBiDiMode;
     property       ParentColor;
     property       ParentCtl3D;
     property       ParentFont;
     property       ParentShowHint;
     property       PasswordChar;
     property       PopupMenu;
     property       ReadOnly;
     property       ShowHint;
     property       TabOrder;
     property       TabStop;
     property       Text;
     property       Visible;
     property       OnChange;
     property       OnClick;
     property       OnContextPopup;
     property       OnDblClick;
     property       OnDragDrop;
     property       OnDragOver;
     property       OnEndDock;
     property       OnEndDrag;
     property       OnEnter;
     property       OnExit;
     property       OnKeyDown;
     property       OnKeyPress;
     property       OnKeyUp;
     property       OnMouseDown;
     property       OnMouseMove;
     property       OnMouseUp;
     property       OnStartDock;
     property       OnStartDrag;
     // Published extensions
     property       Picture: TPicture read FPicture write SetPicture;
  end;

procedure Register;

implementation

//// TEditEx ///////////////////////////////////////////////////////////////////
constructor TEditEx.Create(AOwner: TComponent);
begin

  // Perform inherited
  inherited Create(AOwner);

  // Set control style
  ControlStyle:=ControlStyle + [csReplicatable];

  // Set defaults
  FSize:=0;
  FImage:=TImage.Create(Self);
  FImage.Transparent:=True;
  FImage.Stretch:=True;
  FImage.Center:=True;
  FImage.Parent:=Self;
  FImage.SetBounds(0, 0, 0, Height);
  FPicture:=TPicture.Create;
  FPicture.OnChange:=PictureChanged;

end;

destructor TEditEx.Destroy;
begin

  // Resource protection
  try
     // Free the image
     FImage.Free;
     // Cleanup
     FPicture.Free;
  finally
     // Perform inherited
     inherited Destroy;
  end;

end;

procedure TEditEx.Change;
begin

  // Perform inherited
  inherited;

  // Repaint
  FImage.Repaint;

end;

procedure TEditEx.SetPicture(Value: TPicture);
begin

  // Assign from passed value
  FPicture.Assign(Value);

end;

procedure TEditEx.PictureChanged(Sender: TObject);
begin

  // Load the image
  FImage.Picture.Assign(FPicture);

  // Update the margins in case image was cleared
  SetMargin;

end;

procedure TEditEx.WMKillFocus(var Message: TMessage);
begin

  // Perform inherited
  inherited;

  // Repaint
  FImage.Repaint;

end;

procedure TEditEx.WMSetFocus(var Message: TMessage);
begin

  // Perform inherited
  inherited;

  // Repaint
  FImage.Repaint;

end;

procedure TEditEx.WMSize(var Message: TWMSize);
begin

  // Perform inherited
  inherited;

  // Update the size
  FSize:=Message.Height;

  // Set the left size margin
  SetMargin;

end;

procedure TEditEx.CreateWnd;
begin

  // Perform inherited
  inherited;

  // Set the left size margin
  SetMargin;

end;

procedure TEditEx.SetMargin;
begin

  // Resource protection
  try
     // Check state
     if (FSize = 0) or (FPicture.Graphic = nil) or FPicture.Graphic.Empty then
        // Clear border
        Perform(EM_SETMARGINS, EC_LEFTMARGIN, MakeLong(0, 0))
     else
        // Set left size border
        Perform(EM_SETMARGINS, EC_LEFTMARGIN, MakeLong(FSize + 4, 0));
  finally
     // Set the image bounds
     FImage.SetBounds(1, 1, FSize - 2, FSize - 2);
  end;

end;

procedure Register;
begin

  // Register the component in the Additional tab
  RegisterComponents('Additional', [TEditEx]);

end;

end.
0
 

Expert Comment

by:klompen
ID: 23974692
There is a bug in this TEditEx component.

Here is how to reproduce the bug:

- Create a TEditEx object in a form
- Load that small picture (16x16 pixels)
- Then click the edit area so it get focused
- Type a long text until the text scrolled inside the edit area
- Now, use mouse ... point to the right-most side of the edit area
- press and hold the left mouse (so the text will be selected if you move the mouse)
- then move the mouse to the far far left

You will see that the small image will be sort of cleared ...
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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