?
Solved

Convert VB's code to Delphi

Posted on 2004-11-02
12
Medium Priority
?
257 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:rng
  • 8
  • 4
12 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 12480276
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
 
LVL 12

Expert Comment

by:esoftbg
ID: 12480614
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
 

Author Comment

by:rng
ID: 12480639
Thanks esoftbg.
What is the difference between two?

rng
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:esoftbg
ID: 12480687
in the first I forgot:
      Edit2.Text := '';
      Edit3.Text := '';
0
 

Author Comment

by:rng
ID: 12481794
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
 
LVL 12

Expert Comment

by:esoftbg
ID: 12482583
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
 
LVL 12

Accepted Solution

by:
esoftbg earned 1600 total points
ID: 12483203
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
 
LVL 12

Expert Comment

by:esoftbg
ID: 12487136
> 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
 

Author Comment

by:rng
ID: 12489307
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
 
LVL 12

Expert Comment

by:esoftbg
ID: 12490785
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
 

Author Comment

by:rng
ID: 12490986
Ok, thanks!

rng
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12491017
You are welcome !
Thanks for the points !
emil
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

850 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