Items

  • Thread starter Thread starter Wally
  • Start date Start date
W

Wally

I need to browse all ITEMS of a ListBox and all ITEMS of a ComboBox.
I wrote 2 distinct Sub that are almost identical. The difference is only the
object type. How could I browsing ITEMS using only one Sub with object as
parameter?

Here, my two Subs. Thanks



Private Sub AnalyzeListBox()
Dim sText as String
Dim objCtrl As System.Windows.Forms.ListBox = Form1.ListBox1
For i = 0 To objCtrl.Items.Count - 1
Dim objNewElement As New OneElement
sText = objCtrl.Items(i).ToString
Console.WriteLine(sText)
Next
End Sub


Private Sub AnalyzeComboBox()
Dim sText as String
Dim objCtrl As System.Windows.Forms.ComboBox = Form1.ComboBox1
For i = 0 To objCtrl.Items.Count - 1
Dim objNewElement As New OneElement
sText = objCtrl.Items(i).ToString
Console.WriteLine(sText)
Next
End Sub
 
Hi Wally,

You could pass the actual control as a parameter:

Private Sub AnalyzeControl(ByRef myControl As Control)
Dim sText As String
Dim i As Integer

If TypeOf myControl Is ListBox Then
With DirectCast(myControl, ListBox)
For i = 0 To .Items.Count - 1
Dim objNewElement As New OneElement
sText = .Items(i).ToString
Console.WriteLine(sText)
Next
End With

ElseIf TypeOf myControl Is ComboBox Then
With DirectCast(myControl, ComboBox)
For i = 0 To .Items.Count - 1
Dim objNewElement As New OneElement
sText = .Items(i).ToString
Console.WriteLine(sText)
Next
End With
End If
End Sub

Take care,

Eric
 
Hi Wally,

You can try this,

I hope it helps?

Cor


\\\
AnalyzeListBox(me.Listbox1)
''''''
Private Sub AnalyzeListBox(byval lc as listcontrol)
Dim sText as String
For i = 0 To lc.Items.Count - 1
Dim objNewElement As New OneElement
sText = lc.Items(i).ToString
Console.WriteLine(sText)
Next
End Sub
///
 
Thanks Eric, but this solution doesn't solve the problem. It is only one SUB
but most statements are repeated twice. I'm looking for a solution that
doesn't need repeated statements.
P.S.: sorry if my english is not good.... I'm italian! :-)

W
 
* "Wally said:
I need to browse all ITEMS of a ListBox and all ITEMS of a ComboBox.
I wrote 2 distinct Sub that are almost identical. The difference is only the
object type. How could I browsing ITEMS using only one Sub with object as
parameter? [...]
Private Sub AnalyzeListBox()
Dim sText as String
Dim objCtrl As System.Windows.Forms.ListBox = Form1.ListBox1

Declare 'objCtrl' as 'System.Windows.Forms.ListControl'. Then you can
use the same routine for listboxes and comboboxes. ListBox and ComboBox
inherit from this class.
For i = 0 To objCtrl.Items.Count - 1
Dim objNewElement As New OneElement
sText = objCtrl.Items(i).ToString
Console.WriteLine(sText)
Next
End Sub

\\\
Imports System.Windows.Forms
..
..
..
Public Sub AnalyzeListControl(ByVal TheControl As ListControl)
...
End Sub
///
 
Hi Wally

That objNew element can go as well of course.
\\\
AnalyzeListBox(me.Listbox1)
''''''
Private Sub AnalyzeListBox(byval lc as listcontrol)
Dim sText as String
For i = 0 To lc.Items.Count - 1
sText = lc.Items(i).ToString
Console.WriteLine(sText)
Next
End Sub
///
 
Back
Top