For the more advanced programmer that just wants to copy/paste and figure it out on their own, here is the code that we will be working with:
" private void btnUpload_Click(object sender, EventArgs e)
{
//Call a browse files dialog box
//intialize variables
string strFileLocation = "";
string strDirectory = "C:/";
//Open the dialog box
OpenFileDialog openFileDialog1 = new OpenFileDialog();
Console.WriteLine("Directory: " + strDirectory);
openFileDialog1.InitialDirectory = strDirectory;
openFileDialog1.Filter = "Image Files (*.jpg, *.jpeg, *.bmp, *.tiff, *.giff, *.mpeg, *.png)" +
"|*.jpg, *.jpeg, *.bmp, *.tiff, *.giff, *.mpeg, *.png|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = false;
openFileDialog1.RestoreDirectory = true;
DialogResult Result = openFileDialog1.ShowDialog();
//Load the image to the load image picture box
if (Result == DialogResult.OK)
{
strFileLocation = openFileDialog1.FileName;
lblMatchPerc.Text = "0";
}
pbxLoadImage.ImageLocation = strFileLocation;
}"
As you can see, the code is documented (a very good habit to get into) so that it is easier to follow along with. For those that need the help, that is why this article is here, and I will break it down section by section. Some of the basics that you will need for this to work are a button to call the function (or whatever event you want tied to this), a string to hold the file location (strLocation), and a place to put the information in your project (pbxLoadImage - a picture box). This particular program handles and manipulates images for a picture matching software. This code is geared toward images, but you can use the same snippet to work with any type of file. I will explain how to change the file types and customize it for your project. To get started, you will need to set up the calling event. For this snippet, it is a button called btnUplaod and labeled "Upload Image". Once you have the event set up, you will then need to establish the container for the uploaded file, and the string to hold the file path that the user selected. The string "strDirectory" can be any position on the user's computer. This is just where the dialog box starts. If your users will be using the E: drive more often, you may want it to start there. This dialog box does not actually hold the file. It is only used to establish a file location. It is the remaining code that will handle the actual file.
Once you have the containers set up, you will need to get to the "meat and potatoes" of this snippet. "OpenFileDialog openFileDialog1 = new OpenFileDialog();" will establish the file dialog box. This dialog box will be named openFileDialog1. If there are multiple dialogs opened with this file and you need to differentiate between them, you can change this name. Usually, only one dialog box will be open at a time, so the default name will be fine. This really depends on your program. You can also set this up as a function and use the same code for every dialog box that you need opened. You will need to establish a string writer to grab the text from the box. This is the next line of code, "Console.WriteLine("Directory: " + strDirectory);". This allows the program to set the starting location of the dialog box, and the next line opens the dialog in that directory. The next line may take a little tinkering. This will give you a default file type to search for. The "openFileDialog1.Filter =" line on this code is setup to accept image files as the default. You can change this to any file type that you need for your code. The first value "Image Files" will be what the drop down list is labeled followed by a textual version of the files. This means that the drop down menu in the dialog box for file type will read; ""Image Files (*.jpg, *.jpeg, *.bmp, *.tiff, *.giff, *.mpeg, *.png)". This tells the user what types of files you would prefer or limit them to using. The code after the vertical line is what the computer will display. To avoid confusion, you want these two file type lists to match. When this is selected in the drop down list, it will only display the file type that is listed after the vertical line ("|*.jpg, *.jpeg, *.bmp, *.tiff, *.giff, *.mpeg, *.png). I, personally, like to add the "All Files" option open to my users. This allows them to load files that may not be saved as the type for my application. With image files, there are new types released all the time, so this option should be set.
For some programs, there are limitations and errors that can be produced if the wrong type is used. This is a good reason to not add the "All Files" option. You will need to decide this on your own. The Filterindex option will determine the file type to start with. Option 1 (the default in this version) will select the image files established in the above line. If you want the default to be "All Files" then you will need to change this to 2. Since only one image can be processed at a time in my code, the MultiSelect option is set to false. If you want users to be able to select multiple files, change this value to true. The RestoreDirectory option will start the dialog box on the last known drive or folder the user was in. This makes it easier for the user if they are using this program multiple times. They will not have to navigate to the folder where they store these files each time they open the dialog box. You can change this value to false if you want it to begin in the default drive each time. Once all of the parameters are set, the ".ShowDialog()" command will launch your dialog box.
Now that you have created your dialog box, you will need to handle the results that the user inputs. Since the user may opt for another method to add the files or decide not to add a file at all, you will need to create an If Statement. This will eliminate any errors if the user closes the dialog box without selecting a file. The DialogBox has an output of DialogResult.OK when the user uses the OK button. This means that the user clicked the OK button and not the Cancel or close buttons. If this value is true, you can continue with your code. If not, it will wait for the user to use a different method, or eventually use the dialog box. The strFileLocation variable will store the file location. The other command in this statement is only used in the program that I created this dialog box for and can be deleted or changed to something that you want to occur if the user has selected a file for use in your program. This brings us to the last task for this box command to perform. You will need to store the file location to your program and upload the file that the user selected. For this particular program it is a picture box (pbxLoadImage). You will need to assign the file to the control that you want to use it. Whether it is a picture box for an image or a string variable for text, this is where you add the file to your code. You can then manipulate the file and do what you need to do with it.
This is a rather simple process that can be handled in many different ways. This method is quite simple and very self-explanatory. Remember to comment your code though. Not only does it make it simpler for you to find and correct mistakes, but if you share it with anyone (which all code should be shared with everyone), it will be easier for them to adapt the code to their program. If we can all share code together, just imagine how simple it would be to create a better computer world. You can still charge your clients and users, but as coders, we need to stand together. Please pass the snippets on.
Published by RH
- How to Make a Printable File List in Windows XpIn this tutorial you will see how simple it is to make a printable file list of any directory in Windows Xp.
- How to Program a Starkey S Series Hearing Aid by PhoneHere is a look at the latest Starkey S Series hearing aid and how to program it with your phone.
- CSharp .Net Tutorial: Picture Box Load Icon
- How to Create Postcards in Publisher
- Working with Your Website Files in Your C-panel
- Create a Web Gallery of Your Photos Using ACDSee Pro 2
- Free File Sharing Websites
- How to Embed Existing Files into Microsoft Word
- How to Insert a Hyperlink into an E-mail Using Microsoft Outlook 2003 or 2007



