By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Items

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

"Wally" <va****@nospamcambieri.it> wrote in message
news:9R***********************@news3.tin.it...
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

Nov 20 '05 #2

P: n/a
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
///
Nov 20 '05 #3

P: n/a
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
"Eric Lemmon" <E_********************@hotmail.com> ha scritto nel messaggio
news:e4**************@TK2MSFTNGP09.phx.gbl...
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

"Wally" <va****@nospamcambieri.it> wrote in message
news:9R***********************@news3.tin.it...
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


Nov 20 '05 #4

P: n/a
* "Wally" <va****@nospamcambieri.it> scripsit:
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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5

P: n/a
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
///

Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.