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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13313588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:05:44  来源:igfitidea点击:

VB.net Windows form (Working with Access Database) displaying a selected combobox item in a textbox

vb.net

提问by Josh Buckley

Hello All this is somewhat urgent as this assignment is due Sunday 11/12 at midnight

你好,这一切都有些紧迫,因为这项任务将于 2012 年 11 月 12 日星期日午夜到期

I have attached what I need help with it is selecting a combobox item then getting the data to display in the textboxes below

我附上了我需要帮助的内容是选择一个组合框项目然后获取数据以显示在下面的文本框中

I really don't know how to approach it I double clicked the combobox and this is what I started with to try to just get the ID to display. I haven't attempted anything else. I commented out what I tried because it didn't work.

我真的不知道如何处理它,我双击了组合框,这就是我开始尝试仅显示 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

HERE IS THE CLASS FOR APPOINTMENTS:

这是预约课程:

Public Class Appointments

公开课预约

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

HERE IS THE CLASS FOR CUSTOMERS:

这是给客户的课程:

Public Class Customers

公开课客户

'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

End Class

结束班

HERE IS THE CODE FOR THE FORM WITH THE COMBOBOX AND THE TEXTBOX'S SUCH AS ID, LASTNAME, FIRSTNAME, ETC. THAT NEED TO HAVE THE DATA DISPLAYED WHEN THE COMBOBOX ITEM IS SELECTED. AGAIN THE ID IS DISPLAYING WHEN I WRITE: IDTextBox.Text = SalesStaffComboBox.SelectedValue.ToString BUT IF I DO THE SAME FOR THE OTHER TEXT BOXES (LastName.Text = SalesStaffComboBox.SelectedValue.ToString _____FirstName.Text = SalesStaffComboBox.SelectedValue.ToString ) THE ID NUMBER IS DISPLAYED IN THEM AS WELL.

这是带有组合框和文本框的表单代码,例如 ID、姓氏、名字等。选择组合框项目时需要显示数据。当我写的时候,ID 再次显示: IDTextBox.Text = SalesStaffComboBox.SelectedValue.ToString 但如果我对其他文本框做同样的事情(LastName.Text = SalesStaffComboBox.SelectedValue.ToString _____FirstName.Text = SalesStaffComboBox.SelectedValueToString ) 身号码也显示在他们身上。

Public Class frmUpdateSalary 'instances of the required classes Private aCustomers As New Customers Private aAppointments As New Appointments

Public Class frmUpdateSalary '所需类的实例 Private aCustomers As New Customers Private aAppointments As New Appointments

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

回答by DWolf

To select a value form a combobox, listbox or dropdownlist.

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

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

 cmbBox.Items.removeAt(x)

to remove the item at the selected index

删除所选索引处的项目

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

Without VS10 to check if those are the correct method names or caps But its essentially it

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

回答by Nianios

Your combobox has two data variables for each item, the value and the display.
So for each customer you have the name and his ID (You don't specify if it is the last or the first name).
If in its textbox you say:

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

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

You will get the same result for each textbox, since the left side is always the same. If in your Combobox the Display Value is a combination of LastName and FirstName you can have:

对于每个文本框,您将获得相同的结果,因为左侧总是相同的。如果在您的组合框中显示值是姓氏和名字的组合,您可以拥有:

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

I am writing without visual studio so, sorry if I have any minor errors

我在没有 Visual Studio 的情况下写作,所以如果我有任何小错误,抱歉

You don't explain what is aCustomers but I imagine that the whole information for the customers are there. So you can retrieve the data with the id that you already have.
If you give as more info we can help.

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