2012-11-26 55 views
-1

你好我得到錯誤E2197:[DCC錯誤] proj1.pas(34):直接從程序/功能獲取TAdvEdit.Text

unit proj1; 

interface 

uses 
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, AdvEdit; 

type 
    TForm1 = class(TForm) 
    AdvEdit1: TAdvEdit; 
    Button1: TButton; 
    procedure Button1Click(Sender: TObject); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 

procedure SetEditText(const instr: string; out outstr: string); 
begin 
    outstr := instr; 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
    SetEditText('Pippo', AdvEdit1.Text); 
end; 

end. 

當然:E2197常量對象不能作爲變量參數被傳遞,我可以解決寫作:

procedure TForm1.Button1Click(Sender: TObject); 
var sText: string 
begin 
    SetEditText('Pippo', sText); 
    AdvEdit1.Text := sText; 
end; 

但是,當我有很多AdvEdit,那麼它很難。那麼我問,有可能解決問題,在某些模式中直接給出TAdvEdit.Text作爲參數在我的程序中? 非常感謝。

+1

爲什麼不使用'procedure SetEditText(const Text:string; Edit:TAdvEdit);' – TLama

+0

@TLama爲什麼你需要這樣的函數?它會永遠是值得的嗎? –

+1

@David,當我想,我不知道,通過輸入文本長度或其他設置字體顏色。我知道用這種方式將一個文本分配給一個控件是毫無意義的。 – TLama

回答

4

我推測Text是一個屬性。並且您無法將屬性傳遞給varout參數。您只能將變量傳遞給這些類型的參數。

您需要找到一種不同的方式來編寫代碼。你已經想出了一個這樣的想法,但對我來說似乎是不必要的複雜。我看不到任何比這更簡單的東西:

AdvEdit1.Text := 'Pippo'; 

怎麼會有比這更簡單的代碼?您需要至少指定以下內容:

  • 目標控件。
  • 我們正在處理Text屬性。
  • 我們正在分配的事實。
  • 新值。

上面的代碼沒有那麼多,

+0

大衛你好,謝謝你的回答。是的我明白;但問題是由其他人產生的。我的表單有近15個TAdvEdit控件,我從一個過程(返回字符串和整數值)獲取值。好的,你告訴過。但對我來說,奇怪的是,需要寫15行或更多的行,只是爲了給它指定一個聲明15變量的值。所以,只爲了這個,我問是否有其他解決方案更「乾淨」。再次感謝。 –

+1

我不明白你。你沒有提出問題的複雜性。爲了給屬性賦值,沒有比我的答案中的代碼更簡單的了。 –