选择组合框时填充文本框

时间:2013-10-21 05:20:40

标签: vb.net combobox

我正在使用Visual Basic在Visual Studio 2013中处理一个应用程序,有一些东西我有点想要你可以帮助我...好吧所以我有我的用户使用一个组合框,这是一定的对于“客户名称”,访问中的表格可以很好地提取数据,但我想要写的是代码,因此当在下面的组合框选择中选择“客户名称”时,某些文本框会自动填写“客户”地址''客户电话号码''客户帐户名称'我有一个工作代码,但不幸的是它只适用于Int32我希望它能识别使用文本。有人可以帮忙吗?我将跟进我之前的代码作为帮助

谢谢James

    Private Sub cmdGetByIdentifier_Click(sender As Object, e As EventArgs) Handles cmdGetByIdentifier.Click
If Not String.IsNullOrWhiteSpace(txtIdentifier1.Text) Then
    Dim Identifier As Int32 = 0
    If Int32.TryParse(txtIdentifier1.Text, Identifier) Then
        txtCompanyName1.Text = GetCustomerNameByIdentifier(Identifier)
    Else
        MessageBox.Show("'" & txtIdentifier1.Text & "' is not a valid integer.")
    End If
Else
    MessageBox.Show("Must enter an identifier to get a company name.")
End If
 End Sub

 Private Sub cmdGetCustomer_Click(sender As Object, e As EventArgs) Handles cmdGetCustomer.Click
If Not String.IsNullOrWhiteSpace(txtIdentifier2.Text) Then
    Dim Identifier As Int32 = 0
    If Int32.TryParse(txtIdentifier2.Text, Identifier) Then
        Dim Cust As Customer = GetCustomer(Identifier)
        txtCompanyName2.Text = Cust.Name
        txtContactName2.Text = Cust.ContactName
    Else
        MessageBox.Show("'" & txtIdentifier1.Text & "' is not a valid integer.")
    End If
Else
    MessageBox.Show("Must enter an identifier to get a company name.")
End If

 End Sub
 End Class


    Module DatabaseOperations
    Public Function GetCustomerNameByIdentifier(ByVal Identifier As Int32) As String
Dim CompanyName As String = ""
Dim Builder As New OleDb.OleDbConnectionStringBuilder With {.Provider = "Microsoft.ACE.OLEDB.12.0", .DataSource = IO.Path.Combine(Application.StartupPath, "Database1.accdb")}
Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
    Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
        cmd.CommandText = "SELECT CompanyName FROM Customer WHERE Identifier =@P1"
        Dim NameParameter As New OleDb.OleDbParameter With {.DbType = DbType.Int32, .ParameterName = "P1", .Value = Identifier}
        cmd.Parameters.Add(NameParameter)
        cn.Open()
        CompanyName = CStr(cmd.ExecuteScalar)
    End Using
End Using
Return CompanyName
End Function
Public Function GetCustomer(ByVal Identifier As Int32) As Customer
Dim Customer As New Customer

 Dim CompanyName As String = ""
Dim Builder As New OleDb.OleDbConnectionStringBuilder With {.Provider = "Microsoft.ACE.OLEDB.12.0", .DataSource = IO.Path.Combine(Application.StartupPath, "Database1.accdb")}
Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
    Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
        cmd.CommandText = "SELECT Identifier, CompanyName,ContactName FROM Customer WHERE Identifier =@P1"
        Dim NameParameter As New OleDb.OleDbParameter With {.DbType = DbType.Int32, .ParameterName = "P1", .Value = Identifier}
        cmd.Parameters.Add(NameParameter)
        cn.Open()
        Dim Reader As OleDb.OleDbDataReader = cmd.ExecuteReader
        If Reader.HasRows Then
            Reader.Read()
            Customer.Identifier = Identifier
            Customer.Name = Reader.GetString(1)
            Customer.ContactName = Reader.GetString(2)
        End If
    End Using
End Using

Return Customer

End Function
End Module


Public Class Customer
Public Property Identifier As Int32
Public Property Name As String
Public Property ContactName As String
Public Sub New()
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

像这样:

Public Function GetCustomerNameByName(ByVal Name As String) As String
Dim CompanyName As String = ""
Dim Builder As New OleDb.OleDbConnectionStringBuilder With {.Provider = "Microsoft.ACE.OLEDB.12.0", .DataSource = IO.Path.Combine(Application.StartupPath, "Database1.accdb")}
Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
    Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
        cmd.CommandText = "SELECT CompanyName FROM Customer WHERE CustName =@P1"
        Dim NameParameter As New OleDb.OleDbParameter With {.DbType = DbType.String, .ParameterName = "P1", .Value = Name}
        cmd.Parameters.Add(NameParameter)
        cn.Open()
        CompanyName = CStr(cmd.ExecuteScalar)
    End Using
End Using
Return CompanyName
End Function