简体   繁体   中英

Question related to vb.net: I need help in code

I have to develop an application in which I have to store data of a customer like name,fathername, address in array of objects.

Customer names should also be stored in combo box after writing in textbox and after that select name from combo box data against that name should be generated?

Public Class Customer

  Public Sub btn_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_add.Click
      Dim obj(5) As Object

      obj(0) = txt_name.Text
      obj(1) = txt_fname.Text
      obj(2) = txt_dob.Text
      obj(3) = txt_address.Text
      obj(4) = txt_nic.Text
      cmb_list.Items.Add(obj(0))
      cmb_list.ItemData(cmb_list.SelectedIndex) = 60

      txt_name.Text = ""

  End Sub

  Private Sub cmb_list_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_list.SelectedIndexChanged

    Dim a As String

    a = cmb_list.Text
    TextBox1.Text = a

  End Sub

End Class

I have done this only and need help to proceed.

Can I ask why you need to use an array of objects like that? I think this would be a lot easier if you created a Customer class, like this (for VS 2010, will have to look a bit different in 2008):

Public Class Customer
  Public Property Name as String
  Public Property FName as String
  Public Property Address as String
  Public Property DOB as String
  Public Property Nic as String
End Class

Your form would then look like this:

Public Class CustomerForm

  Public Sub btn_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_add.Click
      Dim customers as new List(of Customer)
      Dim c as new Customer

      c.Name = txt_name.Text
      c.FName = txt_fname.Text
      c.DOB = txt_dob.Text
      c.Address = txt_address.Text
      c.Nic = txt_nic.Text
      customers.Add(c)
      cmb_list.DisplayMember = "FName"
      cmb_list.DataSource = customers

   End Sub

   Private Sub cmb_list_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_list.SelectedIndexChanged

    Dim c As Customer

    c = cmb_list.SelectedItem
    TextBox1.Text = c.FName

  End Sub

End Class

What that does is create a list of Customer objects, puts a customer into the list, and then uses the list to populate the drop down box. When you pick one, it gets that specific customer back out, and gets their first name.

So you can make the list longer ( have more customers) by adding more of them to the customers list and then binding it again.

(1) Store data of a customer in an instance of Class Customer.

(2) Store data of all customers in a List or Array of Type of Customer.

(3) Create Customer instance with data in textboxes (not combo).

(4) Display (not store) Customer names in combo box, and other information in textboxes.

(5) Modify customer information with combo selection and textboxes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM