[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

The best way to design portable self-contained modular code

Posted on 2009-02-09
6
Medium Priority
?
162 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

0
Comment
Question by:gwosgood
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:omegaomega
ID: 23592130
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
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 23592224
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.
0
 
LVL 2

Author Comment

by:gwosgood
ID: 23592262
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!   :)
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 2

Author Comment

by:gwosgood
ID: 23602729
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?
0
 
LVL 12

Accepted Solution

by:
omegaomega earned 1500 total points
ID: 23604564
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

0
 
LVL 2

Author Comment

by:gwosgood
ID: 23622687
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
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month18 days, 5 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question