2013-10-24 50 views
0

在我的frmMain類中,我有一個從數據庫填充的文本框(txtCustomer)。我想將此值傳遞給frmDepartment(txtDeptCustomer)中的另一個文本框。vb.net textbox不替換值

我無法看到爲什麼我使用的代碼沒有在txtDeptCustomer中顯示值的邏輯。我可以使用變量查詢數據庫,因此字符串正在傳遞,但只是不顯示在txtDeptCustomer中。如果有人能指出我的錯誤,我將不勝感激。由於

frmDepartment

Dim customer As Object = frmMain.txtCustomer.Text 

這是傳遞正確的值分貝。

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

文本txtDeptCustomer < ---不顯示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 
+0

在哪裏聲明客戶變量?爲什麼每次使用這個變量都需要一個字符串時使用一個對象? – Steve

+0

是否顯示任何錯誤? txtDeptCustomer是否是公共的?你從'frmMain'調用'frrmDepartment'嗎? – Edper

+0

@Edper我放入試試看,沒有錯誤顯示。謝謝 – user1532468

回答

1

您必須聲明,然後爲您public變量customerfrmDepartment,如:

Public customer as String 

而在button clickfrmMain你傳遞的值如下:

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 
+0

非常好。非常感謝 – user1532468

+0

不客氣@ user1532468。上帝保佑。 – Edper

+0

謝謝。你也是 – user1532468

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