【问题标题】:Call a sub on another form in classLibrary project在类库项目中调用另一个表单上的子
【发布时间】:2018-05-08 03:45:06
【问题描述】:

我有一个多项目的解决方案,我有一个名为master_ChartofAccount 的主表单,在保存数据后在coa_Create 上,我想调用一个名为showCoaSub 来刷新一个网格coa_Create,这是我的代码:

Private Sub cmdAdd_Click(sender As Object, e As EventArgs) Handles cmdAdd.Click
    Dim frm As New coa_Create
    frm.ShowDialog(Me)
End Sub

这是cmdSavecoa_Create 上的代码:

Private Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click
    If Not allowSave() Then Exit Sub
    Dim str As String
    txtKode.Tag = IIf(IsNothing(txtKode.Tag), "", txtKode.Tag)

    If txtKode.Tag.ToString = "" Then
        str = "insert into t_chart_of_account (coa_code, coa_name, p_code, is_parent, db_cr, bs_is) " _
            & "values ('" & objComp.clearSingleQuote(txtKode.Text) & "', '" & objComp.clearSingleQuote(txtNama.Text) & "', " _
            & "'" & objComp.clearSingleQuote(txtParentCode.Text) & "', " & IIf(chkParent.Checked = True, 1, 0) & ", " _
            & "'" & IIf(optDb.Checked = True, "DR", "CR") & "', '" & IIf(optBS.Checked = True, "BS", "IS") & "')"
    Else
        str = "update t_chart_of_account set coa_code = '" & txtKode.Text & "', " _
            & "coa_name = '" & objComp.clearSingleQuote(txtNama.Text) & "', p_code = '" & txtParentCode.Text & "', " _
            & "is_parent = " & IIf(chkParent.Checked = True, 1, 0) & ", db_cr = '" & IIf(optDb.Checked = True, "DR", "CR") & "', " _
            & "bs_is = '" & IIf(optBS.Checked = True, "BS", "IS") & "' WHERE id = " & txtKode.Tag.ToString
    End If
    objComp.setExecute(str, False)

    master_ChartofAccount.showCoa() 'error on this line : Reference to a non-shared member requires an object reference

    objComp.msgShow(Me, "Data berhasil di simpan.", "Simpan Berhasil", MessageBoxButtons.OK, MessageBoxIcon.Information)
    cmdClose_Click(sender, e)
 End Sub

为什么会有错误消息:

对非共享成员的引用需要对象引用

master_ChartofAccount.showCoa()

如何解决这个问题? 还有其他方法可以从另一个Form 调用Sub 吗?

我试过了:

Dim frm as new master_ChartofAccount
frm.showCoa()

master_ChartofAccount 上的网格不会刷新。 谢谢

【问题讨论】:

标签: vb.net


【解决方案1】:

在 coa_Create 中,声明一个 master_ChartofAccount 表单类型的属性。

Public Property Master_ChartofAccount As <class name for your master_chartofaccount form>

然后在 cmdAdd_Click 中

Private Sub cmdAdd_Click(sender As Object, e As EventArgs) Handles cmdAdd.Click
    Dim frm As New coa_Create
    frm.Master_ChartofAccount = Me
    frm.ShowDialog(Me)
End Sub

在你的 cmdSave_Click 中

Me.MasterChartofAccount.showCoa()

在 cmdSave_Click 中,只需将父级强制转换为 mast_chartofaccount 的类类型

CType(Me.Parent, <class name of master chart of account>).showCoa()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    相关资源
    最近更新 更多