Results 1 to 12 of 12

Thread: [RESOLVED] Passing Either A ComboBox Or A ListBox As A Parameter

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Resolved [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
    Quote 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.)

  2. #2
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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)

  4. #4
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  6. #6
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  7. #7

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Passing Either A ComboBox Or A ListBox As A Parameter

    Yep, that did it.
    vb.net Code:
    1. Private Sub LoadDaysOfMonth(ByVal pintWhatMonth As Integer, ByVal pctrlWhatControl As Control)
    2.         Dim NumOfDays As Integer
    3.         Select Case pintWhatMonth
    4.             Case 1, 3, 5, 7, 8, 10, 12
    5.                 NumOfDays = 31
    6.             Case 4, 6, 9, 11
    7.                 NumOfDays = 30
    8.             Case 2
    9.                 NumOfDays = 28
    10.         End Select
    11.         For i As Integer = 1 To NumOfDays
    12.             If TypeOf pctrlWhatControl Is ListBox Then
    13.                 Dim tempListBox As ListBox = DirectCast(pctrlWhatControl, ListBox)
    14.                 tempListBox.Items.Add(pintWhatMonth & "/" & i & "/" & Year(Now))
    15.             ElseIf TypeOf pctrlWhatControl Is ComboBox Then
    16.                 Dim tempComboBox As ComboBox = DirectCast(pctrlWhatControl, ComboBox)
    17.                 tempComboBox.Items.Add(pintWhatMonth & "/" & i & "/" & Year(Now))
    18.             End If
    19.         Next
    20. End Sub
    21.  
    22. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    23.         LoadDaysOfMonth(9, ComboBox1)
    24.         LoadDaysOfMonth(7, ListBox1)
    25. End Sub
    26. End Class

  8. #8
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  9. #9

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Passing Either A ComboBox Or A ListBox As A Parameter

    Quote Originally Posted by gep13 View Post
    I thought I was going mental there for a minute!!
    Programming will do that to you every now and again.

    Thanks for the help.

  10. #10
    New Member
    Join Date
    Oct 2022
    Posts
    1

    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.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [RESOLVED] Passing Either A ComboBox Or A ListBox As A Parameter

    Quote Originally Posted by rifro View Post
    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

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.
    Quote Originally Posted by rifro View Post
    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:
    1. Private Sub LoadDates(month As Integer, control As ListControl)
    2.     Dim year = Date.Now.Year
    3.     Dim dates = Enumerable.Range(1, Date.DaysInMonth(year, month)).
    4.                            Select(Function(day) New Date(year, month, day).ToString("M/dd/yyyy")).
    5.                            ToArray()
    6.  
    7.     control.DataSource = dates
    8. End Sub
    vb.net Code:
    1. Private Sub LoadDates(month As Integer, items As IList)
    2.     Dim year = Date.Now.Year
    3.  
    4.     For Each dt In Enumerable.Range(1, Date.DaysInMonth(year, month)).
    5.                               Select(Function(day) New Date(year, month, day))
    6.         items.Add(dt.ToString("M/dd/yyyy"))
    7.     Next
    8. 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:
    1. Private Sub LoadDates(month As Integer, control As ListControl)
    2.     Dim year = Date.Now.Year
    3.     Dim dates = Enumerable.Range(1, Date.DaysInMonth(year, month)).
    4.                            Select(Function(day) New Date(year, month, day)).
    5.                            ToArray()
    6.  
    7.     control.DataSource = dates
    8. 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:
    1. Private Sub LoadDates(month As Integer, control As ListControl)
    2.     Dim year = Date.Now.Year
    3.     Dim dates = Enumerable.Range(1, Date.DaysInMonth(year, month)).
    4.                            Select(Function(day)
    5.                                       Dim dt As New Date(year, month, day)
    6.  
    7.                                       Return Tuple.Create(dt, dt.ToString("M/dd/yyyy"))
    8.                                   End Function).
    9.                            ToArray()
    10.  
    11.     With control
    12.         .ValueMember = "Item1"
    13.         .DisplayMember = "Item2"
    14.         .DataSource = dates
    15.     End With
    16. 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
  •  



Click Here to Expand Forum to Full Width