将项目添加到函数中的列表框 (Delphi 7)
add items to a listbox in a function (Delphi 7)
我想写一个函数来检查某个字母是否在某个单词中。
这是当前函数(对不起德国人)
function woistderbuchstabe (wort, buchstabe:String):String;
VAR i: Integer;
begin
for i:=1 to length(wort) do
if wort[i]=buchstabe then
showmessage(INTtoSTR(i))
//LB_ausgabe.items.add(INTtoSTR(i));
end;
按照现在的写法,函数确实可以工作了。它显示一条或多条消息,其中包含在单词 "wort" 中搜索的字母(变量 "buchstabe")的位置。例如。对于 wort=abctc 和 buchstabe=c,它显示 3 和 5。
但是如果我这样写的话
function woistderbuchstabe (wort, buchstabe:String):String;
VAR i: Integer;
begin
for i:=1 to length(wort) do
if wort[i]=buchstabe then
LB_ausgabe.items.add(INTtoSTR(i));
end;
(删除 showmessage 并使 ListBox 成为实际代码)
然后我得到错误
Undefined Identifier: 'LB_ausgabe'
这是单元的完整代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
E_kette: TEdit;
E_buchstabe: TEdit;
B_start: TButton;
LB_ausgabe: TListBox;
procedure B_startClick(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function woistderbuchstabe (wort, buchstabe:String):String;
VAR i: Integer;
begin
for i:=1 to length(wort) do
if wort[i]=buchstabe then
showmessage(INTtoSTR(i))
//LB_ausgabe.items.add(INTtoSTR(i));
end;
procedure TForm1.B_startClick(Sender: TObject);
begin
woistderbuchstabe (E_kette.text, E_buchstabe.text);
end;
end.
请尝试具体一点,因为我对 Delphi 很无能。
提前致谢
函数 woistderbuchstabe
不是您的 class TForm1
... 的成员,因此它不能直接访问它的成员,除非您指定一个实例。我建议此修复:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
E_kette: TEdit;
E_buchstabe: TEdit;
B_start: TButton;
LB_ausgabe: TListBox;
procedure B_startClick(Sender: TObject);
private
{ Private-Deklarationen }
function woistderbuchstabe (wort, buchstabe:String):String;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.woistderbuchstabe (wort, buchstabe:String):String;
VAR i: Integer;
begin
for i:=1 to length(wort) do
if wort[i]=buchstabe then
LB_ausgabe.items.add(INTtoSTR(i));
end;
procedure TForm1.B_startClick(Sender: TObject);
begin
woistderbuchstabe (E_kette.text, E_buchstabe.text);
end;
end.
但是你也可以在你的函数中引用你的 Form1: TForm1
实例(全局变量)(不过我建议你坚持使用 OO 方法):
Form1.LB_ausgabe.items.add(INTtoSTR(i));
PS:还要检查 Pos
和 PosEx
函数,因为它们可能是(我从未进行过基准测试)更快的解决方案,因为它们是 asm
实现的。
在我看来,如果您可能需要从其他地方使用此逻辑,则需要将其与您的用户界面分离。您可以通过将函数更改为接受更通用的 class 来填充(如普通的旧 TStrings
)作为参数的过程来实现此目的。由于 TStrings
是 TComboBox.Items
、TListBox.Items
、TMemo.Lines
的通用基础,并在许多其他地方使用,这似乎是完成您想做的事情的最灵活的方式.
procedure woistderbuchstabe (List: TStrings; wort, buchstabe:String);
VAR i: Integer;
begin
for i := 1 to length(wort) do
if wort[i] = buchstabe then
List.Add(InttoStr(i));
end;
这允许您将程序与您的 TListBox
一起使用(使用 LB_ausgabe.items
、TMemo
、使用 Memo1.Lines
、TComboBox
Combobox1.Items
、TRichEdit
与 RichEdit1.Lines
或直接与 SL
的普通旧 TStringList
。
您现在可以从任何地方调用它,例如 TForm.Button1Click(Sender: TObject)
,使用 ListBox1.Items
,或创建并传入 TStringList
的独立方法。它不依赖于特定的形式,因此它更灵活并且可以在其他地方重复使用。
我想写一个函数来检查某个字母是否在某个单词中。
这是当前函数(对不起德国人)
function woistderbuchstabe (wort, buchstabe:String):String;
VAR i: Integer;
begin
for i:=1 to length(wort) do
if wort[i]=buchstabe then
showmessage(INTtoSTR(i))
//LB_ausgabe.items.add(INTtoSTR(i));
end;
按照现在的写法,函数确实可以工作了。它显示一条或多条消息,其中包含在单词 "wort" 中搜索的字母(变量 "buchstabe")的位置。例如。对于 wort=abctc 和 buchstabe=c,它显示 3 和 5。
但是如果我这样写的话
function woistderbuchstabe (wort, buchstabe:String):String;
VAR i: Integer;
begin
for i:=1 to length(wort) do
if wort[i]=buchstabe then
LB_ausgabe.items.add(INTtoSTR(i));
end;
(删除 showmessage 并使 ListBox 成为实际代码)
然后我得到错误
Undefined Identifier: 'LB_ausgabe'
这是单元的完整代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
E_kette: TEdit;
E_buchstabe: TEdit;
B_start: TButton;
LB_ausgabe: TListBox;
procedure B_startClick(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function woistderbuchstabe (wort, buchstabe:String):String;
VAR i: Integer;
begin
for i:=1 to length(wort) do
if wort[i]=buchstabe then
showmessage(INTtoSTR(i))
//LB_ausgabe.items.add(INTtoSTR(i));
end;
procedure TForm1.B_startClick(Sender: TObject);
begin
woistderbuchstabe (E_kette.text, E_buchstabe.text);
end;
end.
请尝试具体一点,因为我对 Delphi 很无能。
提前致谢
函数 woistderbuchstabe
不是您的 class TForm1
... 的成员,因此它不能直接访问它的成员,除非您指定一个实例。我建议此修复:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
E_kette: TEdit;
E_buchstabe: TEdit;
B_start: TButton;
LB_ausgabe: TListBox;
procedure B_startClick(Sender: TObject);
private
{ Private-Deklarationen }
function woistderbuchstabe (wort, buchstabe:String):String;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.woistderbuchstabe (wort, buchstabe:String):String;
VAR i: Integer;
begin
for i:=1 to length(wort) do
if wort[i]=buchstabe then
LB_ausgabe.items.add(INTtoSTR(i));
end;
procedure TForm1.B_startClick(Sender: TObject);
begin
woistderbuchstabe (E_kette.text, E_buchstabe.text);
end;
end.
但是你也可以在你的函数中引用你的 Form1: TForm1
实例(全局变量)(不过我建议你坚持使用 OO 方法):
Form1.LB_ausgabe.items.add(INTtoSTR(i));
PS:还要检查 Pos
和 PosEx
函数,因为它们可能是(我从未进行过基准测试)更快的解决方案,因为它们是 asm
实现的。
在我看来,如果您可能需要从其他地方使用此逻辑,则需要将其与您的用户界面分离。您可以通过将函数更改为接受更通用的 class 来填充(如普通的旧 TStrings
)作为参数的过程来实现此目的。由于 TStrings
是 TComboBox.Items
、TListBox.Items
、TMemo.Lines
的通用基础,并在许多其他地方使用,这似乎是完成您想做的事情的最灵活的方式.
procedure woistderbuchstabe (List: TStrings; wort, buchstabe:String);
VAR i: Integer;
begin
for i := 1 to length(wort) do
if wort[i] = buchstabe then
List.Add(InttoStr(i));
end;
这允许您将程序与您的 TListBox
一起使用(使用 LB_ausgabe.items
、TMemo
、使用 Memo1.Lines
、TComboBox
Combobox1.Items
、TRichEdit
与 RichEdit1.Lines
或直接与 SL
的普通旧 TStringList
。
您现在可以从任何地方调用它,例如 TForm.Button1Click(Sender: TObject)
,使用 ListBox1.Items
,或创建并传入 TStringList
的独立方法。它不依赖于特定的形式,因此它更灵活并且可以在其他地方重复使用。