XN16
XN16

Reputation: 5869

Opening a generic form from another class causes a IWin32Window conversion error

I have been using the following code that will open a generic form from a button click from within a datagrid cell. This has been working fine for several months now, however I need to implement the same code in another couple of forms, so to save repeating code I decide to make a wrapper class to handle this and just create an instance of this class wherever needed. However I get a 'Conversion from type 'ComplexPropertiesFormWrapper' to type 'IWin32Window' is not valid.' error which I simply don't understand considering it worked in my first version of implementation.

First implementation (works as expected):

Private Sub editorButton_Click(sender As Object, e As EditorButtonEventArgs)

    Dim dataObject As New DataObject()
    Dim dataObjectType As Type = dataObject.Type
    Dim formType As Type = GetType(ManageComplexProperties(Of )).MakeGenericType(dataObjectType)
    Dim complexPropertiesForm = Activator.CreateInstance(formType, _
        New Object() {dataObject.Value, dataObject.ValueIsList, Nothing, MyBase.UnitOfWorkNH})

    If complexPropertiesForm.ShowDialog(Me) = DialogResult.OK Then
        dataObject.Value = complexPropertiesForm.GetResult()
    End If
    complexPropertiesForm.Dispose()

End Sub

Second implementation (gets the error as described above):

Here is the modified event handler from above:

Private Sub editorButton_Click(sender As Object, e As EditorButtonEventArgs)

    Dim dataObject As New DataObject()
    Dim dataObjectType As Type = dataObject.Type
    Dim complexPropertiesFormWrapper As New ComplexPropertiesFormWrapper(dataObjectType, dataObject.Value, dataObject.ValueIsList, Nothing, MyBase.UnitOfWorkNH)
    complexPropertiesFormWrapper.Show()
    dataObject.Value = complexPropertiesFormWrapper.Value
    complexPropertiesFormWrapper.Dispose()

End Sub

Here are the relevant methods from the ComplexPropertiesFormWrapper class:

Public Sub Show()

    Dim formType As Type = GetType(ManageComplexProperties(Of )).MakeGenericType(_type)
    Dim _manageComplexPropertiesForm = Activator.CreateInstance(formType, _
         New Object() {_value, _valueIsList, Nothing, _unitOfWork})

    'ERROR OCCURS ON THE FOLLOWING LINE
    _result = _manageComplexPropertiesForm.ShowDialog(Me)
    If _result = DialogResult.OK Then
        _resultValue = _manageComplexPropertiesForm.GetResult()
    End If

End Sub

Public Sub Dispose()

    _manageComplexPropertiesForm.Dispose()

End Sub

There are parts of the code missing, however they are all to do with the operation of the form and class, therefore not going to be caused by this issue.

I have spent a bit of time searching and I can't find much on the subject bar references to the IntPtr of windows and handles of controls, which doesn't seem to describe my issues.

Does anyone have a solution to this issue, and/or an explanation of why it is happening?

Answers are welcome in VB or C#.

Upvotes: 1

Views: 243

Answers (2)

Johnfed
Johnfed

Reputation: 106

The Form.ShowDialog() method is overloaded and accepts either no parameters or an IWin32Window. You are calling the latter implementation.

Does your ComplexPropertiesFormWrapper class inherit Form? If not it is not castable to an IWin32Window.

Can you either change it so it does inherit from Form or call ShowDialog() without the Me parameter? If the former you will need to declare ComplexPropertiesFormWrapper.Show() as Overloads.

First method:

Public Class ComplexPropertiesFormWrapper
    Inherits Form

    Public Overloads Sub Show()

        Dim f As New Form1

        f.ShowDialog(Me)

    End Sub
End Class

Second method:

Public Class ComplexPropertiesFormWrapper

    Public Sub Show()

        Dim f As New Form1

        f.ShowDialog()

    End Sub
End Class

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941455

A name like "FormWrapper" would hint at the source of the problem, it doesn't sound like a class that derives from Form. It is the Form class that implements the IWin32Window interface. Your class has to implement that too to make the conversion valid. Not otherwise difficult to do, just return the wrapped form's Handle property. Do consider not wrapping the Form derived class at all as another solution, there is usually very little need for that.

A long-distance secondary explanation is that you redeclared IWin32Window, don't do that.

Upvotes: 1

Related Questions