1

我有一个 TSpeedButton 类型的自定义组件,它定义了两个额外的属性:

CommentHeading: string;
CommentText: string;

我在设计时设置了 CommentHeading。

当按下速度按钮时,会显示一个备忘录,其下方有一个用于保存其内容的按钮。处理此问题的过程:

procedure CustomSpeedButton1Click(Sender: TObject);
begin
   Receiver := CustomSpeedButton1.Name; // possibly used to save the memo text back to this speedbuttons property after comments are submitted
   ViewComments(CustomSpeedButton1.CommentTitle,CustomSpeedButton1.CommentText);
end;

ViewComments 过程本身:

procedure ViewComments(comment_caption:string; comment_text:string);
begin
  label15.Hide; // label showing editing in progress, hidden until user begins typing
  Button1.Enabled     := false; // the button for saving the memo text, hidden until user begins typing
  CommentsBox.Visible := true; // pop up the comment box at the bottom of the form
  CommentsBox.Caption := 'Comments: ' + comment_caption;
  CommentsMemo.Text   := comment_text; // if there are existing comments assign them to memo
end;

备忘录的内容需要分配给自定义 SpeedButton 的 CommentText 属性。

我最初的想法是,我可以在按下自定义 SpeedButton 时将组件名称传递给变量,然后在按下备忘录上的保存按钮时检索该名称,并使用它将备忘录文本分配给 speedbuttons CommentText 属性。但后来我意识到,要做到这一点,我必须使用某种 case..of 语句来检查每个可能的 speedbutton 名称,然后将 memo 值分配给它的属性,这看起来非常乏味。

有没有更简单的方法可以将备忘录文本分配给打开备忘录的快速按钮?

4

3 回答 3

2

既然您已经在传递额外的变量,为什么不直接传递 SpeedButton 本身呢?然后,您将不需要查找参考。

于 2009-08-27T00:16:06.073 回答
2

最终,您要问的是如何告诉ViewComments函数它正在使用哪个按钮的属性。

您了解Sender参数在OnClick事件中的作用吗?它告诉事件处理程序正在处理哪个对象的事件。它正是服务于您希望为该ViewComments功能带来的角色。

这就是梅森在他的回答中所要表达的。与其传递所有属性值,不如传递对象本身

procedure ViewComments(CommentButton: TCustomSpeedButton);

然后从所有按钮的事件处理程序中调用它:

procedure TForm1.CustomSpeedButton1Click(Sender: TObject);
begin
  ViewComments(CustomSpeedButton1);
end;

procedure TForm1.CustomSpeedButton2Click(Sender: TObject);
begin
  ViewComments(CustomSpeedButton2);
end;

没有字符串,没有case语句,没有查找。

那应该回答你的问题,但你可以做得更好。还记得我之前说过的关于Sender参数的内容吗?当有人单击第一个按钮时,Sender该处理程序的参数OnClick将是按钮,因此我们可以像这样重写第一个事件处理程序:

procedure TForm1.CustomSpeedButton1Click(Sender: TObject);
begin
  ViewComments(Sender as TCustomSpeedButton);
end;

您可以像这样重写第二个事件处理程序:

procedure TForm1.CustomSpeedButton2Click(Sender: TObject);
begin
  ViewComments(Sender as TCustomSpeedButton);
end;

唔。他们是一样的。拥有两个相同的功能是浪费,所以去掉一个并重命名另一个,这样听起来就不是特定于按钮的:

procedure TForm1.CommentButtonClick(Sender: TObject);
begin
  ViewComments(Sender as TCustomSpeedButton);
end;

然后将两个OnClick按钮的属性设置为引用该事件处理程序。您不能仅通过双击 Object Inspector 中的属性来做到这一点。您需要自己输入名称,从下拉列表中选择它,或者在运行时分配事件属性:

CustomSpeedButton1.OnClick := CommentButtonClick;
CustomSpeedButton2.OnClick := CommentButtonClick;

我还想鼓励您为控件使用更有意义的名称。这Label15尤其令人震惊。你怎么记得第十五个标签是表示正在编辑的标签?例如,称之为EditInProgressLabel

于 2009-08-27T06:24:52.797 回答
0

对您的代码稍作改动就可以解决问题:

procedure TForm1.CustomSpeedButton1Click(Sender: TObject);
var
  btn: TCustomSpeedButton;
begin
   btn := Sender as TCustomSpeedButton;
   Receiver := btn.Name; 
   ViewComments(btn.CommentTitle, btn.CommentText);
end;

并在编辑评论后:

procedure TForm1.StoreComments(comment: string);
var
  btn: TCustomSpeedButton;
begin
  btn := FindComponent(Receiver) as TCustomSpeedButton;
  btn.CommentText := comment;
end;

您还可以记住按钮本身,而不仅仅是它的名称。

于 2009-08-27T06:22:46.077 回答