List, Binding Lists, classes Help Please

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
Hi all, This is quit a long explantion i think :S to simplfy this down im going to mention things i THINK are important if you would like more information please let me know what you want to know

Background Info: i Have an application that deals with cars, the most cars it will deal with at once is 10.
i have 3 comboboxes and only really need 2 i think
1 combobox is in an area that is for viewing the details of the car. my plan is for the user to select the car form the combobox list and the car info will be passed into a few labels
another combobox is for deleteing a car incase the user makes a mistake
the 3rd combobox was used when i was using bindinglists and delegates for my application to know what car to work with.

What I Would Like If Possable :
Be able to create an object from my class without declairing it manually in my code and it being named accordanly eg car1, car2,car3 and so on. or have it being added to a list that can be bound to comboboxsHave The Same list Bound to 2 comboboxes eaching combobox calling a diffrent sub

My Code:
My 'Car Class'
VB.NET:
Public Class car
    Public Property Name As String
    Public Make As String
    Public Model As String
    Public Colour As String
    Public Reg As String
    Public damage() As String
    Public Subtotal As short
    Public Discount As byte
    Public additional As byte
    Public Total As short
    Public finished As Boolean = False
End Class

The Sub that uses the car class
VB.NET:
    Public Sub ViewCar(SelectCar As car)
        Form_Invoice_Management.lbl_Car_Make.Text = SelectCar.Make
        Form_Invoice_Management.lbl_Car_Model.Text = SelectCar.Model
        Form_Invoice_Management.lbl_Car_Colour.Text = SelectCar.Colour
        Form_Invoice_Management.lbl_Car_Reg.Text = SelectCar.Reg
        Form_Invoice_Management.lbl_Subtotal.Text = SelectCar.Subtotal
        For i = 0 To (SelectCar.damage.Count - 1)
            Form_Invoice_Management.ListBox_car.Items.Add(SelectCar.damage(i))
        Next
        If SelectCar.Discount <> 0 Then
            Form_Invoice_Management.lbl_Discount.Text = SelectCar.Discount
            Form_Invoice_Management.lbl_Dis_add.Text = "Discount"
        ElseIf SelectCar.additional <> 0 Then
            Form_Invoice_Management.lbl_Discount.Text = SelectCar.additional
            Form_Invoice_Management.lbl_Dis_add.Text = "Additional"
        Else
            Form_Invoice_Management.lbl_Dis_add.Text = "Discount"
        End If
        Form_Invoice_Management.lbl_Total.Text = SelectCar.Total
    End Sub
i was hoping to be able to do something like this
VB.NET:
me.ViewCar(Combobox_Car_View.SelectedItem)
but My Main Issue at the moment is i dont know how to fill a combobox with a list of items i have tried the following

VB.NET:
Dim Carselect As BindingList(Of car) = New BindingList(Of car)
but usng this as a datasource for a combobox is giving an error
(operator = is not defined for types Object and BindingList(Of car)) something along those lines

i tried putting the viewcar sub inside the car class and using a CallByName but again i could add the object into the list for it to send to the sub.

i hope i have explained enough any other info i have missed just ask

Thanks for any help in advance
 
Operator '=' is not defined for types 'Object' and 'System.ComponentModel.BindingList(Of Image_Invoice_Management.car)'.

is the exact the error when appliying it to the combobox
VB.NET:
 Carselect.Add(New car With {.Name = "Car 1"})
        With ComboBox_Current_Car
            .DisplayMember = "Name"
            .ValueMember =
            [COLOR=#0000ff].DataSource = Carselect[/COLOR] 'error happens here
        End With

i didn't specify the error as i am asking for advice on which is the best way to achive what i want, im not looking people to give me exact code just what they think is best so i can learn how to apply it
 
It would appear that the actual issue is that you aren't assigning anything to the ValueMember. As such, the compiler is interpreting your code like this:
VB.NET:
 Carselect.Add(New car With {.Name = "Car 1"})
        With ComboBox_Current_Car
            .DisplayMember = "Name"
            .ValueMember = (.DataSource = Carselect)
        End With
It thinks that you are comparing the DataSource and Carselect for equality to produce a Boolean and then assigning that Boolean to the ValueMember. That would only happen in VB 2010 because it supports implicit line continuation. If you want to assign a value to the ValueMember then do so, otherwise get rid of that line.
 
thanks you that sorts that problem, now we come across another :D

how would i go about adding items into my bindinglist without doing it in code for example

user clicks a button and it adds a new object as my car class then names it for me to use? i dont know what this would be called so i cant just google it :(
 
thanks johnH i decided im going to manually add the items to the list as i have better control over when they are added and slightly easier for error handling as i have a better idea of whats going on.

i have a new quesion thought its still about list so il put it here

i have a subroutine thats takes a list
VB.NET:
Public Sub Create_Invoice(cars As List(Of car))
        Dim oWord As Word.Application
        Dim oDoc As Word.Document
        If System.IO.File.Exists(Application.StartupPath & "\Templates\invoice.dotx") Then
            oWord = CreateObject("Word.Application")
            oWord.Visible = True
            oDoc = oWord.Documents.Add(Application.StartupPath & "\Templates\invoice.dotx")
            oDoc.Bookmarks.Item("CustomerName").Range.Text = customer_Name
            oDoc.Bookmarks.Item("AddressLine1").Range.Text = Address_Line1
            oDoc.Bookmarks.Item("AddressLine2").Range.Text = Address_Line2
            oDoc.Bookmarks.Item("Postcode").Range.Text = Postcode
            oDoc.Bookmarks.Item("PhoneNumber").Range.Text = phoneNumber
            oDoc.Bookmarks.Item("Invoice_Number").Range.Text = Invoice_Name
            oDoc.Bookmarks.Item("Date").Range.Text = Format(System.DateTime.Today, "dd / MM / yyyy")
            For Each car In cars
                Dim i As Integer = 2
                If car.finished = True Then
                    oDoc.Tables(1).Cell(i, 1).Range.Text = car.Model
                    oDoc.Tables(1).Cell(i, 2).Range.Text = car.Colour
                    oDoc.Tables(1).Cell(i, 3).Range.Text = car.Reg
                    For i = 0 To car.damage.Count - 1
                        oDoc.Tables(1).Cell(i, 4).Range.Text &= car.damage(i)
                    Next
                    oDoc.Tables(1).Cell(i, 4).Range.Text &= ""
                    oDoc.Tables(1).Cell(i, 5).Range.Text = car.Total
                    Total_Discount += car.Discount
                    Total_Subtotal += car.Total
                    oDoc.Tables(1).Rows.Last.Range.Rows.Add()
                    i += 1
                End If
            Next

            MsgBox("The Invoice Template (Invoice.dotx) is NOT in the Installtion Directory" & Environment.NewLine & Application.StartupPath & "\Temlates\", MsgBoxStyle.Critical, "File Missing")
            Call Invoice_Download()
        End If
    End Sub

but how do i pass my list to the subroutine
VB.NET:
Call Create_Invoice(Carselect)
 
sorry figured it out, was trying to convert a bindinglist to a list(of t) but changed both to binding list and getting the desired effects
 
sorry figured it out, was trying to convert a bindinglist to a list(of t) but changed both to binding list and getting the desired effects
You can create one from the other using the constructor also, if you need that.
 
Back
Top