Is there any way of making the following code into a function so that I
don't have to re-use the same code for all my buttons? I've tried to do this
with the code at the bottom of my post but I get a build error saying 'Type
FormToShow is not defined'.

Cheers,
Paul


Private WithEvents Example1Form As frmDragDropFromExplorer
Private WithEvents Example2Form As frmDragDrop

Public Shared Sub Main()
Application.EnableVisualStyles()
Application.DoEvents()
Application.Run(New frmMain)
End Sub

Private Sub btnExample1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExample1.Click
Try
Example1Form.Show()
Catch
Example1Form = New frmDragDropFromExplorer
Example1Form.Show()
Finally
Example1Form.Focus()
End Try
End Sub

---- Tried this but got the error 'FormToShow is not defined' ---
Private Sub btnExample1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExample1.Click
ShowForm(Example1Form)
End Sub

Private Function ShowForm(ByVal FormToShow As Form)
Try
FormToShow.Show()
Catch
FormToShow = New FormToShow
FormToShow.Show()
Finally
FormToShow.Focus()
End Try
End Function

Re: Form question (Re-usuable code) by Matt

Matt
Thu Mar 31 20:46:52 CST 2005

Hello Paul,

In your exception handler, you're doing

FormToShow = New FormToShow

Im assuming that FormToShow isnt a valid type in your assembly. Perhaps,
you meant

FormToShow = New frmDragDropFromExplorer

--
Matt Berther
http://www.mattberther.com

> Is there any way of making the following code into a function so that
> I don't have to re-use the same code for all my buttons? I've tried to
> do this with the code at the bottom of my post but I get a build error
> saying 'Type FormToShow is not defined'.
>
> Cheers,
> Paul
> Private WithEvents Example1Form As frmDragDropFromExplorer Private
> WithEvents Example2Form As frmDragDrop
>
> Public Shared Sub Main()
> Application.EnableVisualStyles()
> Application.DoEvents()
> Application.Run(New frmMain)
> End Sub
> Private Sub btnExample1_Click(ByVal sender As System.Object, ByVal e
> As
> System.EventArgs) Handles btnExample1.Click
> Try
> Example1Form.Show()
> Catch
> Example1Form = New frmDragDropFromExplorer
> Example1Form.Show()
> Finally
> Example1Form.Focus()
> End Try
> End Sub
> ---- Tried this but got the error 'FormToShow is not defined' ---
> Private Sub btnExample1_Click(ByVal sender As System.Object, ByVal e
> As
> System.EventArgs) Handles btnExample1.Click
> ShowForm(Example1Form)
> End Sub
> Private Function ShowForm(ByVal FormToShow As Form)
> Try
> FormToShow.Show()
> Catch
> FormToShow = New FormToShow
> FormToShow.Show()
> Finally
> FormToShow.Focus()
> End Try
> End Function



Re: Form question (Re-usuable code) by Paul

Paul
Thu Mar 31 21:34:33 CST 2005

Correct but I don't want to specify the name of the form (in this case
frmDragDropFromExplorer) in the actual function.
I need the function to use the name of the form it has been passed (in this
case Example1Form) and not to be 'hard-coded' with the forms name.

So basically imagine I had 5 buttons on a form, all needing to be checked to
see if they are already open using the function I've created.
Any hard-coding of form names is not going to work. Apoligies if I didn't
explain this in the first post, and sorry the post seems to have been posted
twice! Don't know what went wrong there!

Cheers,
Paul



"Matt Berther" <mberther@hotmail.com> wrote in message
news:221a09c78f58508c7044184fe444c@news.microsoft.com...
> Hello Paul,
>
> In your exception handler, you're doing
>
> FormToShow = New FormToShow
>
> Im assuming that FormToShow isnt a valid type in your assembly. Perhaps,
> you meant
>
> FormToShow = New frmDragDropFromExplorer
>
> --
> Matt Berther
> http://www.mattberther.com
>
>> Is there any way of making the following code into a function so that
>> I don't have to re-use the same code for all my buttons? I've tried to
>> do this with the code at the bottom of my post but I get a build error
>> saying 'Type FormToShow is not defined'.
>>
>> Cheers,
>> Paul
>> Private WithEvents Example1Form As frmDragDropFromExplorer Private
>> WithEvents Example2Form As frmDragDrop
>>
>> Public Shared Sub Main()
>> Application.EnableVisualStyles()
>> Application.DoEvents()
>> Application.Run(New frmMain)
>> End Sub
>> Private Sub btnExample1_Click(ByVal sender As System.Object, ByVal e
>> As
>> System.EventArgs) Handles btnExample1.Click
>> Try
>> Example1Form.Show()
>> Catch
>> Example1Form = New frmDragDropFromExplorer
>> Example1Form.Show()
>> Finally
>> Example1Form.Focus()
>> End Try
>> End Sub
>> ---- Tried this but got the error 'FormToShow is not defined' ---
>> Private Sub btnExample1_Click(ByVal sender As System.Object, ByVal e
>> As
>> System.EventArgs) Handles btnExample1.Click
>> ShowForm(Example1Form)
>> End Sub
>> Private Function ShowForm(ByVal FormToShow As Form)
>> Try
>> FormToShow.Show()
>> Catch
>> FormToShow = New FormToShow
>> FormToShow.Show()
>> Finally
>> FormToShow.Focus()
>> End Try
>> End Function
>
>