Connecting Tech Pros Worldwide Forums | Help | Site Map

How do I make a generic procedure?

iDesmet
Guest
 
Posts: n/a
#1: Jun 27 '08
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

Steve Gerrard
Guest
 
Posts: n/a
#2: Jun 27 '08

re: How do I make a generic procedure?


iDesmet wrote:
Quote:
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?
>
I would write it like this:

Public Sub LoadClient(ByVal 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")


Cor Ligthert[MVP]
Guest
 
Posts: n/a
#3: Jun 27 '08

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(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/libr...en(VS.80).aspx

Cor



"iDesmet" <idesmet.work@gmail.comschreef in bericht
news:aad8816a-84ca-475a-8572-827b08a96f43@y21g2000hsf.googlegroups.com...
Quote:
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
iDesmet
Guest
 
Posts: n/a
#4: Jul 23 '08

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.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
Closed Thread