0

我有一个名为FrmDrvouchers. 它包含一些公共子过程。我从另一个表单中调用这些 subs,该表单从 FrmDrVouchers 作为对话框打开的表单打开。

当我直接打开 FrmDrvouchers 时,一切正常。但是,当我从另一个项目(也是该解决方案的一部分)调用 FrmDrvouchers 时,当我从另一个(对话框)表单调用它们时,它的公共子程序不起作用。

这是我打开 FrmDrvouchers 的按钮单击的代码:

Dim FrmDrv As FrmDrVouchers = New FrmDrVouchers()    
FrmDrv.Show()

这行得通,但那些公共潜艇不行。为什么?


感谢您的回复,

它只是跳过我想做的事情,但不会引发任何异常,现在我正在发布我的代码,,,请看一下,,

这是我的主窗体的 MenuStrip 按钮单击代码:

 Private Sub CashPaymentToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CashPaymentToolStripMenuItem.Click
    Dim DrVouchers As Transactions.FrmDebitVouchers = Transactions.New FrmDebitVouchers()
    DrVouchers.Show()
End Sub

这是不工作的 FrmDebitVouchers 的公共订阅,从某种意义上说,它们跳过了我编写的函数但没有抛出任何异常,

Public Sub DrVoucherOPen(ByVal VoucherNo As Integer)

    'Filling the Dataset with selected Voucher No to OPen the record
    DebitVouchersTableAdapter.FillByVoucher(Flr12131DataSet.DebitVouchers, VoucherNo)
    VoucherDateDateTimePicker.Enabled = False
End Sub

Public Sub DrVoucherBodyOPen(ByVal VoucherNo As Integer)

    '---------Procedure to Open DrVouchersBody And to OPen it in Datagridview-------------'
    'Getting the User No, and if it is admin then load fill dataset and allow user to edit the record
    If GetUserNumber() = 1 Then
        'Filling the dataset
        DebitVouchersBodyTableAdapter.FillByVoucher(Flr12131DataSet.DebitVouchersBody, VoucherNo)

        DrBodyDGV.DataSource = Nothing
        Dim Sum As New Decimal

        'Initializing the SerialNumbers variable
        SerialNumbers = New List(Of Integer)

        'Setting datagridview to opend record
        For i = 0 To Flr12131DataSet.DebitVouchersBody.Rows.Count - 1

            DrBodyDGV.Rows.Add()
            DrBodyDGV.Rows(i).Cells(0).Value = Flr12131DataSet.DebitVouchersBody.Rows(i).Item("SerialNo")
            DrBodyDGV.Rows(i).Cells(3).Value = Flr12131DataSet.DebitVouchersBody.Rows(i).Item("AccountNo")
            DrBodyDGV.Rows(i).Cells(6).Value = Flr12131DataSet.DebitVouchersBody.Rows(i).Item("Debit")
            DrBodyDGV.Rows(i).Cells(7).Value = Flr12131DataSet.DebitVouchersBody.Rows(i).Item("Narration")
            'Getting serial No into List
            SerialNumbers.Add(Flr12131DataSet.DebitVouchersBody.Rows(i).Item("SerialNo"))

            'Getting Account Name into Datagridview
            If Not Not IsNumeric(DrBodyDGV.Rows(i).Cells(3).Value) Then
                Dim Qa As New Flr12131DataSetTableAdapters.QueriesTableAdapter
                Dim StrAccountName = Qa.GetAccountName(DrBodyDGV.Rows(i).Cells(3).Value)
                DrBodyDGV.Rows(i).Cells(5).Value = StrAccountName

            Else
            End If

            Sum += DrBodyDGV.Rows(i).Cells(6).Value

        Next


        TxtTotal.Text = Sum

        'Setting the controls properties for admin

        DrBodyDGV.AllowUserToAddRows = True
        DrBodyDGV.AllowUserToDeleteRows = True

        DrBodyDGV.ReadOnly = False
        BtnSave.Enabled = True
        BtnDelete.Enabled = True
        BtnPrint.Enabled = True
        ToUpdate = True
    Else


        'If user is not admin then load all record and not allow user to modify it or delete
        ' Bounding the datagridview

        Dim Sum As Decimal = 0
        BtnSave.Enabled = False
        DebitVouchersBodyTableAdapter.FillByVoucher(Flr12131DataSet.DebitVouchersBody, VoucherNo)
        DrBodyDGV.DataSource = Flr12131DataSet.DebitVouchersBody

        For i = 0 To DrBodyDGV.Rows.Count - 1
            If Not Not IsNumeric(DrBodyDGV.Rows(i).Cells(3).Value) Then
                Dim Qa As New Flr12131DataSetTableAdapters.QueriesTableAdapter
                Dim StrAccountName = Qa.GetAccountName(DrBodyDGV.Rows(i).Cells(3).Value)
                DrBodyDGV.Rows(i).Cells(5).Value = StrAccountName
            Else
            End If

            Sum += DrBodyDGV.Rows(i).Cells(6).Value

        Next

        TxtTotal.Text = Sum

        DrBodyDGV.AllowUserToAddRows = False
        DrBodyDGV.AllowUserToDeleteRows = False
        ' DrBodyDGV.edit()
        DrBodyDGV.ReadOnly = True
        BtnSave.Enabled = False
        BtnDelete.Enabled = False
        BtnPrint.Enabled = True

    End If


End Sub

这是另一个 DrVouchersRecord 表格,我将其称为 Public Subs:

Private Sub DrVouchersRecordDataGridView_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DrVouchersRecordDataGridView.CellDoubleClick


Dim FrmDrVouchers As FrmDebitVouchers = New FrmDebitVouchers()

    If FrmDrVouchers.DrBodyDGV.Rows.Count > 1 Then

        Dim Ans As Integer
        Ans = MsgBox("Unsaved changes will be lost, want to proceed", vbYesNo + vbInformation, "Alert")
        If Ans = vbYes Then
            Dim VoucherNo As New Integer

            VoucherNo = DrVouchersRecordDataGridView.CurrentRow.Cells(0).Value


            FrmDrVouchers.DrVoucherOPen(VoucherNo)
            FrmDrVouchers.DrVoucherBodyOPen(VoucherNo)
            Me.Close()
        End If

    Else

        Dim VoucherNo As New Integer

        VoucherNo = DrVouchersRecordDataGridView.CurrentRow.Cells(0).Value

        FrmDrVouchers.DrVoucherOPen(VoucherNo)
        FrmDrVouchers.DrVoucherBodyOPen(VoucherNo)
        Me.Close()

    End If

结束子

我的表格顺序是:

FrmMain 是我的主要启动形式

之后 FrmDebitVouchers 在 menustripButtonclick 上打开

之后 DebitVouchersRecord 作为对话框从 DrmDebitVouchers 中打开,并且从这些公共订阅中调用

我也添加了参考

等待你的答案,

4

1 回答 1

1

这个问题困扰着许多从 VB6 转向 VB.NET 的程序员。

在您的 DataGridView_DoubleCellClick 事件中,您创建一个FrmDebitVouchers的新实例。
然后你的代码引用这个新实例的属性/方法/对象,而不是通过 CashPaymentToolStripMenuItem_Click 创建的原始实例。(另请注意,这个新实例永远不会显示在屏幕上,因此您正在向隐藏的表单实例发送/请求更改)

当然,这个实例上的DrBodyDGV.Rows.Count为零(称为 FrmDrVouchers),因为可能这个实例从未像第一个实例那样初始化。

尝试在创建后添加一个 FrmDrVouchers.Show(),您将看到表单类 FrmDebitVouchers 的隐藏 SECOND INSTANCE。

要解决您的问题,您需要将第一个 FrmDebitVouchers 实例的引用传递给 DrVouchersRecord 表单(例如在构造函数中或通过公共属性),然后使用该引用而不是创建 FrmDrVouchers

于 2012-06-03T08:24:35.010 回答