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

VB.net Windows窗体(使用Access数据库)在文本框中显示选定的组合框项,vb.net,Vb.net,您好,所有这些都有点紧急,因为这项任务将于12月11日星期日午夜完成 我附上了我需要的帮助,它是选择一个组合框项目,然后获得数据显示在下面的文本框 我真的不知道如何处理它,我双击了组合框,这就是我开始尝试只是让ID显示。我没有尝试过其他任何事情。我把我试过的东西注释掉了,因为它不起作用 Public Class AppointmentsForm Private aAppointments As New Appointments 'Instance of customers

您好,所有这些都有点紧急,因为这项任务将于12月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
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
以下是针对客户的课程:

公共类客户

'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
末级

下面是带有组合框和文本框(如ID、LASTNAME、FIRSTNAME等)的表单的代码,选择组合框项时需要显示数据。当我写入时再次显示ID:IDTextBox.Text=SalesStaffComboBox.SelectedValue.ToString 但是如果我对其他文本框(LastName.TEXT=SalesStaffComboBox.SelectedValue.ToString\uuuuuuuuuFirstName.TEXT=SalesStaffComboBox.SelectedValue.ToString)执行相同的操作,ID号也会显示在这些文本框中

公共类FRMU更新工资 '所需类的实例 作为新客户的私人针灸师 私人委任为新委任

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

从组合框、列表框或下拉列表中选择值

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

 cmbBox.Items.removeAt(x)
如果cmbBox.selectedIndex-1,则
comboIndex=cmbBox.selectedIndex
结束其他
cmbBox.Items.removeAt(x)
删除选定索引处的项目的步骤

 If cmbBox.selectedIndex <> -1 Then
     comboItem= cmbBox.SelectedItem()
如果cmbBox.selectedIndex-1,则
comboItem=cmbBox.SelectedItem()

如果不使用VS10来检查这些是否是正确的方法名称或上限,但本质上它是正确的。您的组合框中的每个项目都有两个数据变量,即值和显示。
因此,对于每个客户,您都有其姓名和ID(您不需要指定是姓还是名)。
如果在其文本框中,您说:

txtID.Text=cbo.SelectedValue.ToString
txtLastName.Text=cbo.SelectedValue.ToString
txtFirstName.Text=cbo.SelectedValue.ToString
对于每个文本框,您将得到相同的结果,因为左侧总是相同的。 如果组合框中的显示值是LastName和FirstName的组合,则可以使用:

txtID.Text=cbo.SelectedValue.ToString
txtName.Text=cbo.SelectedText.ToString
我在没有visual studio的情况下写作,因此,如果我有任何小错误,请原谅

你没有解释什么是aCustomers,但我认为客户的全部信息都在那里。因此,您可以使用已有的id检索数据。

如果您提供更多信息,我们可以提供帮助。

我不知道您在问什么。数据是否显示在cboCustomer中?好的,在您的对象客户中,您是否可以激发字段id和名称?像comboSelectedItem.Name或comboSelectedItem.ID?请您将填充aCustomers的代码和文本框的代码显示为代码。我们需要了解aCustomers中的内容,以及您将哪些内容作为值发送给TextBoxes。如果您添加代码来编辑您的问题,这会更好,因为像注释一样,它没有意义。您尚未显示为文本框的代码。我脑子里还不清楚你装的是什么。但无论如何,你拥有所有的数据。既然你得到了身份证,你就可以找到名字和姓氏。这些信息存储在哪里?哈布夫对不起,我对Stack Overflow和VB真的很陌生,所以我满怀希望地用所有合适的代码编辑了这个问题,希望你能看到,所有的大写字母不是我在喊对不起。如果可以,请帮助我。因此,编辑后:由于您将相同的值应用于文本框,它们将具有相同的值(id)。据我所知,您在属性项中获得了所有客户。因此,从combobox中可以获得客户的id。现在转到datatable项并找到具有该ID的客户。检索该行,然后将姓氏和名字的值应用于文本框。如果你只想要名字,那么你可以从combobox1.SelectedText获得它。你能举个例子说明如何做到这一点吗?我明白你的意思,因为现在每个文本框中都有相同的字符串。我们班从来没有教过我如何“检索行,然后将姓氏和名字的值应用到文本框中。”这就是我对本课程感到沮丧的原因,也是为什么我感到困难的原因。我们的家庭作业与我们在课堂上所做的大不相同。