Convert VB's code to Delphi

Hello,

Please help to convert the following VB code to Delphi:

----------------------------------
Private Sub CommandButton1_Click()
       
    Dim MyString
    Dim bError As Boolean
    bError = True
    Dim fs
    Dim strFile As String
    Set fs = CreateObject("Scripting.FileSystemObject")
    strFile = UCase(Left(Text1.Text, 1)) & ":\VIDEO_TS\VTS_01_1.VOB"
    If fs.fileexists(strFile) = False Then
        strFile = UCase(Left(Text1.Text, 1)) & ":\VIDEO_TS\VIDEO_TS.VOB"
        If fs.fileexists(strFile) = False Then
            GoTo Here
        End If
    End If
   
    On Local Error GoTo ErrorHandler
    While bError
        bError = False
        Open strFile For Input Shared As #1
        Input #1, MyString
        Close #1
    Wend
    'MsgBox "Unlocked"

Here:
   
    Combo1.SetFocus
    Combo1.ListIndex = -1
    Combo2.ListIndex = -1
    Text2.Text = ""
    Text3.Text = ""

    Exit Sub

ErrorHandler:

    'bError = True
    'If MsgBox("Your DVD drive may be locked.", vbExclamation Or vbYesNo) = vbNo Then
    '    Close #1
    '    Exit Sub
    'End If
    'Resume Next
    If Err.Number = "52" Then
        MsgBox "Your DVD drive may be locked.", vbExclamation
        Close #1
        Text1.Text = ""
        Exit Sub
    End If
   
    Resume Next
   
End Sub
-------------------------------------

Thanks.

rng
rngAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

esoftbgCommented:
procedure TForm1.Button1Click(Sender: TObject);
var
//  bError:        Boolean;
  MyString:      TStringList;
  strFile:       string;
begin
  strFile := UpperCase(Edit1.Text[1]) + ':\VIDEO_TS\VTS_01_1.VOB';
  if not FileExists(strFile) then
  begin
    strFile := UpperCase(Edit1.Text[1]) + ':\VIDEO_TS\VIDEO_TS.VOB';
    if not FileExists(strFile) then
    begin
      ComboBox1.SetFocus;
      ComboBox1.ItemIndex := -1;
      ComboBox2.ItemIndex := -1;
      Exit;
    end;
  end;
  try
    MyString := TStringList.Create;
    try
      MyString.SaveToFile(strFile);
    finally
      MyString.Free;
    end;
  except
//    bError := True;
    on E : exception do
      ShowMessage('Your DVD drive is may be locked. '#10#13 + E.message);
  end;
end;
0
esoftbgCommented:
procedure TForm1.Button1Click(Sender: TObject);
var
  MyString:      TStringList;
  strFile:       string;
begin
  strFile := UpperCase(Edit1.Text[1]) + ':\VIDEO_TS\VTS_01_1.VOB';
  if not FileExists(strFile) then
  begin
    strFile := UpperCase(Edit1.Text[1]) + ':\VIDEO_TS\VIDEO_TS.VOB';
    if not FileExists(strFile) then
    begin
      ComboBox1.SetFocus;
      ComboBox1.ItemIndex := -1;
      ComboBox2.ItemIndex := -1;
      Edit2.Text := '';
      Edit3.Text := '';
      Exit;
    end;
  end;
  try
    MyString := TStringList.Create;
    try
      MyString.SaveToFile(strFile);
    finally
      MyString.Free;
    end;
  except
    on E : exception do
      ShowMessage('Your DVD drive is may be locked. '#10#13 + E.message);
  end;
end;
0
rngAuthor Commented:
Thanks esoftbg.
What is the difference between two?

rng
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

esoftbgCommented:
in the first I forgot:
      Edit2.Text := '';
      Edit3.Text := '';
0
rngAuthor Commented:
In the VB codes the following few lines are trying to read the file, but your code is trying to save.

VB
------------------
        Open strFile For Input Shared As #1
        Input #1, MyString
        Close #1

Delphi
------------------
        MyString.SaveToFile(strFile);


Also, do you know how to write this line in delph ??
------------------
        If Err.Number = "52" Then


Thanks.

rng
0
esoftbgCommented:
Excuse me, it's my mistake:
instead of:
        MyString.SaveToFile(strFile);
let to use:
        MyString.LoadFromFile(strFile); // Read from the file as from Text file (the whole content of the File)
//................................................................................................................................................

in the code construction:

try
//........ Dangerous code which could raise exception (оccurs an error)
except
  // the code here will be executed only if Dangerous code above raises an exception (оccurs an error)
end;

//................................................................................................................................................

procedure TForm1.Button1Click(Sender: TObject);
var
  MyString:      TStringList;
  strFile:       string;
begin
  strFile := UpperCase(Edit1.Text[1]) + ':\VIDEO_TS\VTS_01_1.VOB';
  if not FileExists(strFile) then
  begin
    strFile := UpperCase(Edit1.Text[1]) + ':\VIDEO_TS\VIDEO_TS.VOB';
    if not FileExists(strFile) then
    begin
      ComboBox1.SetFocus;
      ComboBox1.ItemIndex := -1;
      ComboBox2.ItemIndex := -1;
      Edit2.Text := '';
      Edit3.Text := '';
      Exit;
    end;
  end;
  try
    MyString := TStringList.Create;
    try
      MyString.LoadFromFile(strFile);
    finally
      MyString.Free;
    end;
  except
    on E : exception do
    begin
      ShowMessage('Your DVD drive is may be locked. '#10#13 + E.message);
    end;
  end;
end;
0
esoftbgCommented:
procedure TForm1.Button2Click(Sender: TObject);
var
  f:             file of Char;
  Ch:            Char;
  strFile:       string;
begin
  strFile := UpperCase(Edit1.Text[1]) + ':\VIDEO_TS\VTS_01_1.VOB';
  if not FileExists(strFile) then
  begin
    strFile := UpperCase(Edit1.Text[1]) + ':\VIDEO_TS\VIDEO_TS.VOB';
    if not FileExists(strFile) then
    begin
      ComboBox1.SetFocus;
      ComboBox1.ItemIndex := -1;
      ComboBox2.ItemIndex := -1;
      Edit2.Text := '';
      Edit3.Text := '';
      Exit;
    end;
  end;
  try
    AssignFile(f, strFile);
    FileMode := fmOpenWrite;
    Reset(f);
    try
      Read(f, Ch);
    finally
      CloseFile(f);
    end;
  except
    on E : exception do
    begin
      ShowMessage('Your DVD drive is may be locked. '#10#13 + E.message);
    end;
  end;
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
esoftbgCommented:
> Also, do you know how to write this line in delph ??
> ------------------
>         If Err.Number = "52" Then

Sorry, I don't know what is that > Err.Number in VB

In Delphi   E.message shows some info about the Error ....
0
rngAuthor Commented:
esoftbg:

When I compile the program, it report this warning:

------------------------
[Warning] Main.pas(849): Unsafe type 'f: file of Char'
------------------------

How to get rid of it ?

rng
0
esoftbgCommented:
I am using Delphi 7 Enterprise and I have not warnings at all ....

I see a mistake:

    FileMode := fmOpenWrite;
must be:
    FileMode := fmOpenRead;

the construction

    Reset(f);
    try
      Read(f, Ch);
    finally
      CloseFile(f);
    end;

guarantees that the file will be closed no matter of any exception (error)
0
rngAuthor Commented:
Ok, thanks!

rng
0
esoftbgCommented:
You are welcome !
Thanks for the points !
emil
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.