VB.net Windows窗体(使用Access数据库)在文本框中显示选定的组合框项目

时间:2012-11-09 18:08:07

标签: vb.net

你好所有这一切都有点紧急,因为这项任务将于11月11日星期日午夜到期

我附上了我需要的帮助,它选择了一个组合框项目,然后将数据显示在下面的文本框中

我真的不知道如何处理它我双击组合框,这就是我开始尝试只显示ID。我没有尝试过其他任何事情。我评论了我尝试过的东西,因为它不起作用。

Public Class AppointmentsForm
    Private aAppointments As New Appointments

    'Instance of customers
    Private aCustomers As New Customers


    Private Sub AppointmentsForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'Combobox must always have a DataSource, Display Member, and Value Member
        'Load the ComboBox with customer name

        cboCustomer.DataSource = aCustomers.Items
        cboCustomer.DisplayMember = "CustName"
        cboCustomer.ValueMember = "CustId"
        cboCustomer.SelectedIndex = -1   'no items selected


        ' load the datagridview
        ApptDataGridView.DataSource = aAppointments.Items

        'do not show TypeID

        ApptDataGridView.Columns(1).Visible = False
        '.Columns(1) TypeID has index of 1 as it is Column 2 in the data sources

    End Sub

    Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
        'make sure that a record is selected first
        If ApptDataGridView.SelectedRows.Count > 0 Then
            Dim apptid As Integer
            apptid = ApptDataGridView.SelectedRows(0).Cells(0).Value

            'Delete selected record by calling the delte function
            aAppointments.Delete(apptid)
        End If
    End Sub

    Private Sub btnEdit_Click(sender As System.Object, e As System.EventArgs) Handles btnEdit.Click
        'make sure row is selected get that row from the table ..... need to find by ID
        'This is a query which you will create in the class
        'Transfer information from that row to the form, display the form
        If ApptDataGridView.SelectedRows.Count > 0 Then
            modAppointmentsForm.ApptID = ApptDataGridView.SelectedRows(0).Cells(0).Value
            modAppointmentsForm.ShowDialog()
        End If
    End Sub
End Class

这是指定的类别:

公共课程约会

Public adapter As New CompanyDataSetTableAdapters.SalesStaffTableAdapter

'error variable
Public Shared Property LastError As String

Public ReadOnly Property Items() As DataTable
    Get
        Dim table As DataTable = adapter.GetData
        'sorted by Appointment id
        table.DefaultView.Sort = "ID"
        Return table
    End Get
End Property

'create a function to combine date and time
Public Shared Function CombinedDateTime(aDate As DateTime, aTime As DateTime) As DateTime
    'declare timespan variable
    Dim tsDate As New TimeSpan(aTime.Hour, aTime.Minute, 0)
    Return aDate.Add(tsDate)

End Function

这里是客户的类别:

公共类客户

'create a object variable (tableadapter)
'this will be used to creat instances of this class in the form

Private adapter As New CompanyDataSetTableAdapters.SalesStaffTableAdapter

'property to return a table using the GetData method
Public ReadOnly Property Items() As DataTable
    Get
        Dim table As DataTable = adapter.GetData
        table.DefaultView.Sort = "First_Name"
        Return table
    End Get
End Property

'create a function to filter the customers by custid
Public Function GetByCustomerId(custid As Short) As DataTable
    Dim table As DataTable = adapter.GetData 'entire table
    'filter to get desired row(s)
    table.DefaultView.RowFilter = " ID = " & custid
    Return table

End Function

结束班

这里是COMBOBOX和TEXTBOX表格的代码,如ID,LASTNAME,FIRSTNAME等。当选择COMBOBOX项目时,需要显示数据。我写的时候ID正在显示:IDTextBox.Text = SalesStaffComboBox.SelectedValue.ToString 但如果我为其他文本框做同样的事(LastName.Text = SalesStaffComboBox.SelectedValue.ToString _ ____ FirstName.Text = SalesStaffComboBox.SelectedValue.ToString)ID号显示在他们身上。

公共类frmUpdateSalary     '所需类的实例     私人客户作为新客户     私人任命为新任命

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    Me.Close()
End Sub


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

    SalesStaffComboBox.DataSource = aCustomers.Items
    SalesStaffComboBox.DisplayMember = "First_Name"
     SalesStaffComboBox.ValueMember = "ID"


End Sub

Private Sub SalesStaffComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalesStaffComboBox.SelectedIndexChanged
    'fill the textboxes
    'Populate ID textbox with selected customer


    IDTextBox.Text = SalesStaffComboBox.SelectedValue.ToString



    End Sub

2 个答案:

答案 0 :(得分:0)

从组合框,列表框或下拉列表中选择一个值。

 If cmbBox.selectedIndex <> -1 Then
     comboIndex = cmbBox.selectedIndex
 End Else

 cmbBox.Items.removeAt(x)

删除所选索引处的项目

 If cmbBox.selectedIndex <> -1 Then
     comboItem= cmbBox.SelectedItem()

没有VS10来检查这些是正确的方法名称还是大写但是它本质上是

答案 1 :(得分:0)

您的组合框每个项目有两个数据变量,值和显示 因此,对于每个客户,您都有姓名和他的ID(您没有指定它是最后一个名字还是第一个名字) 如果在文本框中你说:

txtID.Text=cbo.SelectedValue.ToString
txtLastName.Text=cbo.SelectedValue.ToString
txtFirstName.Text=cbo.SelectedValue.ToString

每个文本框都会得到相同的结果,因为左侧始终是相同的。 如果在您的Combobox中,显示值是LastName和FirstName的组合,您可以拥有:

txtID.Text=cbo.SelectedValue.ToString
txtName.Text=cbo.SelectedText.ToString

我在没有视觉工作室的情况下写作,对不起,如果我有任何小错误

您没有解释什么是客户,但我想客户的全部信息都在那里。因此,您可以使用您已拥有的ID检索数据 如果您提供更多信息,我们可以提供帮助。