⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 147.htm

📁 水木清华的BBS文章
💻 HTM
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://bbs.tsinghua.edu.cn"><font face="黑体"><big><big>水木清华★</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center">         Delphi编程                            (BM: strayli FlyingBoy)          </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p   align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="454"> <p align="center">[<a href="index.htm">回到开始</a>][<a href="7.htm">上一层</a>][<a href="148.htm">下一篇</a>]
<hr><p align="left"><small>发信人: strayli (stray), 信区: Delphi <br>

标  题: How to associate a string with a component <br>

发信站: BBS 水木清华站 (Mon Sep 21 13:56:30 1998) <br>

  <br>

KEYWORDS: string component AREA: VCL Programming <br>

  <br>

Q:  Is there a way to associate a string with each component? <br>

  <br>

A:  Since the Tag property is a longint, you can type cast it <br>

as a Pointer or PChar.  So, you can basically store a pointer <br>

to a record by using the Tag property. <br>

  <br>

Note:  You're not going to be able to store the string, or <br>

pointer rather, at design time. This is something you'll have <br>

to do at run time. Take a look at this example: <br>

  <br>

 var <br>

  i: integer; <br>

 begin <br>

   for i := 0 to ComponentCount - 1 do <br>

  <br>

     if Components[i] is TEdit then <br>

       Components[i].Tag := LongInt(NewStr('Hello '+IntToStr(i))); <br>



 end; <br>

  <br>

Here, I loop through the components on the form.  If the <br>

component is a TEdit, I assign a pointer to a string to its Tag <br>

property.  The NewStr function returns a PString (pointer to a <br>

string).  A pointer is basically the same as a longint or <br>

better, occupies the same number of bytes in memory. Therefore, <br>

you can type cast the return value of NewStr as a LongInt and <br>

store it in the Tag property of the TEdit component.  Keep in <br>

mind that this could have been a pointer to an entire record. <br>

Now I'll use that value: <br>

  <br>

 var <br>

  i: integer; <br>

 begin <br>

   for i := 0 to ComponentCount - 1 do <br>

     if Components[i] is TEdit then begin <br>

       TEdit(Components[i]).Text := PString(Components[i].Tag)^; <br>

       DisposeStr(PString(Components[i].Tag)); <br>

     end; <br>

 end; <br>

  <br>

  <br>

Here, again I loop through the components and work on only the <br>

TEdits.  This time, I extract the value of the component's Tag <br>

property by typecasting it as a PString (Pointer to a string) <br>

and assigning that value to the TEdit's Text property. Of <br>

course, I must dereference it with the caret (^) symbol.  Once <br>

I do that, I dispose of the string stored in the edit <br>

component.  Important note: if you store anything in the <br>

TEdit's Tag property as a pointer, you are responsible for <br>

disposing of it also. <br>

  <br>

FYI, Since Delphi objects are really pointers to class <br>

instances, you can also store objects in the Tag property. As <br>

long as you remember to Free them. <br>

  <br>

Three methods spring to mind to use Tags to access strings that <br>

persist from app to app. <br>

  <br>

1.  If your strings stay the same forever, create a string <br>

resource in Resource Workshop (or equiv) and use the Tags as <br>

indexes into your string resource. <br>

  <br>

2.  Use TIniFile and create a section for your strings, and <br>



give each string a name with number so that your ini file has a <br>

section like this: <br>

  <br>

[strings] <br>

string1=Aristotle <br>

string2=Plato <br>

string3=Well this is Delphi, after all <br>

  <br>

Then you can fetch them back out this way: <br>

  <br>

  var s1: string; <br>

  ... <br>

  s1 := IniFile1.ReadString('strings', 'string'+IntToStr(Tag), ''); <br>

  <br>

3.  Put your strings into a file, with each followed by a <br>

carriage return.  Read them into a TStringList.  Then your Tags <br>

become an index into this stringlist: <br>

  <br>

  StringList1.LoadFromFile('slist.txt'); <br>

  ... <br>

  s1 := StringList1[Tag]; <br>

  <br>

.txt'); <br>

  ... <br>

  s1 := StringList1[Tag]; <br>

  <br>

Given the way Delphi is set up, I think the inifile method is easiest. <br>

  <br>

-- <br>

※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 210.45.208.4] <br>

</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="7.htm">上一层</a>][<a href="148.htm">下一篇</a>]
<p align="center"><a href="http://cterm.163.net">欢迎访问Cterm主页</a></p>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -