你调用的对象是空的。我该如何删除

时间:2017-02-08 15:26:14

标签: vb.net

我在表单form2中嵌入了一个userform,由于某种原因,我开始得到以下内容。 “对象引用未设置为对象的实例”仅在form2上的设计模式下。

如果我'忽略并继续'通过,并运行该项目,表单工作正常,没有问题,错误消息仍然存在。

点击此处查看错误的屏幕截图。 form2 error

这是form2.designer.vb

下面是userform load事件的代码,我复制了触发上面错误的行。如果我注释掉这一行,设计错误就会消失。

If CType(Me.ParentForm.Owner, Form1).DataGridView1.Columns(col).ReadOnly = False Then 



Public Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim DS As New PIMUserDBDataSet.ewosappendixDataTable
    Dim col As Integer


    DataGridView1.Rows.Add()

    With DS
        For col = 0 To .Columns.Count - 1
            If CType(Me.ParentForm.Owner, Form1).DataGridView1.Columns(col).ReadOnly = False Then                    ComboBox1.Items.Add(DS.Columns(col).ColumnName)
            End If
        Next
        ComboBox1.Sorted = True
    End With


End Sub

非常感谢任何有关如何解决此问题的建议。

感谢,

这是加载/显示表单2的事件代码,因为它将帮助您识别form2 / usercontrol1的父级。

 Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim ufm As New Form2
    ufm.Owner = Me


    ufm.Show()


End Sub

这是我的form1.vb.design

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _

部分类Form1     继承System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        MyBase.Dispose(disposing)
    End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.  
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.components = New System.ComponentModel.Container()
    Me.DataGridView1 = New System.Windows.Forms.DataGridView()
    Me.EwosappendixbindingSource = New System.Windows.Forms.BindingSource(Me.components)
    Me.PIMUserDBDataSet = New WindowsApplication6.PIMUserDBDataSet()
    Me.UserSearch = New System.Windows.Forms.TextBox()
    Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
    Me.ToolTipCustlb = New System.Windows.Forms.ToolTip(Me.components)
    Me.Label1 = New System.Windows.Forms.Label()
    Me.Button1 = New System.Windows.Forms.Button()
    Me.EWOSFormBindingSource = New System.Windows.Forms.BindingSource(Me.components)
    Me.EWOS_FormTableAdapter = New WindowsApplication6.PIMUserDBDataSetTableAdapters.EWOS_FormTableAdapter()
    Me.EwosappendixTableAdapter = New WindowsApplication6.PIMUserDBDataSetTableAdapters.ewosappendixTableAdapter()
    Me.WorkerDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.ExecutiveDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.OwncodeDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.MaxusecusnoDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.FinalownDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.PidDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.ExecsummaryDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.Custliab1DataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.Reqvendreturn5DataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.Pimvendreturn6DataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.Pmvendreturnesc7DataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.AdddtDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.BpsverticalDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.ProdmgrDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.VendNameDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.EWOSClassDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.VendPNDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.LongDescDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.QtyavailDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.ValueDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.TurnsDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.InventoryanalystDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.ExecReviewDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.SrnoDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    Me.SR120DataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
    CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
    CType(Me.EwosappendixbindingSource, System.ComponentModel.ISupportInitialize).BeginInit()
    CType(Me.PIMUserDBDataSet, System.ComponentModel.ISupportInitialize).BeginInit()
    CType(Me.EWOSFormBindingSource, System.ComponentModel.ISupportInitialize).BeginInit()
    Me.SuspendLayout()
    '
    'DataGridView1
    '
    Me.DataGridView1.AllowUserToAddRows = False
    Me.DataGridView1.AllowUserToDeleteRows = False
    Me.DataGridView1.AllowUserToOrderColumns = True
    Me.DataGridView1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
        Or System.Windows.Forms.AnchorStyles.Left) _
        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.DataGridView1.AutoGenerateColumns = False
    Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
    Me.DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.WorkerDataGridViewTextBoxColumn, Me.ExecutiveDataGridViewTextBoxColumn, Me.OwncodeDataGridViewTextBoxColumn, Me.MaxusecusnoDataGridViewTextBoxColumn, Me.FinalownDataGridViewTextBoxColumn, Me.PidDataGridViewTextBoxColumn, Me.ExecsummaryDataGridViewTextBoxColumn, Me.Custliab1DataGridViewTextBoxColumn, Me.Reqvendreturn5DataGridViewTextBoxColumn, Me.Pimvendreturn6DataGridViewTextBoxColumn, Me.Pmvendreturnesc7DataGridViewTextBoxColumn, Me.AdddtDataGridViewTextBoxColumn, Me.BpsverticalDataGridViewTextBoxColumn, Me.ProdmgrDataGridViewTextBoxColumn, Me.VendNameDataGridViewTextBoxColumn, Me.EWOSClassDataGridViewTextBoxColumn, Me.VendPNDataGridViewTextBoxColumn, Me.LongDescDataGridViewTextBoxColumn, Me.QtyavailDataGridViewTextBoxColumn, Me.ValueDataGridViewTextBoxColumn, Me.TurnsDataGridViewTextBoxColumn, Me.InventoryanalystDataGridViewTextBoxColumn, Me.ExecReviewDataGridViewTextBoxColumn, Me.SrnoDataGridViewTextBoxColumn, Me.SR120DataGridViewTextBoxColumn})
    Me.DataGridView1.DataSource = Me.EwosappendixbindingSource
    Me.DataGridView1.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter
    Me.DataGridView1.Location = New System.Drawing.Point(21, 34)
    Me.DataGridView1.Name = "DataGridView1"
    Me.DataGridView1.Size = New System.Drawing.Size(1287, 619)
    Me.DataGridView1.TabIndex = 0
    '
    'EwosappendixbindingSource
    '
    Me.EwosappendixbindingSource.AllowNew = False
    Me.EwosappendixbindingSource.DataMember = "ewosappendix"
    Me.EwosappendixbindingSource.DataSource = Me.PIMUserDBDataSet
    '
    'PIMUserDBDataSet
    '
    Me.PIMUserDBDataSet.DataSetName = "PIMUserDBDataSet"
    Me.PIMUserDBDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema
    '
    'UserSearch
    '
    Me.UserSearch.Location = New System.Drawing.Point(720, 8)
    Me.UserSearch.Name = "UserSearch"
    Me.UserSearch.Size = New System.Drawing.Size(102, 20)
    Me.UserSearch.TabIndex = 1
    Me.UserSearch.Visible = False
    '
    'Timer1
    '
    Me.Timer1.Interval = 750
    '
    'Label1
    '
    Me.Label1.AutoSize = True
    Me.Label1.Location = New System.Drawing.Point(18, 11)
    Me.Label1.Name = "Label1"
    Me.Label1.Size = New System.Drawing.Size(38, 13)
    Me.Label1.TabIndex = 2
    Me.Label1.Text = "Filter : "
    '
    'Button1
    '
    Me.Button1.Location = New System.Drawing.Point(1153, 5)
    Me.Button1.Name = "Button1"
    Me.Button1.Size = New System.Drawing.Size(102, 23)
    Me.Button1.TabIndex = 3
    Me.Button1.Text = "Mass Update"
    Me.Button1.UseVisualStyleBackColor = True
    '
    'EWOSFormBindingSource
    '
    Me.EWOSFormBindingSource.AllowNew = False
    Me.EWOSFormBindingSource.DataMember = "EWOS_Form"
    Me.EWOSFormBindingSource.DataSource = Me.PIMUserDBDataSet
    '
    'EWOS_FormTableAdapter
    '
    Me.EWOS_FormTableAdapter.ClearBeforeFill = True
    '
    'EwosappendixTableAdapter
    '
    Me.EwosappendixTableAdapter.ClearBeforeFill = True
    '
    'WorkerDataGridViewTextBoxColumn
    '
    Me.WorkerDataGridViewTextBoxColumn.DataPropertyName = "worker"
    Me.WorkerDataGridViewTextBoxColumn.HeaderText = "worker"
    Me.WorkerDataGridViewTextBoxColumn.Name = "WorkerDataGridViewTextBoxColumn"
    '
    'ExecutiveDataGridViewTextBoxColumn
    '
    Me.ExecutiveDataGridViewTextBoxColumn.DataPropertyName = "executive"
    Me.ExecutiveDataGridViewTextBoxColumn.HeaderText = "executive"
    Me.ExecutiveDataGridViewTextBoxColumn.Name = "ExecutiveDataGridViewTextBoxColumn"
    '
    'OwncodeDataGridViewTextBoxColumn
    '
    Me.OwncodeDataGridViewTextBoxColumn.DataPropertyName = "owncode"
    Me.OwncodeDataGridViewTextBoxColumn.HeaderText = "owncode"
    Me.OwncodeDataGridViewTextBoxColumn.Name = "OwncodeDataGridViewTextBoxColumn"
    '
    'MaxusecusnoDataGridViewTextBoxColumn
    '
    Me.MaxusecusnoDataGridViewTextBoxColumn.DataPropertyName = "maxusecusno"
    Me.MaxusecusnoDataGridViewTextBoxColumn.HeaderText = "maxusecusno"
    Me.MaxusecusnoDataGridViewTextBoxColumn.Name = "MaxusecusnoDataGridViewTextBoxColumn"
    Me.MaxusecusnoDataGridViewTextBoxColumn.ReadOnly = True
    '
    'FinalownDataGridViewTextBoxColumn
    '
    Me.FinalownDataGridViewTextBoxColumn.DataPropertyName = "finalown"
    Me.FinalownDataGridViewTextBoxColumn.HeaderText = "finalown"
    Me.FinalownDataGridViewTextBoxColumn.Name = "FinalownDataGridViewTextBoxColumn"
    '

    'SR120DataGridViewTextBoxColumn
    '
    Me.SR120DataGridViewTextBoxColumn.DataPropertyName = "SR120"
    Me.SR120DataGridViewTextBoxColumn.HeaderText = "SR120"
    Me.SR120DataGridViewTextBoxColumn.Name = "SR120DataGridViewTextBoxColumn"
    Me.SR120DataGridViewTextBoxColumn.ReadOnly = True
    '
    'Form1
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.AutoSize = True
    Me.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
    Me.ClientSize = New System.Drawing.Size(1362, 703)
    Me.Controls.Add(Me.Button1)
    Me.Controls.Add(Me.Label1)
    Me.Controls.Add(Me.UserSearch)
    Me.Controls.Add(Me.DataGridView1)
    Me.Name = "Form1"
    Me.Text = "EWOS"
    CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
    CType(Me.EwosappendixbindingSource, System.ComponentModel.ISupportInitialize).EndInit()
    CType(Me.PIMUserDBDataSet, System.ComponentModel.ISupportInitialize).EndInit()
    CType(Me.EWOSFormBindingSource, System.ComponentModel.ISupportInitialize).EndInit()
    Me.ResumeLayout(False)
    Me.PerformLayout()

End Sub

Friend WithEvents DataGridView1 As DataGridView
Friend WithEvents PIMUserDBDataSet As PIMUserDBDataSet
Friend WithEvents EWOS_FormTableAdapter As PIMUserDBDataSetTableAdapters.EWOS_FormTableAdapter
Friend WithEvents UserSearch As TextBox
Friend WithEvents Timer1 As Timer
Friend WithEvents EWOSFormBindingSource As BindingSource
Friend WithEvents ToolTipCustlb As ToolTip
Friend WithEvents Label1 As Label
Friend WithEvents EwosappendixTableAdapter As PIMUserDBDataSetTableAdapters.ewosappendixTableAdapter
Friend WithEvents EwosappendixbindingSource As BindingSource
Friend WithEvents Button1 As Button
Friend WithEvents WorkerDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents ExecutiveDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents OwncodeDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents MaxusecusnoDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents FinalownDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents PidDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents ExecsummaryDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents Custliab1DataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents Reqvendreturn5DataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents Pimvendreturn6DataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents Pmvendreturnesc7DataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents AdddtDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents BpsverticalDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents ProdmgrDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents VendNameDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents EWOSClassDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents VendPNDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents LongDescDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents QtyavailDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents ValueDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents TurnsDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents InventoryanalystDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents ExecReviewDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents SrnoDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
Friend WithEvents SR120DataGridViewTextBoxColumn As DataGridViewTextBoxColumn

结束班

这是form1.vb - 我通过button1点击

调用form2
Imports DgvFilterPopup

Public Class Form1

Public filtermanger As DgvFilterManager

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim ufm As New Form2

    'showing form2 which has the user control.
    ufm.Owner = Me
    ufm.Show()

End Sub

Private Sub DataGridView1_BindingContextChanged(sender As Object, e As EventArgs) Handles DataGridView1.BindingContextChanged
    Dim filtermanger As DgvFilterManager
    Dim idx As Integer

    filtermanger = New DgvFilterManager(DataGridView1)
    DataGridView1.GetType.InvokeMember("DoubleBuffered", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.SetProperty, Nothing, DataGridView1, New Object() {True})

    With DataGridView1
        .EnableHeadersVisualStyles = False

        For Each col In DataGridView1.Columns

            'format (highlight) editable columns
            Select Case col.datapropertyname
                Case "execsummary", "custliab_1", "reqvendreturn_5", "pimvendreturn_6", "pmvendreturnesc_7", "bpsvertical"
                    idx = col.index
                    .Columns(idx).HeaderCell.Style.BackColor = Color.Wheat
                    .Columns(idx).HeaderCell.Style.Font = New Font(DataGridView1.Font, FontStyle.Bold)

                    If col.datapropertyname = "custliab_1" Then
                        .Columns(idx).ToolTipText = "Valid Values (uppercase):" _
                      & vbCrLf & "C : Customer Liable" _
                      & vbCrLf & "V : Vendor Liable(Returnable)" _
                      & vbCrLf & "R : Customer commitment but no Liability" _
                      & vbCrLf & "S : KGP Is Stuck With it As customer Is Not Liable. Cannot Return To vendor, but customer contract requires KGP to keep on hand." _
                      & vbCrLf & "N :  Not Liable (Customer Or Vendor)" _
                      & vbCrLf & "Blank"
                    End If
            End Select
        Next
    End With
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim dc As DataColumn
    Dim idx As Integer
    Dim user As Integer

    'Form3 is the initial form the user see's.  they click one of two buttons.
    user = Form3.user
    For Each col In Me.PIMUserDBDataSet.ewosappendix.Columns
        Select Case user
            Case 1 'EWOS User
                dc = col
                idx = col.ordinal
                Select Case dc.ColumnName
                    Case "execsummary", "custliab_1", "reqvendreturn_5", "pimvendreturn_6", "pmvendreturnesc_7", "bpsvertical"
                        dc.ReadOnly = False
                        DataGridView1.Columns(idx).ReadOnly = False
                    Case Else
                        dc.ReadOnly = True
                        DataGridView1.Columns(idx).ReadOnly = True

                End Select
            Case 2 'Inv Anaylyst
                dc = col
                idx = col.ordinal
                Select Case dc.ColumnName
                    Case "reqvendreturn_5", "pimvendreturn_6", "pmvendreturnesc_7"
                        dc.ReadOnly = False
                        DataGridView1.Columns(idx).ReadOnly = False
                    Case Else
                        dc.ReadOnly = True
                        DataGridView1.Columns(idx).ReadOnly = True
                        'dcdt.ReadOnly = True
                End Select


            Case 3
                dc = col
                idx = col.ordinal
                Select Case dc.ColumnName

                    Case "execsummary", "custliab_1", "reqvendreturn_5", "pimvendreturn_6", "pmvendreturnesc_7", "bpsvertical", " ExecReview", "inventoryanalyst" &
                     ",EWOSClass", "custliab_1", "worker", "prodmgr", "owncode", "executive", "finalown"
                        dc.ReadOnly = False
                        DataGridView1.Columns(idx).ReadOnly = False
                    Case Else
                        dc.ReadOnly = True
                        DataGridView1.Columns(idx).ReadOnly = True
                        'dcdt.ReadOnly = True
                End Select
        End Select

    Next
    Me.EwosappendixTableAdapter.Fill(Me.PIMUserDBDataSet.ewosappendix)
End Sub

这是Form2.vb.design

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>

部分班级表格2     继承System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        MyBase.Dispose(disposing)
    End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.  
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
    Me.UserControl11 = New WindowsApplication6.UserControl1()
    Me.SuspendLayout()
    '
    'UserControl11
    '
    Me.UserControl11.Location = New System.Drawing.Point(30, 15)
    Me.UserControl11.Name = "UserControl11"
    Me.UserControl11.Size = New System.Drawing.Size(784, 728)
    Me.UserControl11.TabIndex = 0
    '
    'Form2
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.ClientSize = New System.Drawing.Size(861, 755)
    Me.Controls.Add(Me.UserControl11)
    Me.Name = "Form2"
    Me.Text = "Form2"
    Me.ResumeLayout(False)

End Sub

Friend WithEvents UserControl11 As UserControl1

结束班

**这是usercontrol1的Load事件 - 位于Form2上。 * *之间的代码是抛出设计错误的地方。请参阅上面的form2错误链接**。

Public Class UserControl1
Public Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim DS As New PIMUserDBDataSet.ewosappendixDataTable
    Dim col As Integer


    DataGridView1.Rows.Add()

    With DS
        For col = 0 To .Columns.Count - 1
            **If CType(Me.ParentForm.Owner, Form1).DataGridView1.Columns(col).ReadOnly = False Then
                ComboBox1.Items.Add(DS.Columns(col).ColumnName)
            End If**
        Next
        ComboBox1.Sorted = True
    End With

2 个答案:

答案 0 :(得分:0)

由于您的控件通过Form2的Owner属性引用Form1,您需要在初始化程序运行之前设置它.......

增加Form2的新方法如下。

Public Sub New(myOwner As Form1)

    Owner = myOwner

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

End Sub

然后这样称呼它。

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim ufm As New Form2(Me)
    ufm.Show()
End Sub

我会在IDE中将您的usercontrol更改为安全,如下所示。

If me.parentform.owner IsNot Nothing then
    With DS
        For col = 0 To .Columns.Count - 1
            If CType(Me.ParentForm.Owner, Form1).DataGridView1.Columns(col).ReadOnly = False Then
            ComboBox1.Items.Add(DS.Columns(col).ColumnName)
            End If
        Next
        ComboBox1.Sorted = True
    End With
End If

答案 1 :(得分:0)

在您的vs设计模式下打开用户控件的加载事件时,您可以添加检查条件,以便在不在设计时间时执行加载,例如:

bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);