We help IT Professionals succeed at work.

The best way to design portable self-contained modular code

Medium Priority
188 Views
Last Modified: 2012-05-06
Greetings Experts,

I am on the ground-level for a rather large project spanning over a number of related applications.  One of the similar requirements that each of these apps require is a "Quick Link" list of buttons that, when clicked, launch the other related .exes.  I have designed a simple GroupBox containing the six mentioned buttons.  I need a way to encapsulate the corresponding code linked to these buttons into a module, that I can copy and paste into then distribute to the individual project files.  I think it would be much cleaner and easier to handle to copy just one file, rather than 6 button's worth of code back and forth between projects.

Is what I am asking possible to do?  Please advise me on the proper way to go about doing this.   Thank you for your attention and advice.


Sample Button Code im looking to store in module:


Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
 
        ' <<button startup>>
        Dim filepath As String
        Dim fi As IO.FileInfo
 
        filepath = "C:\documents and settings\all users\desktop\app1.exe"
        fi = New IO.FileInfo(filepath)
        If fi.Exists Then Button1.Enabled = True
 
        filepath = "C:\documents and settings\all users\desktop\app2.exe"
        fi = New IO.FileInfo(filepath)
        If fi.Exists Then Button1.Enabled = True
        ' /<button startup>>
 
    End Sub
 
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click
 
        Dim filepath As String = "C:\documents and settings\all users\desktop\app1.exe"
        Dim appName As String = filepath.Substring(filepath.LastIndexOf("\") + 1)
        Try
            System.Diagnostics.Process.Start(filepath)
        Catch ex As Exception
            MessageBox.Show("An error has occured while trying to launch " & appName & vbCrLf & _
                            "Error Message: " & ex.Message & vbCrLf & _
                            "Error Details: " & ex.InnerException.ToString, "Error")
        End Try
 
    End Sub

Open in new window

Comment
Watch Question

omegaomegaDeveloper
CERTIFIED EXPERT

Commented:
Hello, gwosgood,

Re:
"I think it would be much cleaner and easier to handle to copy just one file, rather than 6 button's worth of code back and forth between projects."

I fully agree.  Any time you are copying code from one place to another, it is likely to cause a future maintenance problem.  It is also a sign of an opportunity to generalize and centralize the code in question, which is what you want to do.

In this particular case, I would probably convert (or encapsulate) the GroupBox into a user control and distribute that control to the projects in question.

(btw, in the second paragraph of your frmMain_Load sample code where you check the existence of app2.exe, do you actually mean to enable Button2?)

Cheers,
Randy
I agree with Randy... sort of.

Whenever I hear "cut and paste," I shudder. This is a recipe for pain in maintenance.

Encapsulate the functionality as a user control, as Randy said. However, I would tend to create a project reference to the user control project (assuming it is in some sort of source control). This will update the user control in all applications when the user control is updated.

If you'd prefer not to have to test all applications when the user control changes, then compile and distribute the DLL containing the control.
gwosgoodVP CIO

Author

Commented:
Omegaomega,

Hi, thank you for your comment.  I am glad you agree with my strategy to 'encapsulate' the groupbox's code within a modular format.  I guess I was looking for more of an example of tying the controls to the module, and they referencing them within the application.

In reference to the app2.exe question, yes, thank you, it was a classic example of copy-n-paste error.  This is exactly the type of thing I would like to avoid with the Quick List bar!   :)
gwosgoodVP CIO

Author

Commented:
I understand the concepts of the module well enough, but I am having difficulties actually designing the code.  Can someone please show me a sample of module code related to my situation, and an example of how to implement it within my main application?
Developer
CERTIFIED EXPERT
Commented:
Hello, gwosgood,

I'm not sure what sort of example you have in mind, but here's my attempt:

1. Add a UserControl to your project (I called mine "MultiButton") and give it a reasonable size and shape.
2. Add the number of buttons desired to the UserControl.  (I used five.)
3. Add the code from the snippet to the code window of the UserContrrol.  (I have modified this slightly from your original code sample to "simplify" it (a matter of opinion, of course), to solve a problem that occurs if there is no InnerException in the error handling section, and to fix the spelling of "occured".)
4. (Re)Build your project.
5. You can now go to the form designer of your application's form and drag MultiButton (or whatever you have chosen to name your UserControl) to your form.
6. Run the application and click on one of the buttons to test.  In my case, I get the message:

     An error has occurred while trying to launch app3.exe
     Error Message: The system cannot find the file specified
     Error Details: {n.a.}

which (for me) is expected.

(btw - I would never keep the default names for controls (i.e. "Button1, Button2, ...) in a real application, but this is intended to be just an example.)

Hope that helps you along the way.

Cheers,
Randy

Public Class MultiButton
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                Handles Button1.Click, Button2.Click, Button3.Click, _
                                                        Button4.Click, Button5.Click
        Dim appName As String = "app" & DirectCast(sender, Button).Name.Substring(6) & ".exe"
        Dim filepath As String = "C:\documents and settings\all users\desktop\" & appName
        ' ''Dim appName As String = filepath.Substring(filepath.LastIndexOf("\") + 1)
        Try
            System.Diagnostics.Process.Start(filepath)
        Catch ex As Exception
            Dim strErrorDetails As String = "{n.a.}"
            If (ex.InnerException IsNot Nothing) Then
                strErrorDetails = ex.InnerException.ToString
            End If
            MessageBox.Show("An error has occurred while trying to launch " & appName & vbCrLf & _
                            "Error Message: " & ex.Message & vbCrLf & _
                            "Error Details: " & strErrorDetails, "Error")
        End Try
 
    End Sub
 
End Class

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
gwosgoodVP CIO

Author

Commented:
Omegaomega,


Thank you for pointing me in the right direction.  In the end, I had to scrap my project and create a new one, using a Control Library project type instead of the Windows Application project I had been designing.  After porting my code over to the new format, I was able to run and save my user control to a .dll file, which I then imported into the toolbox.

Thank you for your assistance

gwosgood
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.