vb.net文本框没有替换值

时间:2013-10-24 10:44:28

标签: vb.net visual-studio-2010

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

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

frmDepartment

Dim customer As Object = frmMain.txtCustomer.Text

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

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

textbox txtDeptCustomer< --- NOT DISPLAYING VALUE

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

 Public customer as String

在您button click的{​​{1}}中,您传递的值如下:

frmMain

然后在加载frmDepartment.customer = txtCustomer.Text frmDepartment.Show() 时,您现在可以选择将frmDepartment分配给customer,如:

txtDeptCustomer