Passing Multiple Parameters Between Forms
I have been trying to figure out, albeit unsuccessfully, the magic trick for passing multiple parameters between Forms.
The first form (frmReportLauncher) is used to select which report (of 9) to generate, and the second (frmReportBrowser) generates and displays the appropriate (unfortunately, Crystal) report in a viewer named crvReportViewer. Unless someone has a better idea, I am attempting to pass the appropriate DataSource, report Query (SQL), .rpt name, and a string to display in the report header. Right now I'm injecting the parameters into a function on the second Form to produce the report.
I've looked at countless posts related to passing parameters between forms. Most of these discuss passing a single parameter, which is fairly easy to do; I already do this elsewhere in my application. A handful discuss passing more than one parameter, but pretty much keep it to two. I figured expanding the scale would work, but no joy. I've tried both a public property approach and a constructor overload approach, but neither worked. I'm hoping someone could give (or point to) an example I can follow to make things work because at this point I have no idea how to make it happen.
TBH, I would rather use FastReports (OpenSource), but unfortunately it doesn't want to work for me either and all examples are C#, which I don't know at all. Using Crystal is a last-resort thing and my headaches are exacerbated by its not working with VS2026; it works with VS2022 though...
Re: Passing Multiple Parameters Between Forms
Whether it's one or three the method is the same. If you use the constructor method,
Code:
Public Class Form2
Private var1 As String
Private var2 As String
Private var3 As String
Public Sub New(param1 As String, param2 As String, param3 As String)
var1 = param1
var2 = param2
var3 = param3
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Use the appropriate data types, this example just uses strings.
Re: Passing Multiple Parameters Between Forms
Quote:
Originally Posted by
wes4dbt
Whether it's one or three the method is the same. If you use the constructor method,
Thanks. I figured that it's a case of scaling up. But I'm also going to assume "Form2" would really be the name declared in the code for the form containing the Crystal object; in my case, frmReportBrowser...
Re: Passing Multiple Parameters Between Forms
You could pass a class or an argument array if need be. For example, with a class:
Code:
Public Class ReportParameters
Public Property Parameter1 As String
Public Property Parameter2 As String
Public Property Parameter3 As String
' etc...
End Class
Public Class frmReportBrowser
Public Sub New(parameters As ReportParameters)
InitializeComponent()
' do something...
End Sub
End Class
Public Class MainForm
Public Sub ButtonOpenReport_Click(sender As Object, e As EventArgs) Handles ButtonOpenReport.Click
Dim parameters As New ReportParameters() With {
.Parameter1 = "Foo",
.Parameter2 = "Bar",
.Parameter3 = "FooBar",
}
Using reportBrowser As New frmReportBrowser(parameters)
reportBrowser.ShowDialog()
End Using
End Sub
End Class
Or with the argument array:
Code:
Public Class frmReportBrowser
Public Sub New(args As String())
InitializeComponent()
_args = args
If (_args Is Nothing) Then
_args = {}
End If
Dim parameter1 As String = _args.FirstOrDefault()
Dim parameter2 As String = _args.Skip(1).FirstOrDefault()
Dim parameter3 As String = _args.Skip(2).FirstOrDefault()
' do something...
End Sub
End Class
Public Class MainForm
Public Sub ButtonOpenReport_Click(sender As Object, e As EventArgs) Handles ButtonOpenReport.Click
Using reportBrowser As New frmReportBrowser(parameters)
reportBrowser.ShowDialog("Foo", "Bar", "FooBar")
End Using
End Sub
End Class
The class option would provide you with scale while keeping strongly typed objects whereas the argument array will provide you scale but you'd be responsible with handling it properly.