8 Replies - 146 Views - Last Post: 07 February 2012 - 10:42 PM Rate Topic: -----

Topic Sponsor:

#1 Razzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-January 12

Question: Picture viewer

Posted 07 February 2012 - 08:40 PM

Basically.. I want to know if doing this is okay. If not, what other ways can I do it?

If the user pressed cancel once the Open file dialog executed, it gave an error.
If I wrap it up around using Try-Catch-EndTry, the error doesn't come up. But I don't know if that's the best way to handle it. Am I wrong? This is my first try at doing a Windows Forms Application so any other tips you can give me would be great!

Thanks in advance ^^

Public Class Form1

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

        OpenFileDialog1.ShowDialog()
        Try
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Question: Picture viewer

#2 trevster344  Icon User is offline

  • Slick.
  • member icon

Reputation: 124
  • View blog
  • Posts: 873
  • Joined: 16-March 11

Re: Question: Picture viewer

Posted 07 February 2012 - 09:05 PM

You can do that, or you can check the value of the integer returned by the dialog.

 if OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.Abort then
messagebox.show("yep you aborted... you bugger")
end if



The real reason it's crashing is because you're not checking the integer returned by the showdialog and no matter what assigning the image.fromfile to the filename property which could be null for all you know. Not necessarily the dialog.

This post has been edited by trevster344: 07 February 2012 - 09:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 Razzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-January 12

Re: Question: Picture viewer

Posted 07 February 2012 - 09:47 PM

I don't want the program to pop up any other messages if the user presses cancel on the dialog.. I mean, most programs don't, unless it's something special.
Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is offline

  • It's been so long, without this feeling
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,533
  • Joined: 24-July 11

Re: Question: Picture viewer

Posted 07 February 2012 - 09:49 PM

Yes… a better solution would be this:

Public Class Form1

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

       If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK
        Try 'this code will only be executed if the result matches the integer in the Enum dialogresult.
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
        Catch ex As Exception
'it wasn't displaying the error because it was "caught"
        End Try
End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub
End Class


I've commented the necessary sections in the code to describe their purposes. You only check what you need to check, and that way you can execute code according to a condition being met in the If block.
By "catching" the error you prevent it from displaying one of those ugly .NET framework dialog boxes and you can instead say:
Catch ex As Exception
MsgBox("oohZ - something went wrong there")


You can then display personalized error messages and such.
Was This Post Helpful? 1
  • +
  • -

#5 trevster344  Icon User is offline

  • Slick.
  • member icon

Reputation: 124
  • View blog
  • Posts: 873
  • Joined: 16-March 11

Re: Question: Picture viewer

Posted 07 February 2012 - 09:54 PM

Personally I'd also add a check to make sure the filename property of the opendialog is not empty too on top of that. I do apologize for my lacking example, but as stated it is only an example not to be taken specifically to context. Also do make sure you understand the showdialog function of the opendialog is a function, not a sub routine, therefore it returns a value and this is the value you are looking to match to an enumeration. Hope we could help you.
Was This Post Helpful? 0
  • +
  • -

#6 Razzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-January 12

Re: Question: Picture viewer

Posted 07 February 2012 - 10:03 PM

Yes both your posts are really helpful! Thank you for that. I just finished watching a 25-videos tutorial series about Visual Basic, and I've just started working on random apps to see if I understand everything well.. Which clearly I don't xD. Doing my best though.
Was This Post Helpful? 0
  • +
  • -

#7 trevster344  Icon User is offline

  • Slick.
  • member icon

Reputation: 124
  • View blog
  • Posts: 873
  • Joined: 16-March 11

Re: Question: Picture viewer

Posted 07 February 2012 - 10:10 PM

You can't understand just by seeing and hearing, the saying in one ear out the other comes to my mind. You have to see, hear, and do to learn, and like any other skill you have to practice on a regular basis or else you'll just forget it.
Was This Post Helpful? 0
  • +
  • -

#8 Razzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-January 12

Re: Question: Picture viewer

Posted 07 February 2012 - 10:13 PM

I know. I just used the video tutorials as a start. I learnt the basic syntax from them, and wrote the code myself to understand what it was doing. But other than the examples given in the videos, I don't know much. That's what I'm trying to improve now ^^ (Bob Tabor's videos. Maybe you know them or heard of them?)
Was This Post Helpful? 0
  • +
  • -

#9 trevster344  Icon User is offline

  • Slick.
  • member icon

Reputation: 124
  • View blog
  • Posts: 873
  • Joined: 16-March 11

Re: Question: Picture viewer

Posted 07 February 2012 - 10:42 PM

Unfortunately I do not but they sound worth checking out haha, I learned the old cheese block way, bits and pieces here and there, and reading the same subject from 10 different authors a trillion times lol.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1