传递变量不起作用

时间:2017-08-10 13:30:05

标签: vb.net global-variables parameter-passing

我问过在表单之间传递变量并得到答案。我用一个简单的形式测试它们并且它起作用了。我创建了一个全局变量类和一个公共变量,它似乎工作。但是当我在我的实际程序中尝试同样的事情时。它不起作用,我无法弄清楚原因。

基本上我有一个显示库存的表格。每个项目都有一个vatid字段。当我点击一个按钮,我希望它打开大桶形式,我通过从数据网格中选择一个不同的大桶ID。然后当我点击一个选择按钮时,vat id必须传回已经打开的库存屏幕并更新该项目的vatid文本框。

view inventory screen

在这个屏幕上我想点击vatid旁边的按钮,它必须打开一个大桶形式,我从中选择一个大桶类型

此屏幕的代码在这里

    Imports MySql.Data.MySqlClient

Public Class frmviewinventory
    Dim mysqlconn As MySqlConnection
    Dim command As MySqlCommand
    Dim dbdataset As New DataTable
    'Public recvatid As Integer

    Private Sub load_table() 'routine to refresh database and reload datagrid

        mysqlconn = New MySqlConnection
        mysqlconn.ConnectionString = "server=localhost;userid=root;password=1234;database=fabpos"
        Dim reader As MySqlDataReader
        Dim sda As New MySqlDataAdapter
        ' Dim dbdataset As New DataTable
        Dim bsource As New BindingSource


        Try
            mysqlconn.Open()
            Dim query As String

            query = "select * from fabpos.inventory"
            command = New MySqlCommand(query, mysqlconn)
            sda.SelectCommand = command
            sda.Fill(dbdataset)
            bsource.DataSource = dbdataset
            dginventory.DataSource = bsource
            sda.Update(dbdataset)
            reader = command.ExecuteReader

            mysqlconn.Close()

        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            mysqlconn.Dispose()

        End Try
    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnloadgrid.Click
        dbdataset.Clear()
        load_table()

    End Sub

    Private Sub btnclose_Click(sender As System.Object, e As System.EventArgs) Handles btnclose.Click

        Me.Close()

    End Sub

    Private Sub frmviewinventory_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        optdescription.Checked = True
        txtsearch.Select()
        load_table()

        dginventory.Columns.Item(0).Width = 50
        dginventory.Columns.Item(1).Width = 50
        dginventory.Columns.Item(2).Width = 90
        dginventory.Columns.Item(3).Width = 300

    End Sub

    Private Sub txtsearch_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtsearch.TextChanged

        Dim dv As New DataView(dbdataset)

        If optstockcode.Checked = True Then

            dv.RowFilter = String.Format("stockcode like '%{0}%'", txtsearch.Text)
            dginventory.DataSource = dv

        ElseIf optbarcode.Checked = True Then

            dv.RowFilter = String.Format("barcode like '%{0}%'", txtsearch.Text)
            dginventory.DataSource = dv

        ElseIf optdescription.Checked = True Then

            dv.RowFilter = String.Format("description like '%{0}%'", txtsearch.Text)
            dginventory.DataSource = dv

        ElseIf optbin.Checked = True Then

            dv.RowFilter = String.Format("binlocation like '%{0}%'", txtsearch.Text)
            dginventory.DataSource = dv
        End If

        'dv.RowFilter = String.Format("stockcode like '%{0}%'", txtsearch.Text)
        'dginventory.DataSource = dv

    End Sub

    Private Sub optstockcode_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles optstockcode.CheckedChanged
        txtsearch.Select()
    End Sub

    Private Sub optbarcode_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles optbarcode.CheckedChanged
        txtsearch.Select()
    End Sub

    Private Sub optdescription_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles optdescription.CheckedChanged
        txtsearch.Select()
    End Sub

    Private Sub optbin_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles optbin.CheckedChanged
        txtsearch.Select()
    End Sub
    Private Sub frmviewinventory_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

        If (e.KeyCode = Keys.Escape) Then
            Me.Close()
        End If

    End Sub
    Private Sub dginventory_SelectionChanged(sender As Object, e As EventArgs) Handles dginventory.SelectionChanged

        Dim i As Integer
        i = dginventory.CurrentRow.Index
        Me.txtid.Text = dginventory.Item(0, i).Value.ToString
        Me.txtstockcode.Text = dginventory.Item(1, i).Value.ToString
        Me.txtbarcode.Text = dginventory.Item(2, i).Value.ToString
        Me.txtdescription.Text = dginventory.Item(3, i).Value.ToString
        Me.txtrecname.Text = dginventory.Item(4, i).Value.ToString
        Me.txtvatid.Text = dginventory.Item(5, i).Value.ToString
        Me.txtcostexcl.Text = dginventory.Item(6, i).Value.ToString
        Me.txtvatamt.Text = dginventory.Item(7, i).Value.ToString
        Me.txtcostincl.Text = dginventory.Item(8, i).Value.ToString
        Me.txtmu.Text = dginventory.Item(9, i).Value.ToString
        Me.txtspexcl.Text = dginventory.Item(10, i).Value.ToString
        Me.txtspincl.Text = dginventory.Item(11, i).Value.ToString
        Me.txtprofit.Text = dginventory.Item(12, i).Value.ToString
        Me.txtgp.Text = dginventory.Item(13, i).Value.ToString

    End Sub

    Private Sub btngetvat_Click(sender As Object, e As EventArgs) Handles btngetvat.Click

        Dim getvat As New frmvat
        getvat.Show()
        getvat.btnselect.Visible = True

    End Sub

End Class

增值税屏幕在这里 vat form 应该将全局变量传递回库存屏幕的代码

 Private Sub dgvat_SelectionChanged(sender As Object, e As EventArgs) Handles dgvat.SelectionChanged

    Dim i As Integer
    i = dgvat.CurrentRow.Index
    Me.txtvatid.Text = dgvat.Item(0, i).Value.ToString
    Me.txtvatcode.Text = dgvat.Item(1, i).Value.ToString
    Me.txtvatdescription.Text = dgvat.Item(2, i).Value.ToString
    Me.txtvatpercentage.Text = dgvat.Item(3, i).Value.ToString
    globalvariables.vatidvariable = Convert.ToInt32(txtvatid.Text)


End Sub

Private Sub btnselect_Click(sender As Object, e As EventArgs) Handles btnselect.Click

    globalvariables.vatidvariable = Convert.ToInt32(txtvatid.Text)
    frmviewinventory.txtvatid.Text = globalvariables.vatidvariable
    Me.Close()

End Sub

我的公共课在这里

Public Class globalvariables

Public Shared vatidvariable As String
Public Shared testvariable As String


End Class

我在一个只有2个表格和文本框的简单应用程序上试过这个,它完美无缺。但在我的实际程序中没有。而vatid仍然保持不变。

我检查了文本bosex的readonly属性和启用属性,它们都很好。我是灰色的。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

不要使用全局变量。那很狡猾。对话应该通过属性或方法提供适当的数据,并且调用者应该从方法的属性中检索数据。例如,假设Form1显示Form2,用户会在TextBox上的Form2输入一些文字,然后该文字应显示在Label中在Form1

Public Class Form2

    Public ReadOnly Property TextBoxText As String
        Get
            Return TextBox1.Text
        End Get
    End Property

End Class


Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Using dialogue As New Form2
            If dialogue.ShowDialog() = DialogResult.OK Then
                Label1.Text = dialogue.TextBoxText
            End If
        End Using
    End Sub

End Class

该属性可以显示您想要的任何数据。它不必直接是控件的属性。另请注意,此代码不包含对话框返回OKCancel的机制,该机制不直接相关。

这不会直接回答你的问题,但它提供了一个更好的机制,我实际上无法在评论中包含所有代码。

答案 1 :(得分:1)

您的以下方法是错误地设置变量

Private Sub btnselect_Click(sender As Object, e As EventArgs) Handles btnselect.Click

    globalvariables.vatidvariable = Convert.ToInt32(txtvatid.Text)
    '**** The following code line is misleading you
    frmviewinventory.txtvatid.Text = globalvariables.vatidvariable
    Me.Close()

End Sub

在这里,您要将值设置为此表单的新实例,而不是打开您的增值表单的实例。您可以在globalvariables.vatidvariable代码后直接访问查看广告资源屏幕上的getvat.Show()。更恰当地用以下

替换getvat.Show()
 If getvat.ShowDialog() = DialogResult.OK Then
     txtvatid.Text = globalvariables.vatidvariable
 End If

希望它对你有用。 没有全局变量可以做得更好。