How do I make a generic procedure?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • iDesmet

    How do I make a generic procedure?

    Hallo,

    For example, I have the following procedure to Load a form:

    Public Sub LoadClient(ByVa l Caption As String)
    Dim frmClient As New frmClient()
    frmClient.MdiPa rent = frmMainMDI
    frmClient.Text = Caption
    frmClient.Show( )
    frmClient.Focus ()

    frmMainMDI.Text = "Prod 2008 - " & Caption
    frmMainMDI.btnC lientNew.Enable d = False
    End Sub

    So to load it, I only call this: LoadClient("New Client")

    But I need to write almost the same to load another form. The only
    thing I change is frmClient and the buttom I want to disable in this
    case.

    So my question is this: Is it posible to make a generic procedure so I
    only pass the values I want like in Caption? or I need a diferent
    approach to acomplish this task? If so how?

    Thanks in advance

    Best Regards,
    David Desmet
  • Steve Gerrard

    #2
    Re: How do I make a generic procedure?

    iDesmet wrote:
    Hallo,
    >
    For example, I have the following procedure to Load a form:
    >
    Public Sub LoadClient(ByVa l Caption As String)
    Dim frmClient As New frmClient()
    frmClient.MdiPa rent = frmMainMDI
    frmClient.Text = Caption
    frmClient.Show( )
    frmClient.Focus ()
    >
    frmMainMDI.Text = "Prod 2008 - " & Caption
    frmMainMDI.btnC lientNew.Enable d = False
    End Sub
    >
    So to load it, I only call this: LoadClient("New Client")
    >
    But I need to write almost the same to load another form. The only
    thing I change is frmClient and the buttom I want to disable in this
    case.
    >
    So my question is this: Is it posible to make a generic procedure so I
    only pass the values I want like in Caption? or I need a diferent
    approach to acomplish this task? If so how?
    >
    I would write it like this:

    Public Sub LoadClient(ByVa l frm As Form, ByVal Caption As String)
    frm.MdiParent = frmMainMDI
    frm.Text = Caption
    frm.Show()
    frm.Focus()
    End Sub

    And call it like this:
    LoadClient(New frmClient(), "New Client")


    Comment

    • Cor Ligthert[MVP]

      #3
      Re: How do I make a generic procedure?

      Hi,

      I think I would do it like this. However I don't understand that button, a
      menu looks more obvious to me. And for sure not that retro word "Load"
      (although Microsoft uses it too standard in Load_Form).

      \\\
      Private Sub Button1_Click(B yVal sender As System.Object, _
      ByVal e As System.EventArg s) Handles
      Button1.Click
      Static staticNumber As Integer = 0
      CreateClient(st aticNumber.ToSt ring)
      staticNumber += 1

      End Sub
      Public Sub CreateClient(By Val Caption As String)
      Dim frm As New Form()
      frm.MdiParent = Me
      frm.Text = Caption
      frm.Show()
      frm.Focus()
      frm.Name = "Prod 2008 - " & Caption
      frm.Text = "Prod 2008 - " & Caption
      ' Button1.Enabled = False
      End Sub
      ///

      Be aware that the reference to this frm goes direct out of scope so you have
      to reference it using the MDIParent.MdiCh ildren array.

      Gets an array of forms that represent the multiple-document interface (MDI) child forms that are parented to this form.


      Cor



      "iDesmet" <idesmet.work@g mail.comschreef in bericht
      news:aad8816a-84ca-475a-8572-827b08a96f43@y2 1g2000hsf.googl egroups.com...
      Hallo,
      >
      For example, I have the following procedure to Load a form:
      >
      Public Sub LoadClient(ByVa l Caption As String)
      Dim frmClient As New frmClient()
      frmClient.MdiPa rent = frmMainMDI
      frmClient.Text = Caption
      frmClient.Show( )
      frmClient.Focus ()
      >
      frmMainMDI.Text = "Prod 2008 - " & Caption
      frmMainMDI.btnC lientNew.Enable d = False
      End Sub
      >
      So to load it, I only call this: LoadClient("New Client")
      >
      But I need to write almost the same to load another form. The only
      thing I change is frmClient and the buttom I want to disable in this
      case.
      >
      So my question is this: Is it posible to make a generic procedure so I
      only pass the values I want like in Caption? or I need a diferent
      approach to acomplish this task? If so how?
      >
      Thanks in advance
      >
      Best Regards,
      David Desmet

      Comment

      • iDesmet

        #4
        Re: How do I make a generic procedure?

        Thanks a lot for showing me the way.

        I had created a Sub that fit my needs. It works well, later I'll add a
        little more of code to change the Icon of the form. I tried it but
        didn't work as expected, but anyway, for now it's oke.

        Here's my code:

        Public Sub frmMdiChildCtrl (ByVal frmChildForm As Type, ByVal
        frmChildCaption As String) ', ByVal frmChildIcon As Icon)

        Dim childForm As Form
        'Check if the child form is already displayed so there isn't
        two instances showing
        For Each childForm In frmMainMDI.MdiC hildren
        If childForm.Text = frmChildCaption Then
        If childForm.Windo wState = FormWindowState .Minimized
        Then
        childForm.Windo wState = FormWindowState .Normal
        End If
        childForm.Activ ate()
        Return
        End If
        Next

        childForm = CType(Activator .CreateInstance (frmChildForm),
        Form)
        childForm.MdiPa rent = frmMainMDI
        'childForm.Icon = frmChildIcon
        childForm.Text = frmChildCaption
        childForm.Show( )
        childForm.Activ ate()
        'childForm.Focu s()

        frmMainMDI.Text = gsAppName & " - " & frmChildCaption

        frmMdiChildCtrl Btn(frmChildCap tion)
        End Sub

        Comment

        Working...