0

how can i have a listview that its items contain links(direct us to html pages) ?

Thank you

Kermia
  • 4,171
  • 13
  • 64
  • 105

2 Answers2

6

Either use a listview or grid which supports this off-the-shelf (tms software for example has components that support a "mini" html) or with a standard TListView do something like:

type
  TLinkItem = class(TObject)
  private
    FCaption: string;
    FURL: string;
  public
    constructor Create(const aCaption, aURL: string);
    property Caption: string read FCaption write FCaption;
    property URL: string read FURL write FURL;
  end;

constructor TLinkItem.Create(const aCaption, aURL: string);
begin
  FCaption := aCaption;
  FURL := aURL;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  Item: TListItem;
  i: Integer;
begin
  FLinkItems := TObjectList.Create({AOwnsObjects=}True);
  FLinkItems.Add(TLinkItem.Create('StackOverflow', 'http://www.stackoverflow.com'));
  FLinkItems.Add(TLinkItem.Create('BJM Software', 'http://www.bjmsoftware.com'));

  for i := 0 to FLinkItems.Count - 1 do
  begin
    Item := ListView1.Items.Add;
    Item.Caption := TLinkItem(FLinkItems[i]).Caption;
    Item.Data := Pointer(FLinkItems[i]);
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FreeAndNil(FLinkItems);
end;

procedure TForm1.ListView1Click(Sender: TObject);
var
  LinkItem: TLinkItem;
  URL: string;
begin
  LinkItem := TLinkItem(ListView1.Items[ListView1.ItemIndex].Data);
  URL := LinkItem.URL;
  ShellExecute(Handle, 'open', PChar(URL), nil, nil, SW_SHOW);
end;

It's up to you how you want your color the link captions in your ListView. If you were to adhere to the long-held Internet standards you would make them blue and underlined.

Marjan Venema
  • 19,136
  • 6
  • 65
  • 79
  • I think you should edit your code ! what is "FLinkItems" ? i can't understand. – Kermia Jan 29 '11 at 19:16
  • 2
    Should? I think I have given you enough to go on without effectively doing your work for you. FLinkItems is declared as a member field in the class declaration of the form which holds the TListView component. – Marjan Venema Jan 29 '11 at 19:29
2

Um, yes, it is easy to go ahead and call the default browser with Delphi. Here's a basic example with validation (so you can have non-URL values in your list):

uses ShLwApi, ShellApi;

procedure TForm1.ListView1DblClick(Sender: TObject);
begin
  if PathIsURL(PChar(ListView1.Selected.Caption)) then
  begin
    ShellExecute(self.WindowHandle, 'open', PChar(ListView1.Selected.Caption),
      nil, nil, SW_SHOWNORMAL);
  end;
end;
Leonardo Herrera
  • 8,388
  • 5
  • 36
  • 66