让程序用户只输入一个逗号怎么办

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

How can I make the user of my program enter only one comma

问题

我明白了。以下是您想要的翻译:

假设我们创建了一个程序来计算一个科目的平均分,并输入了`test1`的分数和`test2`的分数,然后显示出平均分。

我们需要进行两次编辑来输入`test1`和`test2`,以及一次编辑来显示平均分。正如我们所知,`test1`或`test2`的分数,例如,如果是`15,5`,意味着这个分数如果是小数,将包含一个逗号,这意味着它不能是一个包含两个逗号的小数,例如`15,15,1`,因为这不是一个有效的小数。

我应该如何让程序用户只输入一个逗号,以避免出现错误消息呢?

如果我的解释不清楚,我为此道歉。我可以写哪段代码使用户只能输入一个逗号,以及我应该把它写在哪里?比如,在编辑框本身,或者在`OnChange`或`OnKeyPress`属性中,或者确切地说在哪里?

我尝试在`OnChange`事件中写了这段代码:

```pascal
if not ( Key in ['0'..'9'] + [',',#8] ) then Key := #0;

以便用户只能输入数字和逗号,#8用于删除已输入的内容。

但是尽管如此,他仍然可以输入包含多个逗号的数字,这意味着他仍然可以写入15,5,19,9,9,尽管我希望他只写一个有效的包含一个逗号的小数,因为一个小数不能包含多个逗号。

我认为解决办法可能是将条件*1应用于逗号,但我不知道如何实现这个想法。我的意思是逗号只能出现一次。

您认为呢?


<details>
<summary>英文:</summary>

Suppose we created a program to calculate the average of a subject and entered the `test1` point and the `test2` point, and then the average is displayed.

We need two edits to enter the `test1` and `test2`, and one edit to show the average. As we know, that the `test1` or `test2` point, for example, if it is `15,5` it means that the point, if it is decimal, will consist of a comma, meaning it cannot be a decimal number that contains two commas, for example `15,15,1`, as this is not a decimal number.

How can I make the user of my program enter only one comma, that is, how do I prevent him from entering several commas in order for error messages not to appear?

I apologize if my explanation was not clear. What is the code that I can write to make users enter only one comma, and where do I write it? For example, in the edit itself, or in the properties `OnChange` or `OnKeyPress`, or where exactly?

I tried writing this code in the `OnChange` event:

if not ( Key in ['0'..'9'] + [',',#8] ) then Key := #0;


to make the user write only the numbers and the comma, and let `#8` to erase what was written.

But despite this, he can enter a number that contains more than one comma, which means that he can still write, for example, `15,5,1` or `9,9,9`, although I want him to write only a valid decimal with one comma, because a decimal number can&#39;t contain more than one comma.

I think that the solution is, for example, to put the condition &#39;*&#39;*1, but I don&#39;t know how to employ the idea. I mean, the comma is multiplied once.

Or, what do you think?

</details>


# 答案1
**得分**: 1

我建议在实际进行计算时使用 [`TryStrToFloat()`](https://docwiki.embarcadero.com/Libraries/en/System.SysUtils.TryStrToFloat) 函数来验证用户的输入,该函数位于 `SysUtils` 单元中。这样可以让你进行自己的错误处理等。

例如:

```delphi
uses
  ..., SysUtils;

procedure TMyForm.GetAverageButtonClick(Sender: TObject);
var
  point1, point2: Double;
begin
  if not TryStrToFloat(Edit1.Text, point1) then
  begin
    ShowMessage('Point1 is not valid');
    Edit1.SetFocus;
    Exit;
  end;

  if not TryStrToFloat(Edit2.Text, point2) then
  begin
    ShowMessage('Point2 is not valid');
    Edit2.SetFocus;
    Exit;
  end;

  Edit3.Text := FloatToStr((point1 + point2) / 2.0);
end;

不要浪费时间和精力在每次按键时进行验证 - 特别是因为你并没有完全验证输入,比如你没有阻止用户输入超过一个逗号等。

英文:

I would suggest using TryStrToFloat() in the SysUtils unit to validate the user's input at the time you are actually ready to perform the calculation. That will allow you to do your own error handling, etc.

For example:

uses
  ..., SysUtils;

procedure TMyForm.GetAverageButtonClick(Sender: TObject);
var
  point1, point2: Double;
begin
  if not TryStrToFloat(Edit1.Text, point1) then
  begin
    ShowMessage(&#39;Point1 is not valid&#39;);
    Edit1.SetFocus;
    Exit;
  end;

  if not TryStrToFloat(Edit2.Text, point2) then
  begin
    ShowMessage(&#39;Point2 is not valid&#39;);
    Edit2.SetFocus;
    Exit;
  end;

  Edit3.Text := FloatToStr((point1 + point2) / 2.0);
end;

Don't bother wasting your time/effort validating every individual key press - especially since you are not fully validating the input anyway, ie you are not preventing the user from entering more than 1 comma, etc.

答案2

得分: 0

你可以使用 Pos 函数来检查逗号是否已存在:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if (Key = '.') and ('.' <> DecimalSeparator) then
    Key := DecimalSeparator;
  if not (Key in ['0'..'9', DecimalSeparator, #8]) then
  begin
    Key := #0;
    Beep;
    Exit;
  end;
  if (Key = DecimalSeparator)
    and (Pos(DecimalSeparator, TEdit(Sender).Text) > 0) then
  begin
    Key := #0;
    Beep;
  end;
end;

此外,如果用户使用数字键盘,它也接受点字符,并在需要时将其转换为十进制分隔符。

英文:

You can use Pos function to check if a comma already exists:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if (Key = &#39;.&#39;) and (&#39;.&#39; &lt;&gt; DecimalSeparator) then
    Key := DecimalSeparator;
  if not (Key in [&#39;0&#39;..&#39;9&#39;, DecimalSeparator, #8]) then
  begin
    Key := #0;
    Beep;
    Exit;
  end;
  if (Key = DecimalSeparator)
    and (Pos(DecimalSeparator, TEdit(Sender).Text) &gt; 0) then
  begin
    Key := #0;
    Beep;
  end;
end;

Aditionally, it accepts dot char, if the user uses number pad, and converts it to decimal separator if needed.

huangapple
  • 本文由 发表于 2023年4月11日 05:26:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980866.html
匿名

发表评论

匿名网友

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

确定