Home > Archive > Visual Basic > September 2004 > How can I improve this code?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How can I improve this code?
|
|
| Atreju 2004-09-29, 3:55 am |
| 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 2004-09-29, 3:55 am |
| 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
"Atreju" <me@somewhere.org> wrote in message
news:a4ckl05vnf36bsqcd02ofk0s6056par7b3@
4ax.com...
>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 2004-09-29, 3:55 am |
| 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
"Atreju" <me@somewhere.org> wrote in message
news:a4ckl05vnf36bsqcd02ofk0s6056par7b3@
4ax.com...
>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.
>
| |
| Larry Serflaten 2004-09-29, 3:55 am |
|
"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
| |
| Randy Birch 2004-09-29, 3:55 am |
| Moving the code to a function provides no benefit ... it is indeed verbose
in this case.
--
Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/
"Atreju" <me@somewhere.org> wrote in message
news:a4ckl05vnf36bsqcd02ofk0s6056par7b3@
4ax.com...
: 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.
:
| |
| David Youngblood 2004-09-29, 3:55 am |
| "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).
>
> 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
|
|
|
|
|