0

I basically i have two forms, one named 'frmSettings' and another names 'frmXLExternalFile', 'frmXLExternalFile' is created from 'frmSettings', there is data passed between these two forms and when i return it using a property it returns as null. Ive tried returning it by settings it to public but that still doesnt seem to work some strange reason. I've set breakpoints and traced the variable ( actually a structure ) and it is certainly not 'null'

frmXLExternalFile

    Dim XL_File As frmMain.XLSheetData
Public ReadOnly Property XLFile As frmMain.XLSheetData
        Get
            Return XL_File
        End Get
    End Property

Private Sub frmXLExternalFile_formClosing(sender As Object, e As EventArgs) Handles MyBase.FormClosing
            If txtFilepath.Text <> "" And cmboName.Text <> "" Then
                XL_File = New frmMain.XLSheetData
                XL_File.name = cmboName.Text
                XL_File.filePath = txtFilepath.Text
                frmMain.settings.setXLFile()
                frmMain.settings.cmboXLSheets.Text = txtFilepath.Text
            End If
            frmMain.settings.Enabled = True
        End Sub

frmMain (This is where the structure is declared)

Public Structure XLSheetData
        Dim name As String
        Dim filePath As String
    End Structure

frmSettings

Dim XL_FileList As List(Of frmMain.XLSheetData)

    Sub setXLFile()
        Dim file As frmMain.XLSheetData = frmXLExternalFile.XLFile
        XL_FileList.Add(file)
        cmboXLSheets.Items.Add(file.filePath)
    End Sub

basically, The top form calls this the bottom method once the field - XL_File - is filled, this then uses the property - 'XLFile' - to 'Get' the object and put it in the 'frmSettings' class. As I have said, i have tried setting 'XL_File' to public and tried accessing it directly but the same exception is thrown. It is null, the combo box and text box that are used to fill the object are not null. Any help would be appreciated. Thanks.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Daniel Baron
  • 34
  • 1
  • 5
  • change from `Structure` to `Class`. – Blorgbeard Feb 09 '15 at 00:04
  • 2
    Sounds like maybe you are using default form instances, see http://stackoverflow.com/q/28252346/1070452 – Ňɏssa Pøngjǣrdenlarp Feb 09 '15 at 00:04
  • What's the exact error? Are you initializing `XL_FileList` anywhere? – Blorgbeard Feb 09 '15 at 00:06
  • Are you displaying `frmXLExternalFile` with `Show()` or `ShowDialog()`? – Idle_Mind Feb 09 '15 at 01:07
  • @Idle_Mind im using 'Show()' – Daniel Baron Feb 09 '15 at 14:40
  • @Blorgbeard Just so to make sure i know what you mean, XLSheetData is within a class for that form, so do you want me to change the structure to a class? I dont understand how that would benefit me as all i require is a structure and not a class? And yes it is intialized, ill try and get the error for you, Thanks – Daniel Baron Feb 09 '15 at 14:44
  • @Blorgbeard this is the exception I am given 'A first chance exception of type 'System.ArgumentNullException' occurred in System.Windows.Forms.dll', I set up a mesagebox to show 'exception.message' and this is what i got 'Value cannot be null. Parameter name: item. – Daniel Baron Feb 09 '15 at 14:49
  • It's generally a bad idea to use Structure unless you have a good reason. Classes are simpler. Structures are value-types which means they have different semantics and you may run into unexpected behaviour. You can find out which line causes the error by looking at the exception stack-trace. Try showing `exception.ToString()` instead of just the message. – Blorgbeard Feb 09 '15 at 18:45
  • @Blorgbeard cmboXLSheets.Items.Add(frmXLExternalFile.XL_File.filePath)' This is the line of code throwing the exception. The exception is caused as 'frmXLExternalFile.XL_File' is null. The problem is that i do not understand why this is null. I have tried changing structure to class but the problem still prevails. Any help grateful – Daniel Baron Feb 09 '15 at 22:48

1 Answers1

0
  1. Here's one way to do it with Show(), by passing in the "Owner" form:

In frmSettings:

Dim frmX As New frmXLExternalFile
' ... pass some data to frmX ...
frmX.Show(Me) ' <-- pass this form in as the "Owner"

In frmXLExternalFile:

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    If txtFilepath.Text <> "" And cmboName.Text <> "" Then
        XL_File = New frmMain.XLSheetData
        XL_File.name = cmboName.Text
        XL_File.filePath = txtFilepath.Text

        Dim main As frmSettings = DirectCast(Me.Owner, frmSettings) ' <-- use the instance passed in as the owner via Show()
        ' ... use "main" somehow ...
        main.settings.setXLFile()
        main.settings.cmboXLSheets.Text = txtFilepath.Text
        main.settings.Enabled = True
    Else
        ' ... possibly do something else ...
    End If
End Sub
  1. This example demonstrates a ShowDialog() approach:

In frmSettings:

Dim frmX As New frmXLExternalFile
' ... pass some data to frmX ...
If frmX.ShowDialog() = Windows.Forms.DialogResult.OK Then ' <-- execution in this Form STOPS until "frmX" is dismissed
    ' Retrieve the value from out "frmX" instance:
    Dim file As frmMain.XLSheetData = frmX.XLFile

    ' ... do something with "file" ...
    XL_FileList.Add(file)
    cmboXLSheets.Items.Add(file.filePath)
    cmboXLSheets.Text = file.filePath
End If

In frmXLExternalFile:

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    If txtFilepath.Text <> "" And cmboName.Text <> "" Then
        XL_File = New frmMain.XLSheetData
        XL_File.name = cmboName.Text
        XL_File.filePath = txtFilepath.Text

        ' Set DialogResult, returning execution to the ShowDialog() line:
        Me.DialogResult = Windows.Forms.DialogResult.OK
    Else
        ' ... possibly do something else ...
    End If
End Sub
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40