TEdit.Text不会随用户输入的文本更新。

huangapple go评论55阅读模式
英文:

TEdit.Text won't update with user entered text

问题

我写了一个程序,其中使用了一个 TEditEdit1)和一个 TButtonButton1)。当按下 Button1 时,会将 Edit1.Text 的内容与一个字符串常量进行比较。如果它们相同,一些其他按钮将被激活。Edit1Button1 直接放在 Form1 上。

这一切都有效。

然后,我重新排列了 Form1(窗口的外观)。我添加了一些面板(Panels)和网格面板(GridPanels)。Button1Edit1Form1 移动到了 Form1->Panel5->GridPanel2

从那时起,问题就出现了。

当我读取 Form1.Edit1.Text 时,只有原始文本显示出来。当程序运行并且我在 Edit1 中输入内容时,输入的文本是可见的。但是 Form1.Edit1.Text 的内容/读取并没有改变。

是否有任何设置可以阻止将输入的数据放入 Edit.Text 中?

无论我输入什么,Edit1.Text 仍然保持在代码(对象检查器)中的原始内容。

我已经几次将项目移动到面板上,从未出现过这种行为。

这是代码,移动 Button1Edit1 到面板之后没有更改的代码:

procedure TForm1.Button1Click(Sender: TObject);
begin
  // 用于调试查看内容的showmessage
  showmessage(Form1.Edit1.Text);

  if Form1.Edit1.Text = '1234' then
  begin
    // 启用一个按钮和一个编辑框
    Form1.Button_Open.Enabled := True;
    Form1.Edit_Id.Enabled := True;
  end
  else
  begin
    showmessage('错误的输入');
    // 可以添加更多代码
  end;
end;

我尝试查找阻止数据输入到文本字段更新 Edit1.Text 的设置,但我没有找到任何内容。

  • 我删除了 Edit1Button1,然后以相同名称的方式放置在它们以前的位置(GridPanel),但仍然不起作用。

  • 我删除了 Edit1Button1,然后以不同名称的方式放置在它们以前的位置(GridPanel),但仍然不起作用。

  • 我删除了 Edit1Button1,然后直接放在 Form1 上,但它们仍然显示相同的行为。

我在文本字段中输入的任何内容都没有出现在 Edit1.Text 中。它仍然是在代码中输入的原始文本。

英文:

I wrote a program where a TEdit (Edit1) and TButton (Button1) are used. When Button1 is pressed, the content of Edit1.Text is compared to a string constant. If both are identical, some other buttons will be set active. Edit1 and Button1 were directly placed onto Form1.

This all worked.

Then I rearranged Form1 (appearance of the window). I added some Panels and GridPanels. Button1 and Edit1 moved from Form1 to Form1->Panel5->GridPanel2.

Since then, things do not work anymore.

When I read Form1.Edit1.Text, only the original text shows up. When the program runs and I type something into Edit1, the typed text is visible. But the reading/content
of Form1.Edit1.Text does not change.

Is there any setting that stops that the data entered into the edit field is put into Edit.Text?

Whatever I type, Edit1.Text remains the original content that is in the code (Object Inspector).

I already did it a couple of times that I moved items onto panels and never had such a behavior.

Here is the code, that was not changed after moving Button1 and Edit1 onto panels.

procedure TForm1.Button1Click(Sender: TObject);
begin
  //showmessage for debugging to see what the content is
  showmessage(Form1.Edit1.Text);

  if Form1.Edit1.Text = '1234' then
  begin
    // enable a button and an edit
    Form1.Button_Open.Enabled := True;
    Form1.Edit_Id.Enabled := True;
  end
  else
  begin
    showmessage('Wrong input');
   // room for more code
  end;
end;

I tried to find settings that stop data entered into the text field from updating Edit1.Text, but I did not find anything.

  • I deleted Edit1 and Button1 and entered new ones with the same names at the places they were before (GridPanel), but that still did not work.

  • I deleted Edit1 and Button1 and entered new ones with different names at the places they were before (GridPanel), but still did not work.

  • I deleted Edit1 and Button1 and entered new ones directly on Form1, but they also showed the same behavior.

Nothing that I type into the text field finds its way into Edit1.Text. It remains the original text that has been typed into the code.

答案1

得分: 1

我找到了原因,通过查看主程序/项目文件:

Form1 实际上被调用了两次。删除第二次调用后,程序开始按照应该的方式工作。

英文:

I found the reason, by looking into the main program/project file:

Form1 was actually called twice. By deleting the second call, the program started to work as it should.

program Project1;

uses
  Vcl.Forms,
  Vcl.Themes,
  Vcl.Styles,
  EMR.pas {Form1};

{$R *.res}
 
begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);  
  Application.CreateForm(TForm1, Form1); //called twice!!  
  Application.Run;
end.

huangapple
  • 本文由 发表于 2023年2月16日 17:27:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定