0

I've seen this, this, and this, but they don't quite answer my question.

I have a project that is an upgrade of a VB6 program to VB.NET (2008) on the way to an eventual upgrade to today's version. I have a module that does a variety of things for many forms. The name of the active form is stored in a global variable (Current_Active_Form). There are the same controls on many forms with the same name from form to form (comboX, for instance). The one I am concerned with now is a function that loads a combobox (comboX) with a list of items using a for loop. There are many other functions that do this with many other comboboxes and datagridviews, so this will help me all over the place.

Example:

Public Function fillComboX(ByRef fncString As Object) As String
   'Current_Active_Form is a global variable
    Dim startPoint as Integer    'substring starting point
    Dim items as Integer         'number of items in item string
    Dim strItem as String        'string for individual item
    dim strItems as String       'string for all items

    ...'code that fills strItems based on fncString 
       '(don't ask me why it's an object)
    ...'items is filled at the same time

    startPoint = 1
    With Current_Active_Form
        .comboX.Items.Add("")
        '^^ line gives error comboX is not a member of System.Windows.Forms.Form
        For i = 1 to items
            strItem = strItems.Substring(startPoint, 50)
            .comboX.Items.Add(strItem)
            '^^so does this line
            startPoint = startPoint + 50
        Next i
    End With
End Function

Is there a way to do this similar to how I it have here? I can make whatever changes I need to, but this is a big enough upgrade as is. Thanks!

Community
  • 1
  • 1
pixelmeow
  • 654
  • 1
  • 9
  • 31
  • 1
    It sounds like `Current_Active_Form` is defined `As Form` rather than `As FormX` where formX is the actual class name of the form. That might be better as a method in the form class – Ňɏssa Pøngjǣrdenlarp Apr 05 '16 at 23:11
  • The second link offers another solution: pass the CBO. But if that is only ever used for `FormX.comboX` make it a method for the form to manage its own controls rather than some Module way over yonder – Ňɏssa Pøngjǣrdenlarp Apr 05 '16 at 23:18
  • @Plutonix Correct, the form is declared `Public Current_Active_Form As System.Windows.Forms.Form`. It could be one of several forms that call the `fillComboX` function. I can go ahead and copy and paste this code all over the place, but I'd rather keep it centrally, if possible. – pixelmeow Apr 06 '16 at 13:39
  • You are likely going to find that VB6 to .NET is more than just fixing up the syntax here and there: .NET is more strongly typed; some refactoring will like;y be involved. `Form` is a base class from which all other forms inherit; it doesnt have a `comboX` only an instance of `FormXYZ` does. If that CBO used a datasource, all the other actors could just add/modify that source as needed and the changes would appear. – Ňɏssa Pøngjǣrdenlarp Apr 06 '16 at 13:45
  • `comboX` is just the name of the combo box. So if `Current_Active_Form` has the name of the form, and the name of the combo box is `comboX`, then I can't do this from the module? I understand that there's more to this than changing syntax, I'm just trying to get that out of the way first. If I have to do this a different way, then I can comment it for now. Thanks for your help so far. – pixelmeow Apr 06 '16 at 14:34
  • `FormFoo` is a specific Type, which has all sorts of controls on it, including `comboX`. `FormFoo` inherits from `System.Windows.Forms.Form` which is the basic form - no controls. So when you assign it to `Current_Active_Form` defined `As Form` you threw away all that info on the more specific Type (including that the compiler can know that it even *has* a `comboX`). More than likely you have another issue upstream with default form instances. – Ňɏssa Pøngjǣrdenlarp Apr 06 '16 at 14:45
  • @Plutonix Ah, okay, I get it. That makes total sense. Thank you so much for your explanation. I'll take care of these things behind the individual forms. – pixelmeow Apr 06 '16 at 15:04
  • As part of refactoring, consider a class-centric design (at least). If that is a customer form, make a Customer class which is in charge of ***all*** things Customer related. It is the only actor which talks to the form. Everything else "tells" Customer that this or that list changed and it invokes methods etc on the form to bring about change. – Ňɏssa Pøngjǣrdenlarp Apr 06 '16 at 15:21

1 Answers1

0

You need to refer to the Controls collection in the Current_Active_Form and use the control name as the key to get a reference to the control you would like to modify. I have provided code below to illustrate what I mean. The code does rely on Current_Active_Form being defined as a Form.

Public Current_Active_Form As Form

Public Function fillComboX(ByRef fncString As Object) As String
    'Current_Active_Form is a global variable
    Dim startPoint As Integer    'substring starting point
    Dim items As Integer         'number of items in item string
    Dim strItem As String        'string for individual item
    Dim strItems As String = CStr(fncString)      'string for all items

    'code that fills strItems based on fncString 
    '(don't ask me why it's an object)
    'items is filled at the same time

    startPoint = 0
    With Current_Active_Form
        If .Controls.ContainsKey("comboX") Then
            '^^ line gives error comboX is not a member of System.Windows.Forms.Form
            For i = 0 To strItems.Length - 1
                strItem = strItems.Substring(startPoint, 4)
                DirectCast(.Controls("comboX"), ComboBox).Items.Add(strItem)
                '^^so does this line
                startPoint = startPoint + 4
            Next i
        End If
    End With
End Function

Also your code is defined as a Function but does not return a value, so unless you intend on returning a value, it should be defined as a Sub.

I also reduced the substring length for my testing purposes which you can easily change back to 50

Kelly Barnard
  • 1,131
  • 6
  • 8
  • Thanks, I'll give this a try. There are a lot of functions in this code that don't return anything, so that's one of the things we're looking at changing. – pixelmeow Apr 06 '16 at 16:52