How do I make a generic procedure?

  • Thread starter Thread starter iDesmet
  • Start date Start date
I

iDesmet

Hallo,

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

Public Sub LoadClient(ByVal Caption As String)
Dim frmClient As New frmClient()
frmClient.MdiParent = frmMainMDI
frmClient.Text = Caption
frmClient.Show()
frmClient.Focus()

frmMainMDI.Text = "Prod 2008 - " & Caption
frmMainMDI.btnClientNew.Enabled = 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
 
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(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles
Button1.Click
Static staticNumber As Integer = 0
CreateClient(staticNumber.ToString)
staticNumber += 1

End Sub
Public Sub CreateClient(ByVal 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.MdiChildren array.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.mdichildren(VS.80).aspx

Cor
 
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.MdiChildren
If childForm.Text = frmChildCaption Then
If childForm.WindowState = FormWindowState.Minimized
Then
childForm.WindowState = FormWindowState.Normal
End If
childForm.Activate()
Return
End If
Next

childForm = CType(Activator.CreateInstance(frmChildForm),
Form)
childForm.MdiParent = frmMainMDI
'childForm.Icon = frmChildIcon
childForm.Text = frmChildCaption
childForm.Show()
childForm.Activate()
'childForm.Focus()

frmMainMDI.Text = gsAppName & " - " & frmChildCaption

frmMdiChildCtrlBtn(frmChildCaption)
End Sub
 
Back
Top