?
Solved

Visual Basic -> Delphi

Posted on 2006-04-11
5
Medium Priority
?
293 Views
Last Modified: 2010-04-05
Can anyone help me to transform this Visual Basic code into Delphi:

Option Explicit

Private mSystem(1 To 13) As String
Private mFileNumber As Integer

Private Sub Command1_Click()
    mSystem(1) = "1"
    mSystem(2) = "x"
    mSystem(3) = "2"
    mSystem(4) = "x2"
    mSystem(5) = "1x"
    mSystem(6) = "1"
    mSystem(7) = "2"
    mSystem(8) = "2"
    mSystem(9) = "x"
    mSystem(10) = "1x"
    mSystem(11) = "2"
    mSystem(12) = "2"
    mSystem(13) = "1"
   
    mFileNumber = FreeFile
    Open "c:\system.txt" For Output As mFileNumber
    PrintSystem "", 1
    Close mFileNumber
    Shell "notepad.exe c:\system.txt", vbNormalFocus
End Sub

Private Sub PrintSystem(ByVal row As String, ByVal level As Integer)
    If level = 14 Then
        Print #mFileNumber, row
    Else
        Dim i As Integer
        For i = 1 To Len(mSystem(level))
            PrintSystem row & Mid(mSystem(level), i, 1), level + 1
        Next i
    End If
End Sub
0
Comment
Question by:strobe0
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
AmigoJack earned 1000 total points
ID: 16431979
uses ShellApi;

var
  mSystem: array[1..13] of String;
  mFileNumber: text;

procedure PrintSystem(row: String; level: Integer);
var
  i: integer;
begin
    If level = 14 Then Write(mFileNumber, row) else begin
        For i := 1 To Length(mSystem[level]) do PrintSystem(row+ copy(mSystem[level], i, 1), level + 1);
    End;
End;

procedure TForm1.Button1Click(Sender: TObject);
begin
    mSystem[1] := '1';
    mSystem[2] := 'x';
    mSystem[3] := '2';
    mSystem[4] := 'x2';
    mSystem[5] := '1x';
    mSystem[6] := '1';
    mSystem[7] := '2';
    mSystem[8] := '2';
    mSystem[9] := 'x';
    mSystem[10] := '1x';
    mSystem[11] := '2';
    mSystem[12] := '2';
    mSystem[13] := '1';
   
    AssignFile(mFileNumber, 'c:\system.txt');
    Rewrite(mFileNumber);
    PrintSystem('', 1);
    CloseFile(mFileNumber);

    ShellExecute(0, nil, 'notepad.exe', 'c:\system.txt', nil, SW_SHOWNORMAL);
End;


----------------------

that would be quite a 1:1 translation :) however, to make it more easy for you, simply start delphi, create a new application, add a TButton and then replace ALL OF THE prewritten code with the one below:

-----------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ShellApi,
  StdCtrls;

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

var
  Form1: TForm1;
  mSystem: array[1..13] of String;
  mFileNumber: text;

implementation

{$R *.DFM}


procedure PrintSystem(row: String; level: Integer);
var
  i: integer;
begin
    If level = 14 Then Write(mFileNumber, row) else begin
        For i := 1 To Length(mSystem[level]) do PrintSystem(row+ copy(mSystem[level], i, 1), level + 1);
    End;
End;

procedure TForm1.Button1Click(Sender: TObject);
begin
    mSystem[1] := '1';
    mSystem[2] := 'x';
    mSystem[3] := '2';
    mSystem[4] := 'x2';
    mSystem[5] := '1x';
    mSystem[6] := '1';
    mSystem[7] := '2';
    mSystem[8] := '2';
    mSystem[9] := 'x';
    mSystem[10] := '1x';
    mSystem[11] := '2';
    mSystem[12] := '2';
    mSystem[13] := '1';

    AssignFile(mFileNumber, 'c:\system.txt');
    Rewrite(mFileNumber);
    PrintSystem('', 1);
    CloseFile(mFileNumber);

    ShellExecute(0, nil, 'notepad.exe', 'c:\system.txt', nil, SW_SHOWNORMAL);

end;

end.
0
 

Author Comment

by:strobe0
ID: 16433720
Thanks a lot for your answer, AmigoJack!

Is there a way to save the output to memory instead of a file?
0
 
LVL 12

Expert Comment

by:AmigoJack
ID: 16439141
of course, but how do you mean that? for example: everything youre writing to the file could also be held by a single string.
0
 

Author Comment

by:strobe0
ID: 16439942
Can you give an example how I should code this if I want to save it in a string?
0
 
LVL 12

Expert Comment

by:AmigoJack
ID: 16440284
thats pretty simple :) i recommend you to check what i have changed compared to my other content of "unit1.pas" - i think this will help you most to append other changes in the same way and help to understand it:

(instead of writing it to a file, everything is put together into one string, which then will be displayed. for this you have to add a TMemo to the new project as well)

------------------------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ShellApi,
  StdCtrls;

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

  // this is a comment: variables declared here are accessable anywhere
  // so you can also define here the all-in-one string
var
  Form1: TForm1;
  mSystem: array[1..13] of String;
  sFullText: string;

implementation

{$R *.DFM}


procedure PrintSystem(row: String; level: Integer);
var
  i: integer;
begin
    If level = 14 Then sFullText:= sFullText+ row else begin
        For i := 1 To Length(mSystem[level]) do PrintSystem(row+ copy(mSystem[level], i, 1), level + 1);
    End;
End;

procedure TForm1.Button1Click(Sender: TObject);
begin
    mSystem[1] := '1';
    mSystem[2] := 'x';
    mSystem[3] := '2';
    mSystem[4] := 'x2';
    mSystem[5] := '1x';
    mSystem[6] := '1';
    mSystem[7] := '2';
    mSystem[8] := '2';
    mSystem[9] := 'x';
    mSystem[10] := '1x';
    mSystem[11] := '2';
    mSystem[12] := '2';
    mSystem[13] := '1';

    sFullText:= '';  // initialize it = means we set it to empty
    PrintSystem('', 1);

    // here goes our final string
    Memo1.text:= sFullText;

end;

end.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question