0

在我的 frmMain 类中,我有一个从数据库填充的文本框(txtCustomer)。我想将此值传递给 frmDepartment(txtDeptCustomer) 中的另一个文本框。

我看不到为什么我使用的代码没有在 txtDeptCustomer 中显示值的逻辑。我可以用变量查询数据库,所以字符串正在传递,但只是没有显示在 txtDeptCustomer 中。如果有人能指出我的错误,我将不胜感激。谢谢

部门

Dim customer As Object = frmMain.txtCustomer.Text

这是将正确的值传递给 db。

sql = "SELECT * FROM Departments where Customer = '" & CType(customer, String) & "'"

文本框 txtDeptCustomer <--- 不显示值

Private Sub txtDeptCustomer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDeptCustomer.TextChanged
        txtDeptCustomer.Text = CType(customer, String)
    End Sub
4

2 回答 2

2
Public Customer as String = Nothing

    Private Sub btnDO_Click(sender As Object, e As EventArgs) Handles btnDoWork.Click
        Customer = Database Call
        Dim frmDepartmentInstance as new frmDepartment
        frmDepartment.ShowDialog(Me)
    End Sub

然后在 frmDepartment 的 Load 事件中你可以说

txtDeptCustomer.Text = frmMain.Customer

概念证明:新项目。两种形式 | 表格 1 有一个按钮和一个文本框 | Form2 只有文本框

Public Class Form1
    Public Test As String = Nothing
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Test = TextBox1.Text
        Dim frm2 As New Form2
        frm2.ShowDialog(Me)
    End Sub
End Class

Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    TextBox1.Text = Form1.Test
End Sub
End Class
于 2013-10-24T11:39:52.140 回答
1

您必须在 中声明 then 作为public您的customer变量frmDepartment,例如:

 Public customer as String

button click你的frmMain你传递的值如下:

frmDepartment.customer = txtCustomer.Text
frmDepartment.Show()

然后在加载你的frmDepartment你现在可以分配customertxtDeptCustomer喜欢的选项:

Private Sub frmDepartment_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    txtDepartment.Text = customer
End Sub
于 2013-10-24T11:28:54.523 回答