Listbox DisplayMember from nested object VB.net - vb.net

I have a class called ComponentTransactionPair, it houses two objects called m_Component and m_Transaction, there is a public property
Protected m_componentTransactionPair As List(Of ComponentTransactionPair)
In trying to hook this to a ListBox I am using the code below
lstbCurrentTransactions.DataSource = m_componentTransactionPair
m_currentOptionsLoaded = True
lstbCurrentTransactions.DisplayMember = "m_Transaction.Description"
The DisplayMember is not working properly for me in doing this and i'm pretty sure it's displaying the type rather than the Description property within the Transaction.
Is it even possible to use a nested object's property to get this category here because everything I've seen does not use a nested object.

Fixed this by just adding another property to the class and took it from the value I needed. Worked perfectly afterwards. If there's a less hacky solution i'd be interested to hear it though.

Related

Using List(Of Interface) rather than a Collection doesn't populate DataGridView properly

I have a screen which can display three sets of data, from three different classes. However, it should only display one at a time depending on the mode it is set to upon initialization.
Naturally, these three classes (I have only written one so far) implement an interface (IExportSelectionStructure) which defines their shared property: Selected. There are no other properties these classes share which can be put in the interface.
The screen has the following code:
Private _BaseStructure As IExportStructure
Private _DataSet As List(Of IExportSelectionStructure)
Public Sub New(exportMode As ExportRunMode)
'BaseStructure setup goes here
_DataSet = _BaseStructure.GetDataSet(_Session, _Settings)
With ExportDataGridView
.AutoGenerateColumns = False
.Columns.AddRange(_BaseStructure.GenerateExporterColumns.ToArray)
.Datasource = _DataSet
End With
End Sub
I'm running in Customer export mode.
At this point,_DataSet has a three ExportCustomerSelectionStructure classes inside, along with the expected data inside each class.
However, when it gets set as the datasource for the gridview, the gridview displays three records, but the columns are empty, except the "Selected" column - which is a CheckBoxCell.
In terms of debugging this, I tried switching _DataSet to a Collection, and also got IExportStructure.GetDataSet to return a Collection - lo and behold, the data appears on screen absolutely fine, but I don't want to use a Collection, I want to use a List(Of IExportSelectionStructure). I can't go any more specific than the Interface, and I don't really want to go any more generic than that.
What am I doing wrong, and have I missed anything obvious?
Okay, so this was a strange one.
After struggling for two days with this I realised I had to cast the list of generics back to the specific class I am working with dependent on the mode of the screen, otherwise the DataGridView doesn't know what class of data you're working with! As such, the code around this for me is:
_DataSet = _BaseStructure.GetDataSet(_Session, _Settings)
Select Case _ExportRunMode
Case ExportRunMode.Customers
ExportDataGridView.Datasource = _DataSet.Cast(Of ExportCustomerSelectionStructure).ToList
'Etc...
End Case

Is it possible to use a userform property to remove a item in a dictionnary?

So I am trying to remove an item in a dictionary with a dictionary key via a property in a userform. So far, I have succeed to add a new item in dictionary, count item in the dictionary and get an item with userform property. So all of this is functional but I can't get the remove (key) functional.
Here is the property in my userform. (maybe I should use a get property?):
Public Property Let Materialremove(ByVal PartNumber As String)
DicMaterial.Remove (PartNumber)
End Property
Here is how I call for my property:
If UBidStatus.Materialexists(PartNumber) Then
UBidStatus.Materialremove (PartNumber)
End If
I get a Compile error:
Invalid use of property in VBA
I agree with Doug Glancy, I think you should turn it into a function. I don't think your approach is a good one. The reason you are having problems is because you are trying to treat a property like a function. You can make the code work this way: UBidStatus.Materialremove = PartNumber but I strongly advise against that for the sake of the next developer who has to look at the code after you and figure out what it's saying because what you are trying to do makes no logical sense. You should turn the property into a function. You need the assignment operator to make the line work because that is the purpose of a Let property statement: to assign a value to a property. But since you are trying to force a property to act like a function, it doesn't make any sense when someone tries to read the code.

Getting the Index of an Added ListView.Item

So all I am trying to do is get the index of an Item Programmatically added to a ListView from a Database.
Example:
Index = lvwNames.Items.Add(Player)
Player is a Class that uses Get/Set methods for ID, FirstName, and, LastName.
I have googled my heart out to no avail.
EDIT
So I had a GetInformation() procedure that populated my Player Class. So I was helped by a personal friend, I just needed to pass in my ListViewItem ByRef into there and I can grab the data I needed (SUCH A DUMMY!). Still having an issue with the Index. I would like to have the newly added item selected after it is added.
Getting the Index of an Added ListView.Item as per the title.
Method A: Look at the count
lvwNames.Items.Add(Player.ToString())
Dim ndx = lvwNames.Items.Count-1
Method B: Use an object
Dim lvi As New ListViewItem(Player.ToString())
lvwNames.Items.Add(lvi)
Dim ndx = lvi.Index
However, Player is an Object and there is no matching Add overload, with Option Strict (which all good code should use). The best you can do is pass it as ToString() as shown.
The result will be something like WindowsApplication1.Player unless your class overrides ToString(). In no case will the ListView parse it to put the properties ("Get/Set methods") as sub items which sort of sounds like what you expect.
For that, a DatagridView would be a better choice using a List(Of Player). Each property could be mapped to its own column.

Add Property to Properties Window

I have built a UserControl class and am exposing certain properties to the parent object.
When i drag and drop the component to the parent objects designer surface, i get the effect i need (per say). What i want to further develop is the ability to pre-fill the property value (in properties window) as a default but it isnt auto-populating as i would have expected.
Here is what i have so far:
<Browsable(True), Category("Data"),
DefaultValue("01/01/1990")>
Public Property [Date] As String
Get
Return Me._dt
End Get
Set(value As String)
Me._dt = value
dtValue.Text = value
End Set
End Property
I understand that DefaultValueAttribute assigns the value if no other assignments are made, but thought it would also place that value in the field in the Properties Window.
As well, this ultimate assignment will go up one more level as a collection, so any advise or URL's for how to make a Collection of Components available via Properties Window?
I understand that DefaultValueAttribute assigns the value if no other assignments are made
That is incorrect.
The DefaultValueAttribute is just metadata that tells the designer what the default is.
It is still up to your code to ensure that the property actually gets that value.

VBA object's Collection property contains data but won't let me access it (get EOF/BOF error)

I have a class module in an Excel project that has a property called Marks, this is a VB Collection and has a public get property (but no set or let).
I can assign values to this without any problem:
myObject.Marks.Add 3.14159
However, when I try to do something with this object (e.g., iterating through it), I get an error:
3021: Either BOF or EOF is true or the current record has been deleted
However, if I try myObject.Marks.Count, it shows that the collection contains the amount of data that I was expecting... I just can't access it!
I don't really understand why. I am using the same process with other collection properties within the object -- even collections of collections -- and they're working fine.
Any ideas?
myObject.myCollection.Add recordset!field adds the recordset field object to the myCollection object, rather than its value. As such, simply casting the field to its appropriate type solves the problem.