Form question (Re-usuable code)

P

Paul

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
 
M

Matt Berther

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
 
P

Paul

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
 
M

Matt Berther

Hello Paul,

You're going to have to pass the type of the form in a separate parameter
and use Reflection to create it. I'm not familiar enough with VB.NET, so
my example will be in C#. Hopefully you wont have any problems translating.

[C#]
private void ShowForm(Form formToShow, Type defaultForm)
{
try
{
formToShow.Show();
}
catch (Exception)
{
// This is where the magic happens
formToShow = (Form)Activator.CreateInstance(defaultForm);
}
finally
{
formToShow.Focus();
}
}

Your calling method would look something like:

ShowForm(example1Form, typeof(frmDragDropFromExplorer))

where you would vary the type passed as the second parameter as appropriate.

Hope this helps...
 
P

Paul

I converted it to VB.NET but it doesn't work. Pressing on the buttons does
nothing!

This is the converted code....

Private Sub btnExample1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExample1.Click
ShowForm(Example1Form, GetType(frmDragDropFromExplorer))
End Sub

Private Sub ShowForm(ByVal formToShow As Form, ByVal defaultForm As Type)
Try
formToShow.Show()
Catch ex As Exception
formToShow = CType(Activator.CreateInstance(defaultForm), Form)
Finally
formToShow.Focus()
End Try
End Sub


Any ideas?

Thanks,
Paul




Matt Berther said:
Hello Paul,

You're going to have to pass the type of the form in a separate parameter
and use Reflection to create it. I'm not familiar enough with VB.NET, so
my example will be in C#. Hopefully you wont have any problems
translating.

[C#]
private void ShowForm(Form formToShow, Type defaultForm)
{
try
{
formToShow.Show();
}
catch (Exception)
{
// This is where the magic happens
formToShow = (Form)Activator.CreateInstance(defaultForm);
}
finally
{
formToShow.Focus();
}
}

Your calling method would look something like:

ShowForm(example1Form, typeof(frmDragDropFromExplorer))

where you would vary the type passed as the second parameter as
appropriate.

Hope this helps...

--
Matt Berther
http://www.mattberther.com
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
 
M

Matt Berther

Hello Paul,

lol... Both of us missed that we dont call Show in the exception handler. :)

Catch ex As Exception
formToShow = CType(Activator.CreateInstance(defaultForm), Form)
formToShow.Show()
Finally
formToShow.Focus()
End Try

--
Matt Berther
http://www.mattberther.com
I converted it to VB.NET but it doesn't work. Pressing on the buttons
does nothing!

This is the converted code....

Private Sub btnExample1_Click(ByVal sender As System.Object, ByVal e
As
System.EventArgs) Handles btnExample1.Click
ShowForm(Example1Form, GetType(frmDragDropFromExplorer))
End Sub
Private Sub ShowForm(ByVal formToShow As Form, ByVal defaultForm As
Type)
Try
formToShow.Show()
Catch ex As Exception
formToShow = CType(Activator.CreateInstance(defaultForm),
Form)
Finally
formToShow.Focus()
End Try
End Sub
Any ideas?

Thanks,
Paul
Hello Paul,

You're going to have to pass the type of the form in a separate
parameter and use Reflection to create it. I'm not familiar enough
with VB.NET, so my example will be in C#. Hopefully you wont have any
problems translating.

[C#]
private void ShowForm(Form formToShow, Type defaultForm)
{
try
{
formToShow.Show();
}
catch (Exception)
{
// This is where the magic happens
formToShow = (Form)Activator.CreateInstance(defaultForm);
}
finally
{
formToShow.Focus();
}
}
Your calling method would look something like:

ShowForm(example1Form, typeof(frmDragDropFromExplorer))

where you would vary the type passed as the second parameter as
appropriate.

Hope this helps...

--
Matt Berther
http://www.mattberther.com
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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top