VB: Option Buttons using Process Class

I am trying to work with two radio buttons in a simple VB.net app that allows a user to select and open a PDF using the Process object. One PDF will be assigned to each radio button. Here is the code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click

            If RadioButton1.Checked = True Then

            'Use the Select Case statement to select which PDF the user will open.
            Dim test As String
            Dim path As String
            Select Case test
                Case 1
                    path = "C:\test_3\johnson.pdf"
                Case 2
                    path = "C:\test_3\Ross.pdf"
            End Select

            'Process object
            Dim psi As New ProcessStartInfo()
            psi.UseShellExecute = True
        psi.FileName = ?Case
            Process.Start(psi)
        End If

Please give me some help to write the code.

This is not homework for a class. It is self-study using books.
Mark01Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Perhaps this:

Dim psi As New ProcessStartInfo()
psi.UseShellExecute = True
psi.FileName = Path

If that does not work, you'd need to tell us exactly what your issue is. For example, do you receive an error when you run this? Or does it run, but do nothing?

Also, in order for this to work correctly the machine running the code would need a File Association for the .pdf extension. If there is no file association, then you'd have to determine which program you want to use to perform the task, and that program would need some from of "command line switch" where you could pass in the path of the file. If the program doesn't have that, you wouldn't be able to use it.
0
ste5anSenior DeveloperCommented:
In object orient programming (OOP) we have the SOLID principles.

The single responsibility principle tells us to (as also the separation of concerns principle) to split things up. Thus separate UI from logic.

This means at least something like

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click

  Dim fileName As String

  If RadioButton1.Checked = True Then
    Select Case test
    Case 1
      fileName = "C:\test_3\johnson.pdf"
    Case 2
      fileName = "C:\test_3\Ross.pdf"
    End Select

    OpenFile fileName
  End If

End Sub

Private Sub OpenFile(AFileName As String)

    Dim psi As New ProcessStartInfo()

    psi.UseShellExecute = True
    psi.FileName = AFileName
    Process.Start(psi)

End Sub

Open in new window


Problem: Take a look again how radio buttons work. They are normal placed on a parent control to work as group.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark01Author Commented:
@Scott McDaniel: I have not tried to run the app because I know the code contains numerous errors. With regard to file association, the following code does work:

 
         Dim psi As New ProcessStartInfo()
           Dim pdf1 As String
           pdf1 = "C:\test_3\johnson.pdf"
           psi.UseShellExecute = True
           psi.FileName = pdf1
           Process.Start(psi)

Open in new window


@ste5an: Thank you for the code and programming concepts. I will study my programming books and work with the code.
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Mark01Author Commented:
I am working with ste5an's code and there are no build errors. I get a "cannot start process because a file name has not been provided" error.

The following warnings were received: "Warning 1 Variable 'test' is used before it has been assigned a value. A null reference exception could result at runtime."
"Warning 2 Variable 'fileName' is used before it has been assigned a value. A null reference exception could result at runtime."

The screenshots show the errors and warnings.
ErrorWarningsThe form
0
ste5anSenior DeveloperCommented:
Yup, cause I modified your (incomplete) code. As your messages tell you test is used before assigned. This is from your original code:
Dim test As String
Dim path As String

Select Case test
Case 1
	path = "C:\test_3\johnson.pdf"
Case 2
	path = "C:\test_3\Ross.pdf"
End Select

Open in new window


The SELECT .. CASE on test makes no sense. You need to test on your radio groups value instead.

Then the error in OpenFile() tells you that you need error handling. Step one: add parameter checks. Step two: add exception handling.
0
Mark01Author Commented:
How is fileName from the SELECT .. CASE passed to AFileName? I appreciate your patience as I am the book exercises.
0
ste5anSenior DeveloperCommented:
E.g. by using LINQ:

var checkedButton = GroupBox1.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(rb => rb.Checked);

Open in new window

0
Mark01Author Commented:
The SELECT .. CASE on test makes no sense. You need to test on your radio groups value instead.
Then the error in OpenFile() tells you that you need error handling. Step one: add parameter checks. Step two: add exception handling.

I'm stuck. The book's exercises are not helpful.
0
ste5anSenior DeveloperCommented:
Then the error in OpenFile() tells you that you need error handling. Step one: add parameter checks. Step two: add exception handling.

E.g.
Private Sub OpenFile(AFileName As String)

    Dim psi As New ProcessStartInfo()

	If String.IsNullOrEmpty(AFileName) Then
	  Throw New ArgumentException("AFileName is null or empty.")
	End If
	
	If Not File.Exists(AFileName) Then
	  Throw New ArgumentException("File not found.")
	End If
	
    psi.UseShellExecute = True
    psi.FileName = AFileName
    Process.Start(psi)

End Sub

Open in new window

0
Mark01Author Commented:
Thank you for the code. I will review my books to see if I can figure out why the 'test' and 'fileName' variables have been used before they have been assigned a value.
0
Mark01Author Commented:
I appreciate your error-handling code, but I get numerous build errors. There are no build errors with your first code sample. Can we keep this simple and just work with your first code sample?

Here is the full code (no build errors):
    Private Sub OpenFile(ByVal AFileName As String)
        'EE Version 1 without error code
        Dim psi As New ProcessStartInfo()

        psi.UseShellExecute = True
        psi.FileName = AFileName
        Process.Start(psi)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'ee
        If RadioButton1.Checked = True Then
            Dim fileName As String
            Dim test As String

            Select Case test
                Case 1
                    fileName = "C:\test_3\johnson.pdf"
                Case 2
                    fileName = "C:\test_3\Ross.pdf"
            End Select

            OpenFile(fileName)
        End If
    End Sub

Open in new window

I stepped through the code and in the Locals window, 'test' and 'fileName'  remained "nothing."
0
Mark01Author Commented:
I think the following will benefit me the most:
1. Get help from this forum to resolve the errors caused by the code in comment # 42461501.
2. I will then work with the code on my own and begin to modify it.
3. Frequently review the theory and sample code in books.

I would prefer to keep this app as simple as possible and postpone working on adding parameter checks and exception handling as described in #42459179.
0
Mark01Author Commented:
Can somebody help resolve the error that is caused by the code shown below? The error is shown in 42458593 above.

Code:
    Private Sub OpenFile(ByVal AFileName As String)
        'EE Version 1 without error code
        Dim psi As New ProcessStartInfo()

        psi.UseShellExecute = True
        psi.FileName = AFileName
        Process.Start(psi)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'ee
        If RadioButton1.Checked = True Then
            Dim fileName As String
            Dim test As String

            Select Case test
                Case 1
                    fileName = "C:\test_3\johnson.pdf"
                Case 2
                    fileName = "C:\test_3\Ross.pdf"
            End Select

            OpenFile(fileName)
        End If
    End Sub

Open in new window

0
Mark01Author Commented:
Thank you Scott McDaniel and ste5an.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.