Ntsito Maelane
Ntsito Maelane

Reputation: 11

How do this C# textbox events in Delphi

I would like to convert the following C# code to Delphi. I am not getting it right.

txtManual = new System.Windows.Forms.TextBox[] 
                { txtMNoteValue1, txtMNoteValue2, txtMNoteValue3, txtMNoteValue4,
                  txtMNoteValue5, txtMNoteValue6, txtMNoteValue7,
                  txtMCoinValue1, txtMCoinValue2, txtMCoinValue3, txtMCoinValue4,
                  txtMCoinValue5, txtMCoinValue6, txtMCoinValue7, txtMCoinValue8,
                  txtMCreditValue, txtMCheckValue, txtMCouponValue, txtMOtherValue };
// textbox event
for (i = 0; i < txtManual.Length; i++)
{
    txtManual[i].KeyUp += new KeyEventHandler(txtManual_Keyup);
    txtManual[i].KeyPress += new KeyPressEventHandler(txtManual_KeyPress);
    txtManual[i].LostFocus += new EventHandler(txtManual_LostFocus);
}

Upvotes: -3

Views: 102

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598434

A literal translation would look something like the following:

var
  txtManual: array of TEdit;
  i: Integer;
begin
  txtManual := [
    txtMNoteValue1, txtMNoteValue2, txtMNoteValue3, txtMNoteValue4,
    txtMNoteValue5, txtMNoteValue6, txtMNoteValue7,
    txtMCoinValue1, txtMCoinValue2, txtMCoinValue3, txtMCoinValue4,
    txtMCoinValue5, txtMCoinValue6, txtMCoinValue7, txtMCoinValue8,
    txtMCreditValue, txtMCheckValue, txtMCouponValue, txtMOtherValue
  ];

  // textbox event
  for i := Low(txtManual) to High(txtManual) do
  begin
    txtManual[i].OnKeyUp := txtManual_Keyup;
    txtManual[i].OnKeyPress := txtManual_KeyPress;
    txtManual[i].OnExit := txtManual_LostFocus;
  end;

  ...
end;

Where the event handlers would look something like this:

procedure TMyForm.txtManual_Keyup(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  ...
end;

proceedue TMyForm.txtManual_KeyPress(Sender: TObject; var Key: Char);
begin
  ...
end;

procedure TMyForm.txtManual_LostFocus(Sender: TObject);
begin
  ...
end;

Upvotes: 2

Related Questions