Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Puting an image inside an edit box

Posted on 2008-06-10
6
Medium Priority
?
2,339 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
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.

 

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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

721 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