2015-06-22 56 views
0

我正在写一个简单的应用程序来读取文本框的值并添加到列表框控件。但我必须通过列表框控件才能运行。任何建议?如何将ListBox控件的所有值传递给函数?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    test("E:\Satyajit.txt") 
End Sub 

Public Function test(ByVal filename As String) 
    Dim FILE_NAME As String = filename 
    Dim TextLine As String 
    Dim result As String = Path.GetFileName(FILE_NAME) 
    Dim objReader As New System.IO.StreamReader(FILE_NAME) 
    Do While objReader.Peek() <> -1 
     TextLine = objReader.ReadLine() 
     words = TextLine.Split(New Char() {","c}) 
     ListBox1.Items.Add(words(3) & "," & words(4)) 
     objItem = ListView1.Items.Add(words(3) & "," & words(4)) 
    Loop 
    test1(ListBox1.Items)//pass the listbox value hare 
End Function 

Public Function test1(ByVal value As String) 
    Dim Fest As String = value 
    MsgBox(Fest) 
End Function 
+0

你想通过它所有列表框的ems?还是只有1? – missellorin

+0

列表框中所选项目的值? – Fred

+0

我不确定你在问什么。或许:1)'公共函数test1(ByVal items As ListBox.ObjectCollection)...',2)'test1(ListBox1.Items)'? – paulsm4

回答

1

你传递一个ListBox的内容的方法,这只是将它们显示在MsgBox()中。有两种方法可以完成我认为你想要的事情。

  1. 可以传递ListBox.Items的方法和通过每个项目串联成一个单一的StringStringBuilder迭代,然后将字符串传递给MsgBox()。这种方法使您的方法依赖于ListBoxItems。

  2. 您可以迭代通过ListBox.Items将它们连接成单个StringStringBuilder,然后将该字符串传递给您的方法。这使您的方法更具可扩展性。

我推荐的方法#2,是这样的:

Dim MyListBox As New ListBox 
MyListBox.Items.Add("Item1") 
MyListBox.Items.Add("Item2") 
MyListBox.Items.Add("Item3") 
MyListBox.Items.Add("Item4") 
MyListBox.Items.Add("Item5") 

Dim sb As New StringBuilder 
For Each Item In MyListBox.Items 
    sb.AppendLine(Item) 
Next 
Test1(sb.ToString()) 

的Test1的方法看起来像:

Public Sub Test1(ByVal value As String) 
    MsgBox(value) 
End Sub 

结果:

enter image description here

0

你可以整个控制传递给函数:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim lstbox As New ListBox 
    lstbox.Items.Add("Hello") 
    lstbox.Items.Add("Second Item") 
    lstbox.Items.Add("Third Item") 

    MsgBox("The list contains: " & Length(lstbox) & " characters") 
End Sub 

Function Length(ByVal ctrl As ListBox) As Integer 
    Dim TotalNumberOfItems As Integer = 0 
    For Each item As String In ctrl.Items.ToString 
     TotalNumberOfItems += 1 
    Next 
    Return TotalNumberOfItems 
End Function 

或只是其项目

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim lstbox As New ListBox 
    lstbox.Items.Add("Hello") 
    lstbox.Items.Add("Second Item") 
    lstbox.Items.Add("Third Item") 

    MsgBox("The list contains: " & Length(lstbox.Items) & " characters") 
End Sub 

Function Length(ByVal col As ListBox.ObjectCollection) As Integer 
    Dim TotalNumberOfCharacters As Integer = 0 
    For Each item As String In col 
     TotalNumberOfCharacters += item.Length 
    Next 
    Return TotalNumberOfCharacters 
End Function 
+0

如果他只对列表感兴趣,他不应该通过整个控制。很容易将列表传递给一个函数:只需声明像test1(ByVal items As ListBox.ObjectCollection)这样的函数' – paulsm4

+0

请仔细阅读我的答案carefuly – Hama

+0

那么我该如何显示? – Satyajit

相关问题