2012年11月8日 星期四

[學習] Create Dynamic Form with Option Strict On


Option Strict 陳述式

Option Strict On 不允許晚期繫結


原始為---

Option Strict Off

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

        Dim frmEdit As Object = Nothing
        Select Case TabControl1.SelectedIndex
            Case 0
                frmEdit = New Edit_education
                DirectCast(frmEdit, Form).StartPosition = FormStartPosition.CenterParent
            Case 1
                frmEdit = New Edit_family
                DirectCast(frmEdit, Form).StartPosition = FormStartPosition.CenterParent
            Case 2
                frmEdit  = New Edit_credential
                DirectCast(frmEdit, Form).StartPosition = FormStartPosition.CenterParent
        End Select
        frmEdit.ShowDialog
        If frmEdit.DialogResult = Windows.Forms.DialogResult.OK Then
                'do something
        End If
End Sub


更改為---

Option Strict On

會發生 Error 隱含轉換
錯誤1     Object 不允許 frmEdit.ShowDialog 晚期繫結
錯誤2     Object 不允許 frmEdit.DialogResult 晚期繫結

因此改為---

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


        Dim dlgResult As DialogResult
        Select Case TabControl1.SelectedIndex
            Case 0
                Dim frmEdit As Edit_education = New Edit_education
                DirectCast(frmEdit, Form).StartPosition = FormStartPosition.CenterParent
                dlgResult = frmEdit.ShowDialog 'Option Strict On 不允許晚期繫結 因為宣告為Object
            Case 1
                Dim frmEdit As Edit_family = New Edit_family
                DirectCast(frmEdit, Form).StartPosition = FormStartPosition.CenterParent
                dlgResult = frmEdit.ShowDialog 'Option Strict On 不允許晚期繫結 因為宣告為Object
            Case 2
                Dim frmEdit As Edit_credential = New Edit_credential
                DirectCast(frmEdit, Form).StartPosition = FormStartPosition.CenterParent
                dlgResult = frmEdit.ShowDialog 'Option Strict On 不允許晚期繫結 因為宣告為Object
        End Select

        If dlgResult = Windows.Forms.DialogResult.OK Then
                'do something
        End If
End Sub

或者改為---

        Dim frmEditN As Form = Nothing
        Select Case TabControl1.SelectedIndex
            Case 0
                Dim frmEdit As Edit_education = New Edit_education
                DirectCast(frmEdit, Form).StartPosition = FormStartPosition.CenterParent
                frmEditN = frmEdit
            Case 1
                Dim frmEdit As Edit_family = New Edit_family
                DirectCast(frmEdit, Form).StartPosition = FormStartPosition.CenterParent
                frmEditN = frmEdit
        End Select

        frmEditN.ShowDialog()
        If frmEditN.DialogResult = Windows.Forms.DialogResult.OK Then
                'do something
        End If





沒有留言:

張貼留言