Results 1 to 7 of 7

Thread: Translating from Delphi to VB6

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    35

    Translating from Delphi to VB6

    I'm translating an app from Delphi to VB6.
    Form1 sends a message to Form2 using SendMessage and WM_APP.
    Form2 receives the message and does something.
    It's very easy to do it in Delphi, but I have no idea how to do it in VB6.
    Please, can you help me?
    Last edited by jalexm; Apr 11th, 2012 at 08:51 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Translating form Delphi to VB6

    Are both forms in the same project?

    If so the simplest solution is to make a Sub (or Function) in Form2 that Form1 calls. All you need to do is declare it as Public, eg:
    Code:
    Public Sub DoSomething
    ..and when you call it, specify the parent:
    Code:
    Form2.DoSomething

  3. #3
    Addicted Member
    Join Date
    Mar 2007
    Location
    India
    Posts
    227

    Re: Translating form Delphi to VB6

    In VB6 on has to first declare Win APIs like SendMessage, etc. and then use these windows APIs.

    Can you say as to what exactly you are trying to achieve here and what is the Delhi code that you want to translate?

    HTH

    Yogi Yang

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    35

    Re: Translating form Delphi to VB6

    Quote Originally Posted by yogiyang View Post
    In VB6 on has to first declare Win APIs like SendMessage, etc. and then use these windows APIs.
    Can you say as to what exactly you are trying to achieve here and what is the Delhi code that you want to translate?
    Solution presented by si_the_geek above is good if we have few forms.
    The app has 2 forms now, but maybe it will have many more in the future.
    This is the reason I want to keep SendMessage, etc.

    The Delphi code you asked is very similar to this:
    Code:
    In Utilities unit:
    
    const
       MsgToAllForms = WM_APP + 1;
       msgUpdateInfo1 = 1;
       msgUpdateInfo2 = 2;
    
    procedure SendMsgToAllForms(WParam: Integer);
    var
       i: Integer;
    begin
       for i := 0 to Pred(Screen.CustomFormCount) do
          SendMessage(Screen.CustomForms[i].Handle, MsgToAllForms, WParam, 0);
    end;
    
    ====================
    
    In Form1:
    
         if Condition1 then
            SendMsgToAllForms(msgUpdateInfo1);
         ...
         if Condition2 then
            SendMsgToAllForms(msgUpdateInfo2);
         ...
    
    ====================
    
    In Form2:
    
         unit Form2;
    
         interface
    
         uses
            Utilities;
    
         type
            TForm2 = class(TForm)
               ...
               procedure MsgReceiver(var Msg: TMessage); message MsgToAllForms;
               ...
            private
               ...
            public
               ...
            end;
    
         implementation
    
         procedure TForm2.MsgReceiver(var Msg: TMessage);
         begin
            case Msg.WParam of
               msgUpdateInfo1:
                  Form2.UpdateInfo1;
               msgUpdateInfo2:
                  Form2.UpdateInfo2;
            end;
         end;

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Translating from Delphi to VB6

    A couple ways to skin this cat....

    1. Using SendMessage requires subclassing to process the sent message. I don't think you want to go there if there are easier solutions. Subclassing is powerful, but dangerous if not experienced with it.

    2. You can build a public sub/function as si_the_geek described and use a the forms collection in a loop to send the message
    Code:
    ' in your forms
    Public Sub MsgReceiver(Msg As Long)
       ' process the msg
    End Sub
    
    ' now from anywhere in your code, you can send same message to all forms
        Dim f As Long
        For f = 0 To Forms.Count - 1
            CallByName Forms.Item(f), "MsgReceiver", VbMethod, 123
        Next
    There are other methods one can use also. For example using a public class that is implemented by all forms
    Code:
    ' in a new class named: cMsgReceiver
    Public Event MsgReceiver(theMsg As Long)
    
    Public Sub SendMessage(Msg As Long)
        RaiseEvent MsgReceiver(Msg)
    End Sub
    
    ' in a module
    Public g_MsgReceiver As cMsgReceiver ' shared by the forms
    
    ' now in every form that should receive message, in declarations section
    Dim WithEvents myMsgReceiver As cMsgReceiver
    
    Private Sub Form_Load()
        If g_MsgReceiver Is Nothing Then Set g_MsgReceiver = New cMsgReceiver
        Set myMsgReceiver = g_MsgReceiver
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        Set myMsgReceiver = Nothing
    End Sub
    
    Private Sub myMsgReceiver_MsgReceiver(theMsg As Long)
        Debug.Print "received msg: "; theMsg; " in form: "; Me.Name
    End Sub
    
    ' now to send the message to all forms
        Call g_MsgReceiver.SendMessage(123)
    Edited: The last option above is probably the most flexible because only forms that need to process your custom messages need to include the sample code. With the forms collection looping solution, it applies to 100% of your forms and in that solution, you'll need to include a public sub whether form will process messages or not. The last solution is more code, but more flexible. In fact, it is not limited to just forms; can be used in other classes & usercontrols too -- your choice.
    Last edited by LaVolpe; Apr 11th, 2012 at 10:10 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Translating from Delphi to VB6

    The simplest way to do that is to extend the method I showed, just looping thru the form objects, eg:
    Code:
    Dim frm as Form
      For Each frm In Forms
        frm.DoSomething
      Next
    edit: beaten to it, but at least I showed a different style!

  7. #7
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Translating from Delphi to VB6

    Quote Originally Posted by jalexm View Post
    I'm translating an app from Delphi to VB6.
    Form1 sends a message to Form2 using SendMessage and WM_APP.
    Form2 receives the message and does something.
    It's very easy to do it in Delphi, but I have no idea how to do it in VB6.
    Please, can you help me?
    Sounds like a Poster Child for why you might as well rewrite from scratch.

    How many times have you written a program where code for one Form has to "broadcast" something to every other Form anyway?

    I'd hate to see what the meat of the application is written like.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width