Getting Errors with a String

Hi I can't get the Caption name into a String? is it possible to do that? without having the & within it?

I'm just wanting the name of the popupmenu caption in a string.



var
  i: integer;
  name:string;
begin
name:=PopupMenu1.Items[i].Caption;//gives error

Open in new window

LVL 5
eNarcAsked:
Who is Participating?
 
jimyXCommented:
try this one:
var
  i: integer;
  name:string;
begin
//make sure to assign value to i which is less or equal than the count of the PopupMenu items count
name:=PopupMenu1.Items.Items[i].Caption;

Open in new window

0
 
jimyXCommented:
I believe the error you get is because of the variable i:
var
  i: integer;
  name:string;
begin
i:=0; // the first item
name:=PopupMenu1.Items[i].Caption;

Open in new window


Make sure the variable "i" is in the range of 0 and the PopupMenu itemscount-1.
0
 
eNarcAuthor Commented:
I found that the Caption holds a & within the Caption.

how would I be able to remove that &?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
eNarcAuthor Commented:
keeps saying not a valid component name?

Name:=PopupMenu1.Items[0].Caption;
0
 
jimyXCommented:
When you use single ampersand "&" it make the very next character underlined. But when you want the ampersand to be shown you use double ampersands "&&" then it shows only one.

If you mean to remove it when copying the caption then:

Name:= StringReplace(PopupMenu1.Items[0].Caption,'&','',[rfReplaceAll]);

Can you show the dfm of the unit that includes the PopupMenu please.
0
 
jimyXCommented:
For better readability and correct showing of text:
var
  name, temp:string;
begin
  temp:= StringReplace(PopupMenu1.Items[0].Caption,'&&','#',[rfReplaceAll]);
  temp:= StringReplace(temp,'&','',[rfReplaceAll]);
  Name:= StringReplace(temp,'#','&',[rfReplaceAll]);

Open in new window

0
 
Ephraim WangoyaCommented:

Dont use "Name"

Do it directly or rename your variable

var
  i: integer;
  MenuCaption:string;
begin
  MenuCaption :=PopupMenu1.Items.Items[0].Caption;
  Edit1.Text := MenuCaption;

or just
  Edit1.Text := PopupMenu1.Items.Items[0].Caption;
0
 
Ephraim WangoyaCommented:
Yap, use the stringreplace as shown in previous post

Edit1.Text := StringReplace(PopupMenu1.Items[0].Caption, '&', '', []);
0
 
Geert GOracle dbaCommented:
this is the same problem as your other question

and yeah, never use name in a form as a variable !
0
 
eNarcAuthor Commented:
I placed it within a with form1 do begin so it used name for the form, my bad.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.