2012-02-24 70 views
1

我絕對新VB.NET,我做了一些VBS很久以前。vb.net數組列表將不會加載

現在我想做一個MVP還挺系統,我試圖定義並加載一個陣列,通過該系統。然後讀出它,它應該填充一個ListView1對象。

誰能閃耀爲什麼它不填充一些輕。我知道他們設置正確,因爲控制檯告訴我它已經設置好了。

我知道樣品將與同一項目(每週要準確)填寫我每次添加一個項目的時間,但是這不是問題的關鍵。

另外,是否有可能定義數組沒有索引,我的意思是現在我與(6)設置,以便它爲字符串6個塊,但我想更加靈活。

有沒有辦法像在PHP例如 $項目[ 「名字」] $項目[ 「姓氏」] ...

感謝您的時間和精力! :-)

ClientlistItem.vb(實際的項目的定義)

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(該模型因爲它是)

進口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(主持人)

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(視圖..這有我想填補的ListView)

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 
+0

在你的代碼你是哪裏試圖精確地填充ListView? – 2012-02-24 14:01:23

回答

2

通過「新ClientlistItem(iEntry)」調用Clientlist.AddItem,你把iEntry元素融入私人ClientlistItem.entry成員。你究竟打算如何在ClientlistItem之外訪問「Private entry(6)As String」中的數據?

一種解決方案是創建內部ClientlistItem的屬性:

Public ReadOnly Property Items As String() 
    Get 
     Return entry 
    End Get 
End Property 

末將這個你的button1_Click函數中,以確認值越來越並住在:

For i As Integer = 0 To mClientlistModel.Items.Count - 1 
     For j As Integer = 0 To mClientlistModel.Items(i).Items.Count - 1 
      Debug.Print(String.Format("item({0},{1})=", i, j) + mClientlistModel.Items(i).Items(j)) 
     Next 
    Next 
    Stop 

的IDE的「立即」窗口應顯示如下內容:

item(0,0)=Sunday 
item(0,1)=Monday 
item(0,2)=Tuesday 
item(0,3)=Wednesday 
item(0,4)=Thursday 
item(0,5)=Friday 
item(0,6)= 

Th上面的列表應該隨着每個按鈕的點擊而增長。

希望這會有所幫助!

+1

請注意,「Public ReadOnly Property Items As String()」將允許修改元素。如果你真的想要它們是隻讀的,你需要創建一個「Public ReadOnly屬性項(索引爲整數)作爲字符串」,也可能是「公共只讀屬性計數爲整數」。 – rskar 2012-02-24 14:49:24