Solved

Puting an image inside an edit box

Posted on 2008-06-10
6
2,240 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
[X]
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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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 …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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