procedure TForm1.Panel3Click(Sender: TObject);
begin
if item.caption='1' then
begin
form2.WebBrowser1.Navigate('link1');
form2.Caption:='1';
form2.Show;
end;
if item.caption='2' then
begin
form2.WebBrowser1.Navigate('link2');
form2.Caption:='2';
form2.Show;
end;
end.
what im trying to do is on Onclick event check item's caption and then browse a specefic link, this doesnt work and i tried the switch method which doesnt work too(sorry for my bad english), any suggestions on how can i make it work ?
What exactly "doesnt work" for you? You need to be more specific. There is nothing wrong with the code you have shown, provided that item
has been declared and assigned beforehand (and end.
is really end;
).
I suspect that you want item
to be the object that was clicked, is that right? If so, then you simply need to assign this OnClick
handle to the relevant objects and then type-cast the Sender
parameter accordingly, eg:
procedure TForm1.PanelClick(Sender: TObject);
var
item: TPanel;
begin
item := Sender as TPanel; // or TMenuItem, or whatever you are using...
if item.Caption = '1' then
begin
Form2.WebBrowser1.Navigate('link1');
Form2.Caption := '1';
Form2.Show;
end
else if item.Caption = '2' then
begin
Form2.WebBrowser1.Navigate('link2');
Form2.Caption := '2';
Form2.Show;
end;
end;
Personally, I would not use the Caption
to make decisions. I would do something more like this instead:
const
Links: array[1..2] of string = (
'link1',
'link2'
// and so on...
);
procedure TForm1.PanelClick(Sender: TObject);
var
item: TPanel;
begin
item := Sender as TPanel; // or whatever...
Form2.WebBrowser1.Navigate(Links[item.Tag]);
Form2.Caption := item.Caption;
Form2.Show;
end;
Where the object with Caption='1'
has Tag=1
assigned, and the object with Caption='2'
has Tag=2
assigned, and so on.
In the chance that you actually only have 1 object whose Caption
you change dynamically based on other conditions, then this approach still works, simply update the Tag
accordingly whenever you update the Caption
.