I've searched all over the Net and here on DL but I can seem to find only information on passing a single argument when opening a new form. When I attempt to send more than one I get the "Too many arguments to 'Public Sub New()'" error. I am trying to send three arguments that I need passed to new new form. I was attempting to use the three arguments/variables in the LOAD event of the new form as they are needed for the initial state of the new form.

Form one has a DataGridView populated from an XML file. I am attempting to pass the path of the path of the specific XML file and the one line of the DGV that the user currently has selected so that when the form loads it can open the XML from the provided path and populate the DGV with the current selected row from form one already selected in the DGV on the new form. All three pieces of information that are to be sent as arguments are properly set to variables in form one as I had tested them by printing the to console as well as a MsgBox so they are ready to be sent but cannot figure out how to send them.

I usually always provide code however I have tried so many methods that haven't worked that I am not even sure what to try showing here. Out of slight frustration I've deleted everything from the second form's .vb file haha So there's really nothing I could currently post from that.

But I tries creating a module to publicly declare the three variables and use it on both forms so that the first form could set the variables to a value and the new form could grab the values from there but any examples I had found online to do so failed to work for me and most of the threads were 'locked' due to age so I could not continue the thread for more info specific to my case.

I will try to answer any questions that may help. There's got to be a way to do this haha And once I know..then I will have learned it and ca pass my knowledge on. I greatly appreciate any and all help!

Simply create a New() method that accepts the 3 variables. Then when you create a new version of the form use that overload to send the variables.(public Form2(string varA,string varB, string varC)). In Form1:

Form2 newForm2 = new Form2(var1,var2,var3);

I believe I've tried that and I am still receiving the error but at least now I have some code to show.

Form 1:

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Dim CurrentSong As DataGridViewRow = DataGridView1.Rows(0)
        Dim CurrentPosition = AxWindowsMediaPlayer1.Ctlcontrols.currentPosition
        Dim FifthForm = New MiniMode(Playing, CurrentSong, CurrentPosition)

        FifthForm.Show()
        Me.Hide()
    End Sub

Form 2

Public Class MiniMode

    Public Sub MiniMode(Playing As String, CurrentSong As Integer, CurrentPosition As Integer)
        MsgBox(Playing & CurrentSong & CurrentPosition)
    End Sub
End Class

Form 2 is technically Form5.vb but I am referring to it as Form 2 for the sake of what I am trying to accomplish. Again, the variables are holding the values that I need them to in the sub of Form1.

I am not sure if I am messing it up with the class in the second form or what. I've created plenty of subs that pas multiple arguments within the same form but keep getting the too many arguments error.

I am trying to see if I can end my birthday by getting this part working hahaha

Sorry I gave you c# code instead of VB.

In the second form overload the New method:

Public Sub New(Playing As String, CurrentSong As Integer, CurrentPosition As Integer)

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

End Sub

Now instantiating a new form should work

Edited 1 Year Ago by tinstaafl

AFIAK The Form constructor has no overloads. See here.
If you have a second form you can refer to another form by adding a field not by passing parameters.

AFIAK The Form constructor has no overloads.

That's not strictly true, the form at the end of the day is just a class.

In VB.NET, you do not see the constructor by default but if you type Public Sub New and hit enter you will get the following standard constructor code:

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

You can then (as with any class) create an overloaded New method such as:

    Public Sub New(ByVal arg1 As String, ByVal arg2 As String, ByVal arg3 As String)

        InitializeComponent()

        MessageBox.Show(arg1)
        MessageBox.Show(arg2)
        MessageBox.Show(arg3)

    End Sub

Be sure to call InitializeComponent() as mentioned in the boiler plate code generated.

To test, from Form1 add two buttons: one will just show the form with no message boxes, the other will output the three message boxes:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim f2 As New Form2
        f2.Show()

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim f2 As New Form2("arg1", "arg2", "arg3")
        f2.Show()

    End Sub

Edited 1 Year Ago by djjeavons

Comments
Thanks for your tip!

We've nailed it now! I had forgotten that VBStudio creates a Sub New automatically "in the background" with no parameters when adding a new form to a project and therefore one has to create one on their form's .vb file to 'override' the Studio created one. Thank you so much for your help as it's now working like a charm!

This question has already been answered. Start a new discussion instead.