|
-
Apr 11th, 2012, 07:17 AM
#1
Thread Starter
Member
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.
-
Apr 11th, 2012, 07:37 AM
#2
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:
-
Apr 11th, 2012, 07:38 AM
#3
Addicted Member
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
-
Apr 11th, 2012, 08:48 AM
#4
Thread Starter
Member
Re: Translating form Delphi to VB6
 Originally Posted by yogiyang
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;
-
Apr 11th, 2012, 09:10 AM
#5
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.
-
Apr 11th, 2012, 09:11 AM
#6
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!
-
Apr 11th, 2012, 04:48 PM
#7
Re: Translating from Delphi to VB6
 Originally Posted by jalexm
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|