Passing Value Back (forms)

S

Stephen Costanzo

My goal is to open Form2 from Form1 and have Form2 pass back an integer to
Form 1.

In VB, this is rather simple:

Form 1 (vanilla form with a button):
Dim WithEvents x As Form2
Dim i As Integer

Sub form2Event(ByVal num As Integer) Handles x.RouteNumber
i = num
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
x = New Form2
x.ShowDialog()
MessageBox.Show(i.ToString)
End Sub

Form 2 (vanilla form with a button)

Public Event RouteNumber(ByVal num As Integer)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
RaiseEvent RouteNumber(5)
Me.Close()
End Sub

When I look to do this simple function in C#, it appears (from the Event
Sample in the help) to involve a whole lot of coding, including creating
classes to handle the event etc. Am I just spoiled by VB.NET and its event
handling or am I misreading the C# documentation from the help files.

Thanks in advance.
 
T

Tedb

Why not just have the Route Number be a public property of Form2 that can be
read after the call to ShowDialog?

Form 2 (vanilla form with a button)

Public RouteNumber as Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
RouteNumber = 5
Me.Close()
End Sub

Form1

Dim x As Form2
Dim i As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
x = New Form2
x.ShowDialog()
MessageBox.Show(x.RoutNumber.ToString)
End Sub
 
S

Stephen Costanzo

As long as that doesn't violate some principle in coding, that's fine. I
wasn't sure if doing that violated some OO principle.

Side Note: Is there a site that lists the 'dos and don'ts of OO'? I know
that global variables are a "don't" but wouldn't know if exposing the
property as Teddb specified is ok - seems like it would be as a form is
simply a class.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top