I am absolutely new to VB.NET, i’ve done some vbS a long time ago.
Now I am trying to make a MVP kinda system and I am trying to define and load an array, through the system. Then read it out and it should populate a ListView1 object.
Can anyone shine some light on why it doesn’t populate. I know they are set correct, because the console tells me it’s set ok.
I know the sample would fill with the same item (a week to be exact) every time i add an item, but that’s not the point.
Also, is it possible to define the array without the index, i mean now i set it with (6) so it sets 6 blocks for strings, but I would like to be more flexible.
Is there a way like in php for example
$item[“firstname”]
$item[“lastname”]
…
thanks you for your time and effort! 🙂
ClientlistItem.vb (definition of the actual item)
Public Class ClientlistItem
Private entry(6) As String
Public Sub New(ByVal iEntry() As String)
entry(0) = iEntry(0)
entry(1) = iEntry(1)
entry(2) = iEntry(2)
entry(3) = iEntry(3)
entry(4) = iEntry(4)
entry(5) = iEntry(5)
End Sub
End Class
ClientList.vb (The model as it were)
Imports System.Collections.Generic
Public Class Clientlist
Public Event ClientlistChanged()
Private mItems As List(Of ClientlistItem) = New List(Of ClientlistItem)
Public ReadOnly Property Items() As List(Of ClientlistItem)
Get
Return mItems
End Get
End Property
Public Sub AddItem(ByRef iEntry() As String)
Console.WriteLine(iEntry(0))
Dim item As ClientlistItem = New ClientlistItem(iEntry)
mItems.Add(item)
RaiseEvent ClientlistChanged()
End Sub
End Class
ClientlistPresenter.vb (the presenter)
Public Class ClientlistPresenter
Private iEntry() As String
Private mClientlistModel As Clientlist
Private mClientlistView As ClientlistView
Public Sub New(ByRef view As ClientlistView)
mClientlistModel = New Clientlist
mClientlistView = view
mClientlistView.Init(mClientlistModel, Me)
End Sub
Public Sub AddItem(ByVal iEntry() As String)
mClientlistModel.AddItem(iEntry)
End Sub
End Class
ClientlistView.vb (the view.. this has the LIstview I would like to fill)
Imports System.Windows.Forms
Public Class ClientlistView
Private mClientlistPresenter As ClientlistPresenter
Private WithEvents mClientlistModel As Clientlist
Private Sub OrderView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mClientlistPresenter = New ClientlistPresenter(Me)
End Sub
Private Sub Orderview_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Resize
Dim fWidth As Integer = Me.Width
Dim fHeight As Integer = Me.Height
ListView1.Bounds = New Rectangle(New Point(0, 25), New Size(fWidth, fHeight))
End Sub
Public Sub Init(ByRef model As Clientlist, ByRef presenter As ClientlistPresenter)
mClientlistPresenter = presenter
mClientlistModel = model
' Set the view to show details.
listView1.View = View.Details
' Allow the user to rearrange columns.
listView1.AllowColumnReorder = True
' Display check boxes.
listView1.CheckBoxes = True
' Select the item and subitems when selection is made.
listView1.FullRowSelect = True
' Display grid lines.
listView1.GridLines = True
' Sort the items in the list in ascending order.
listView1.Sorting = SortOrder.Ascending
ListView1.Columns.Add("#", 40, HorizontalAlignment.Left)
ListView1.Columns.Add("Wholename", 140, HorizontalAlignment.Left)
ListView1.Columns.Add("Date of birth", 80, HorizontalAlignment.Left)
listView1.Columns.Add("Country of birth", -2, HorizontalAlignment.Center)
' Add the ListView to the control collection.
Me.Controls.Add(listView1)
End Sub
Private Sub ClientlistChanged() Handles mClientlistModel.ClientlistChanged
ListView1.Items.Clear()
Dim i As Object
For Each i In mClientlistModel.Items()
Next
End Sub
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim week(6) As String
week(0) = "Sunday"
week(1) = "Monday"
week(2) = "Tuesday"
week(3) = "Wednesday"
week(4) = "Thursday"
week(5) = "Friday"
week(6) = "Saturday"
mClientlistPresenter.AddItem(week)
End Sub
End Class
Via the “New ClientlistItem(iEntry)” call in Clientlist.AddItem, you are putting the iEntry elements into the private ClientlistItem.entry member. How exactly did you plan to make the data in “Private entry(6) As String” accessible outside of ClientlistItem?
One solution is to create a Property inside of ClientlistItem:
Put this inside your Button1_Click function at the end to confirm that values are getting in and staying in:
The “Immediate” window of the IDE should show something like:
That above list should grow with every button click.
Hope this helps!