public partial class Form1 : Form
VB
Public Class Form1
public partial class Form1 : Form
{
private Form2 f2 = new Form2();
public void MyArbitraryMethod1()
{
Form2 f3 = new Form2();
// I CAN ACCESS f2 AND f3
}
public void MyArbitraryMethod2()
{
// I CAN ACCESS f2
}
}
Public Class Form1
Private f2 As New Form2()
Public Sub MyArbitraryMethod1()
Dim f3 As New Form2()
' I CAN ACCESS f2 AND f3
End Sub
Public Sub MyArbitraryMethod2()
' I CAN ACCESS f2
End Sub
End Class
public partial class Form2 : Form
{
private string _form1Data;
public Form2(string form1Data)
{
InitializeComponent();
this._form1Data = form1Data;
}
public void SomeFunction1()
{
}
public void SomeFunction2(string form1Data)
{
}
}
Public Class Form2
Private _form1Data As String
Public Sub New(ByVal form1Data As String)
InitializeComponent()
Me._form1Data = form1Data
End Sub
Public Sub SomeFunction1()
End Sub
Public Sub SomeFunction2(ByVal form1Data As String)
End Sub
End Class
public partial class Form1 : Form
{
private string _dataToPass = "Hello World!";
public void MyArbitraryMethod1()
{
Form2 f3 = new Form2();
f3.SomeFunction2(this._dataToPass);
f3.Show();
}
private void button1_Click(object sender, System.EventArgs e)
{
MyArbitraryMethod1();
}
}
Public Class Form1
Private _dataToPass As String = "Hello World!"
Public Sub MyArbitraryMethod1()
Dim f3 As New Form2()
f3.SomeFunction2(Me._dataToPass)
f3.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MyArbitraryMethod1()
End Sub
End Class
public void SomeFunction2(string form1Data)
{
this.label1.Text = form1Data;
}
Public Sub SomeFunction2(ByVal form1Data As String)
Me.Label1.Text = form1Data
End Sub
using System.Windows.Forms;
namespace WindowsFormsApplication13
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string StringToDisplay
{
get { return this.label1.Text; }
set { this.label1.Text = value; }
}
}
}
using System.Windows.Forms;
namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
private string _dataToPass = "Hello World!";
public void MyArbitraryMethod1()
{
Form2 f3 = new Form2();
f3.StringToDisplay = this._dataToPass;
f3.Show();
}
private void button1_Click(object sender, System.EventArgs e)
{
MyArbitraryMethod1();
}
}
}
Public Class Form2
Public Sub New()
InitializeComponent()
End Sub
Public Property StringToDisplay() As String
Get
Return Me.Label1.Text
End Get
Set(ByVal value As String)
Me.Label1.Text = value
End Set
End Property
End Class
Public Class Form1
Private _dataToPass As String = "Hello World!"
Public Sub MyArbitraryMethod1()
Dim f3 As New Form2()
f3.StringToDisplay = Me._dataToPass
f3.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.MyArbitraryMethod1()
End Sub
End Class
public partial class Form1 : Form
{
private string _dataToPass = "Hello World!";
public void MyArbitraryMethod1()
{
Form2 f3 = new Form2(this);
f3.Show();
}
public string StringToDisplay
{
get { return this._dataToPass; }
}
private void button1_Click(object sender, System.EventArgs e)
{
MyArbitraryMethod1();
}
}
public partial class Form2 : Form
{
public Form2(Form1 form1Instance)
{
InitializeComponent();
this.label1.Text = form1Instance.StringToDisplay;
}
}
Public Class Form1
Private _dataToPass As String = "Hello World!"
Public Sub MyArbitraryMethod1()
Dim f3 As New Form2(Me)
f3.Show()
End Sub
Public ReadOnly Property StringToDisplay() As String
Get
Return Me._dataToPass
End Get
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MyArbitraryMethod1()
End Sub
End Class
Public Class Form2
Public Sub New(ByRef form1Instance As Form1)
InitializeComponent()
Me.Label1.Text = form1Instance.StringToDisplay
End Sub
End Class
public partial class Form1 : Form
{
public static string _dataToPass = "Hello World!";
public void MyArbitraryMethod1()
{
Form2 f3 = new Form2();
f3.Show();
}
private void button1_Click(object sender, System.EventArgs e)
{
MyArbitraryMethod1();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.label1.Text = Form1._dataToPass;
}
}
Public Class Form1
Public Shared _dataToPass As String = "Hello World!"
Public Sub MyArbitraryMethod1()
Dim f3 As New Form2()
f3.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MyArbitraryMethod1()
End Sub
End Class
Public Class Form2
Public Sub New()
InitializeComponent()
Me.Label1.Text = Form1._dataToPass
End Sub
End Class
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
Author
Commented:The intent of the article was to be more of a "beginner's introduction", and it was born out of countless questions I've seen asked on the site regarding sending data from one form to another. Certainly MVC is a viable option, but it is not an intuitive designer pattern for beginners. Aside from that, some smaller projects might not benefit from the overhead of implementing the MVC pattern. Moreover, you could also implement an MVP pattern to accomplish the same (I believe). In short, I would say, "right tool for the job."
Thanks for reading!