C#主 |
private void ShowForm2Info() { Form f2.Show(); MessageBox.Show(f2.MyName); } |
C#从 |
internal string MyName = "A Form2 Object"; |
VB主 |
Private Sub ShowForm2Info() Dim f2 As New Form2 f2.Show() MsgBox(f2.MyName) End Sub |
VB从 |
Friend MyName As String = "A Form2 Object" |
C#主 |
private void ShowForm2() { Form f2.Show(); } internal string MyName = "A Form1 Object"; |
C#从 |
private Form public Form2(Form1 _f1) { InitializeComponent(); f1 = _f1; } private void ShowForm1Info() { MessageBox.Show(f1.MyName); } |
VB主 |
Private Sub ShowForm2() Dim f2 As New Form2(Me) f2.Show() End Sub Friend MyName As String = "A Form1 Object" |
VB从 |
Private f1 As Form1 Public Sub New(ByVal _f1 As Form1) MyBase.New() InitializeComponent() f1 = _f1 End Sub Private Sub ShowForm1Info() MsgBox(f1.MyName) End Sub |
C#主 |
private void ShowForm2() { Form f2.Show(); } internal static string MyName = "A Form1 Object"; |
C#从 |
private void ShowForm1Info() { MessageBox.Show(Form1.MyName); > } |
VB主 |
Private Sub ShowForm2() Dim f2 As New Form2() f2.Show() End Sub Friend Shared MyName As String = "A Form1 Object" |
VB从 |
Private Sub ShowForm1Info() MsgBox(Form1.MyName) End Sub |
C#主 |
private void ShowForm2() { Form f2.Show(); f2.SendMessage += new Form2.DelegateOfSendMessage(ShowMessage); }
private void ShowMessage(string str) { MessageBox.Show(str); } |
C#从 |
public delegate void DelegateOfSendMessage(string str); public event DelegateOfSendMessage SendMessage; private void Send() { SendMessage("A Message From Form2"); } |
VB主 |
Private Sub ShowForm2() Dim f2 As New Form2 f2.Show() AddHandler f2.SendMessage, AddressOf ShowMessage End Sub Private Sub ShowMessage(ByVal str As String) MsgBox(str) End Sub |
VB从 |
Public Delegate Sub DelegateOfSendMessage(ByVal str As String) Public Event SendMessage As DelegateOfSendMessage
Private Sub Send() RaiseEvent SendMessage("A Message From Form2") End Sub |
6、习惯了VB6中全局变量的朋友可能觉得.NET中不支持全局变量对于窗体间的交互很不方便,那么再请大家参考这篇文章:《.NET窗体之间的交互》(http://blog.csdn.net/warsgrobe/archive/2005/07/29/438013.aspx),里面叙述了用VB.NET模拟全局变量的方法。