Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General 2 / September 2004



Tip: Looking for answers? Try searching our database.

How can I improve this code?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Atreju - 29 Sep 2004 04:58 GMT
I have several commandbuttons in my application. Basically, each one
launches a different form (mdi child, but that's not relevant).

The commandbutton is a control array.
Therefore I determine which index was clicked and launch the
appropriate form.

I would like to make this more efficient in the following ways:
At any given time, I may want to re-arrange these buttons in such a
way that the index of any particular one may point to a different
form. I would like to perhaps build a class or Enum (I really don't
know how to approach it) whereby the commandbutton can simply respond
to a click event by something like the following:
myForm.Do Index- where myForm is something that has been pre-defined,
and index will tell the class? (or whatever) which one was clicked,
and the form will be determined by that number.

Perhaps I am blowing this out of proportion.. I just don't know enough
to determine for myself what the most efficient way of doing this is.

I just want the flexibility to be able to rearrange things without
having to pick around the click event to rearrange the response.

What I'm doing now is a select case statement and in each index value
I launch the appropriate form by the usual frmThisForm.Show.
This is quite verbose for starters, there's a lot of redundancy as
well.

Would a function be in order? Such as

Private Function LaunchForm(byVal iIndex as Integer) as Form
 Select Case iIndex
   Case 0
     LaunchForm = frmThisForm
   Case 1
     LaunchForm = frmThatForm
 End Select
End Function

Then call the function? This seems just as verbose.

I would think there would be some way to define a list of Forms and
relate them to an index.

I hope I have adequately described what I need to do.

Please advise.
Khmer_lovers - 29 Sep 2004 05:35 GMT
Hi,

Why don't you call your function to handle the buttons caption. This way you
don't have to rewrite a lot of codes.

'Launch form method
Private Function LaunchForm(Byval strButtonCaption as string) as Form
   Select  Case UCase(strButton)
       Case "LAUNCH_THIS_FORM":  Set LaunchForm = frmThisForm
       Case "LAUNCH_THAT_FORM": Set LaunchForm = frmThatForm
       Case Else:    Set LaunchForm = Nothing
   End select
End Function

'Button click event
Private Sub LaunchButton_Click(Byval Index as Integer)
   Dim xForm as Form
   Set xForm = LaunchForm(LaunchButton(Index).Caption)

   'Does the clicked button has associated form
   If xForm Is Nothing = False Then
         Msgbox "Button has no associated form"
   Else
        Call xForm.Show
   End If
End Sub

Hope this help and good lucks
>I have several commandbuttons in my application. Basically, each one
> launches a different form (mdi child, but that's not relevant).
[quoted text clipped - 43 lines]
>
> Please advise.
Khmer_lovers - 29 Sep 2004 05:42 GMT
Hi,

Sorry i make a few mistakes in the previous post, here the better version.
:)

Why don't you modify your function to handle the button's caption. This way
you
don't have to rewrite a lot of codes.

'Launch form method
Private Function LaunchForm(Byval strButtonCaption as string) as Form
   Select  Case UCase(strButton)
       Case "LAUNCH_THIS_FORM":  Set LaunchForm = frmThisForm
       Case "LAUNCH_THAT_FORM": Set LaunchForm = frmThatForm
       Case Else:    Set LaunchForm = Nothing
   End select
End Function

'Button click event
Private Sub LaunchButton_Click(Byval Index as Integer)
   Dim xForm as Form
   Set xForm = LaunchForm(LaunchButton(Index).Caption)

   'Does the clicked button has associated form
   If xForm Is Nothing = True Then
         Msgbox "Button has no associated form"
   Else
        Call xForm.Show
   End If
End Sub

>I have several commandbuttons in my application. Basically, each one
> launches a different form (mdi child, but that's not relevant).
[quoted text clipped - 43 lines]
>
> Please advise.
Randy Birch - 29 Sep 2004 06:11 GMT
Moving the code to a function provides no benefit ... it is indeed verbose
in this case.

Signature

Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/

: I have several commandbuttons in my application. Basically, each one
: launches a different form (mdi child, but that's not relevant).
[quoted text clipped - 43 lines]
:
: Please advise.
Larry Serflaten - 29 Sep 2004 06:11 GMT
"Atreju" <me@somewhere.org> wrote
> I have several commandbuttons in my application. Basically, each one
> launches a different form (mdi child, but that's not relevant).
>
> I would like to make this more efficient in the following ways:

> I just want the flexibility to be able to rearrange things without
> having to pick around the click event to rearrange the response.

Well you'll have to pick at something to make changes, but the
question might be, did you want to make changes at design time,
or runtime?   Here is a method that will allow both.  To help
keep the code documented add an Enum to list all the available
forms you want to show:

Private Enum ProjectForms
 Form1
 Form2
 ' ...
End Enum

At design time, or runtime, set the TAG property of the buttons
to the value of the forms they are attached to:
(Runtime is best here to keep things documented in the code)

Private Sub Form_Load()
 Buttons(0).Tag = ProjectForms.Form1
 Buttons(1).Tag = ProjectForms.Form2
 ' ...
End Sub

When the user hits a button, you can then pass that tag value to
your select case routine:

Private Sub Buttons_Click(Index As Integer)
 NewForm(Buttons(Index).Tag).Show
End Sub

Private Function NewForm(ByVal Index As Long) As Form
 Select Case Index
 Case ProjectForms.Form1
   Set NewForm = New Form1
 Case ProjectForms.Form2
   Set NewForm = New Form2
 ' ...
 End Select
End Function

In that method, each click of the button brings up a new instance of
that type of form.   And, you can re-arrange which buttons call which
forms at runtime, by again, adjusting their Tag values.

HTH
LFS
David Youngblood - 29 Sep 2004 06:46 GMT
> I have several commandbuttons in my application. Basically, each one
> launches a different form (mdi child, but that's not relevant).
>
> The commandbutton is a control array.
> Therefore I determine which index was clicked and launch the
> appropriate form.

You might also take a look at the Forms.Add method.
But, you won't be able to use vb's built-in form variables with this method.

Private Sub Form_Load()
   cmdButton(0).Tag = "Form2"
   cmdButton(1).Tag = "Form3"
End Sub

'* If you don't need a reference to the new form
Private Sub cmdButton_Click(Index As Integer)
   Forms.Add(cmdButton(Index).Tag).Show
End Sub

'* Or, if you do need a reference
Private Sub cmdButton_Click(Index As Integer)
   Dim MyForm As Form
   Set MyForm = Forms.Add(cmdButton(Index).Tag)
   MyForm.Show
End Sub

David
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.