-
Mar 24th, 2009, 10:03 AM
#1
[RESOLVED] Passing Either A ComboBox Or A ListBox As A Parameter
I have this in VB6
Code:
Private Sub LoadDaysOfMonth(pintWhatMonth As Integer, pctrlWhatControl As Control)
Dim NumOfDays As Integer
Dim i As Integer
Select Case pintWhatMonth
Case 1, 3, 5, 7, 8, 10, 12
NumOfDays = 31
Case 4, 6, 9, 11
NumOfDays = 30
Case 2
NumOfDays = 28
End Select
For i = 1 To NumOfDays
pctrlWhatControl.AddItem pintWhatMonth & "/" & i & "/" & Year(Now)
Next
End Sub
Private Sub Command1_Click()
LoadDaysOfMonth 10, List1
LoadDaysOfMonth 2, Combo1
End Sub
Works like a champ.
So, I try to translate this into VB.NET
Code:
Private Sub LoadDaysOfMonth(ByVal pintWhatMonth As Integer, ByVal pctrlWhatControl As Control)
Dim NumOfDays As Integer
Select Case pintWhatMonth
Case 1, 3, 5, 7, 8, 10, 12
NumOfDays = 31
Case 4, 6, 9, 11
NumOfDays = 30
Case 2
NumOfDays = 28
End Select
For i As Integer = 1 To NumOfDays
pctrlWhatControl.items.add(pintWhatMonth & "/" & i & "/" & Year(Now))
Next
End Sub
But, now an error comes
Originally Posted by Error
'items' is not a member of Systems.Windows.Forms.Control
It works just fine if I use pctrlListBox As ListBox or if I use pctrlCombo As ComboBox, but I need to be able to use both with the same routine.
(PS: Yes, I do have a leap year check in here which I took out as it is not germine to my translation problem.)
(PPS: Using a control such as the monthview or datepicker is not an option.)
-
Mar 24th, 2009, 10:12 AM
#2
Re: Passing Either A ComboBox Or A ListBox As A Parameter
Hey,
You are passing a Control as the Parameter, and Control is the Base Class, hence it doesn't know what the Items collections is.
If you are trying to make a method that will accept both a ComboBox or a ListBox either create an overloaded method for each type, or first cast the Control as either a ComboBox or a ListBox and then access the items property.
i.e.
Code:
If TypeOf control Is ComboBox Then
'do stuff
ElseIf TypeOf control Is ListBox Then
'do stuff
End If
Hope that makes sense.
Gary
Last edited by gep13; Mar 24th, 2009 at 10:21 AM.
-
Mar 24th, 2009, 10:58 AM
#3
Re: Passing Either A ComboBox Or A ListBox As A Parameter
But the 'do stuff' that I want to do it add items. Items isn't being recognized unless I remove the word control and define my parameter as a ListBox or as a combobox. This defeats the purpose of the single sub to perform this task on a number of different forms throughout the project using either a listbox or combo box depending on what is in use on any given form.
If I can easily do this in VB6, then there is no way that anyone can convince me I can't do it in VB.NET - it is just a matter of how and that happens to be eluding me at the moment.
But, the struggle continues.
(BTW: I did try
Code:
For i As Integer = 1 To NumOfDays
If TypeOf pctrlWhatControl Is ListBox Then
pctrlWhatControl.items.add(pintWhatMonth & "/" & i & "/" & Year(Now))
ElseIf TypeOf pctrlWhatControl Is ComboBox Then
pctrlWhatControl.items.add(pintWhatMonth & "/" & i & "/" & Year(Now))
End If
Next
It didn't matter...it still doesn't know what 'items' are)
-
Mar 24th, 2009, 10:59 AM
#4
Re: Passing Either A ComboBox Or A ListBox As A Parameter
Hey,
Once you are in the If Statement, you can then do something like:
Code:
Dim tempComboBox As ComboBox = DirectCast(pctrlWhatControl, ComboBox)
tempComboBox.Items.Add()
That is off the top of my head, not in the IDE.
Gary
-
Mar 24th, 2009, 11:03 AM
#5
Re: Passing Either A ComboBox Or A ListBox As A Parameter
But I won't know whether it is a combobox or a listbox.
If it was all the same throughout then I could set my parameter as the actual data type name rather than using the generic data type of Control.
-
Mar 24th, 2009, 11:44 AM
#6
Re: Passing Either A ComboBox Or A ListBox As A Parameter
Hey,
You do know what it is, as you have just checked the type of the Control that is being passed through, so you can cast it to that type:
Code:
For i As Integer = 1 To NumOfDays
If TypeOf pctrlWhatControl Is ListBox Then
Dim tempListBox As ListBox = DirectCast(pctrlWhatControl, ListBox)
tempListBox.Items.Add()
ElseIf TypeOf pctrlWhatControl Is ComboBox Then
Dim temComboBox As ComboBox = DirectCast(pctrlWhatControl, ComboBox)
temComboBox.Items.Add()
End If
Next
Or am I missing something?
Gary
-
Mar 24th, 2009, 11:50 AM
#7
Re: Passing Either A ComboBox Or A ListBox As A Parameter
Yep, that did it.
vb.net Code:
Private Sub LoadDaysOfMonth(ByVal pintWhatMonth As Integer, ByVal pctrlWhatControl As Control)
Dim NumOfDays As Integer
Select Case pintWhatMonth
Case 1, 3, 5, 7, 8, 10, 12
NumOfDays = 31
Case 4, 6, 9, 11
NumOfDays = 30
Case 2
NumOfDays = 28
End Select
For i As Integer = 1 To NumOfDays
If TypeOf pctrlWhatControl Is ListBox Then
Dim tempListBox As ListBox = DirectCast(pctrlWhatControl, ListBox)
tempListBox.Items.Add(pintWhatMonth & "/" & i & "/" & Year(Now))
ElseIf TypeOf pctrlWhatControl Is ComboBox Then
Dim tempComboBox As ComboBox = DirectCast(pctrlWhatControl, ComboBox)
tempComboBox.Items.Add(pintWhatMonth & "/" & i & "/" & Year(Now))
End If
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
LoadDaysOfMonth(9, ComboBox1)
LoadDaysOfMonth(7, ListBox1)
End Sub
End Class
-
Mar 24th, 2009, 11:51 AM
#8
Re: Passing Either A ComboBox Or A ListBox As A Parameter
Woo Hoo!!
I thought I was going mental there for a minute!!
Happy to help!!!
Gary
-
Mar 24th, 2009, 11:54 AM
#9
Re: Passing Either A ComboBox Or A ListBox As A Parameter
Originally Posted by gep13
I thought I was going mental there for a minute!!
Programming will do that to you every now and again.
Thanks for the help.
-
Oct 4th, 2022, 05:56 AM
#10
New Member
Re: [RESOLVED] Passing Either A ComboBox Or A ListBox As A Parameter
I still see code duplication.
I would not pass the ComboBox, but its items instead.
-
Oct 4th, 2022, 09:11 AM
#11
Re: [RESOLVED] Passing Either A ComboBox Or A ListBox As A Parameter
Originally Posted by rifro
I still see code duplication.
I would not pass the ComboBox, but its items instead.
Why revive a 13 year old RESOLVED thread? Try answering the new questions in the forum
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 5th, 2022, 10:26 PM
#12
Re: [RESOLVED] Passing Either A ComboBox Or A ListBox As A Parameter
As we're here though, I can't really say that I like the stated solution much either.
The first thing that comes to mind is that both ComboBox and ListBox inherit ListControl, so you could use that as the parameter type. Unfortunately, the Items property is not inherited from that type, so you can't add items to a ListControl that way.
Originally Posted by rifro
I still see code duplication.
I would not pass the ComboBox, but its items instead.
You could do that, although the Items property of the ListBox and ComboBox are different types. They both implement IList, so you could use that, although that only provides an Add method, where AddRange would be better. Here are a couple of options I would consider better than what was arrived at above:
vb.net Code:
Private Sub LoadDates(month As Integer, control As ListControl)
Dim year = Date.Now.Year
Dim dates = Enumerable.Range(1, Date.DaysInMonth(year, month)).
Select(Function(day) New Date(year, month, day).ToString("M/dd/yyyy")).
ToArray()
control.DataSource = dates
End Sub
vb.net Code:
Private Sub LoadDates(month As Integer, items As IList)
Dim year = Date.Now.Year
For Each dt In Enumerable.Range(1, Date.DaysInMonth(year, month)).
Select(Function(day) New Date(year, month, day))
items.Add(dt.ToString("M/dd/yyyy"))
Next
End Sub
This uses Enumerable.Range, which would not have been available in 2009, but you could do the equivalent with a For loop. There's also the option of writing a Function that returns the list of dates and passing that to the Items.AddRange method of the control, rather than passing the control into the method. This all ignores the fact that these options all load Strings into the control to represent dates. I'd be more inclined to use data-binding and actually bind Date values, possibly using Strings for display if required, e.g.
vb.net Code:
Private Sub LoadDates(month As Integer, control As ListControl)
Dim year = Date.Now.Year
Dim dates = Enumerable.Range(1, Date.DaysInMonth(year, month)).
Select(Function(day) New Date(year, month, day)).
ToArray()
control.DataSource = dates
End Sub
That will display the default date format for the local machine, which is probably best anyway. If you wanted to force a format for display:
vb.net Code:
Private Sub LoadDates(month As Integer, control As ListControl)
Dim year = Date.Now.Year
Dim dates = Enumerable.Range(1, Date.DaysInMonth(year, month)).
Select(Function(day)
Dim dt As New Date(year, month, day)
Return Tuple.Create(dt, dt.ToString("M/dd/yyyy"))
End Function).
ToArray()
With control
.ValueMember = "Item1"
.DisplayMember = "Item2"
.DataSource = dates
End With
End Sub
Note that, unlike the original code, this will actually display February 29 if we're in a leap year.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|