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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.