Improving the GrabAPicture Application (Part 5)

This week’s post discusses the third item on the list in the Improving the GrabAPicture Application (Part 1), which is to add a browse button to frmMain. This is a design issue that I shouldn’t need to address now. I previously raked Microsoft across the coals for changing a perfectly good user interface in my Examining the Calculator in Windows 7 post, well, this is an examination of a flaw in my original design for GrabAPicture that shouldn’t have occurred either. A reader wrote and told me about it and the moment I read the message I knew that the omission was of the sort that anyone working with the application long enough would notice. Unfortunately, I had gotten into the bad habit of cutting and pasting the paths to images I wanted to test, so it was an omission that I didn’t notice . So, here in all its glory is the post for the browse button that should have originally appeared on frmMain.

The first thing you’ll need to do is modify the user interface to include the button. Begin by changing the txtWallpaperURI.Size.Width property value to 160. Then, add a new button called btnGetResource with these property values:

 

  • Location.X = 168
  • Location.Y = 32
  • Size.Width = 24
  • Size.Height = 20
  • TabIndex = 5
  • Text = …

The new user interface will look like this:

GrabAPictureUpdate0501

You also need to add a new OpenFileDialog component named OFD. This is a non-visual component, so you want see it in the user interface. Assign these property values to OFD:

 

  • Filter = BMP Files|*.BMP|JPG Files|*.JPG|GIF Files|*.GIF|PNG Files|*.PNG|All Graphics Files|*.BMP;*.JPG;*.GIF;*.PNG
  • FilterIndex = 1


Double click btnGetResource to create a new Click event handler. Add this code to the event handler:

Private Sub btnGetResource_Click(sender As System.Object, _
                                 e As System.EventArgs) _
                              Handles btnGetResource.Click
 
   ' If the user selects something and clicks OK.
   If OFD.ShowDialog() = DialogResult.OK Then
 
      ' Assign the value to the textbox.
      txtWallpaperURI.Text = OFD.FileName
   End If
End Sub

The resulting code makes it possible to obtain a file for use on the Desktop using an Open dialog box as shown here.

GrabAPictureUpdate0502

Even though this isn’t a huge change in functionality, it’s a necessary change. Making things easy on the user is always an essential part of creating a great application. Let me know if you have any questions about this post (especially my decision to highlight a design flaw in this manner) at John@JohnMuellerBooks.com.