Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

C# OFD [Intermediate Level Tutorial]

C# Open File Dialog


What Is It:
It does what it says, it opens a file!

Getting Started:
Begin by making a button, a picturebox, and an OpenFileDialog.

Double Click the button and insert the following.
Code
string File = "";

          

            openFileDialog1.Title = "HackForums";
            openFileDialog1.InitialDirectory = "C:";
          

            openFileDialog1.Filter = "JPEG|*.jpg";
            


            openFileDialog1.ShowDialog();

            File = openFileDialog1.FileName;
            pictureBox1.Image = Image.FromFile(File);


string File = "";
Creates a string variable with no text.


openFileDialog1.Title = "HackForums";
openFileDialog1.InitialDirectory = "C:";
openFileDialog1.Filter = "JPEG|*.jpg";


The first line sets the title on top of the box.
The second line sets the directory it will begin searching in.
The third line sets the filter, the dialog will only except jpeg picture files.

Underneath that insert this:
Code
openFileDialog1.ShowDialog();

            File = openFileDialog1.FileName;
            pictureBox1.Image = Image.FromFile(File);

The first line shows the Dialog.
The second line says that the previous empty string is now the name of the file you open.
The third line sets your picture box equal to the picture file you just opened!

Spoiler (Click to View)

Final Code:
Code
private void button1_Click(object sender, EventArgs e)
        {
            string File = "";

          

            openFileDialog1.Title = "HackForums";
            openFileDialog1.InitialDirectory = "C:";
          

            openFileDialog1.Filter = "JPEG|*.jpg";
            


            openFileDialog1.ShowDialog();

            File = openFileDialog1.FileName;
            pictureBox1.Image = Image.FromFile(File);
            
            


          
        }
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
Hey, Thanks for posting these all for new members. They are useful for people just starting off. But you don't need to make a new thread for each snippet. You could combine all your snippets into one super thread. Hope this isn't too much of a hassle. Thanks!
(Oct 20, 2010 07:48 PM)corey Wrote:  Hey, Thanks for posting these all for new members. They are useful for people just starting off. But you don't need to make a new thread for each snippet. You could combine all your snippets into one super thread. Hope this isn't too much of a hassle. Thanks!

Well, considering each of my threads is on a a separate topic I feel that one long thread would be very cluttered and confusing. I am going to be posting dozens of new tutorials and I somehow feel that 1 thread would be too long for everything. I will however, create a thread that lists all of my tutorials by experience category after I post some more!

Thanks for the feedback, by the way.
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
Well

Code
openFileDialog1.ShowDialog();

    File = openFileDialog1.FileName;
    pictureBox1.Image = Image.FromFile(File);

That needs to be changed to

Code
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
    File = openFileDialog1.FileName;
    pictureBox1.Image = Image.FromFile(File);
    }

That makes it so it will not work unless they press OK so it won't give a null value if they close the dialog, but good tut for beginners. 🤝
(Oct 20, 2010 08:23 PM)xSilent Wrote:  Well

Code
openFileDialog1.ShowDialog();

    File = openFileDialog1.FileName;
    pictureBox1.Image = Image.FromFile(File);

That needs to be changed to

Code
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
    File = openFileDialog1.FileName;
    pictureBox1.Image = Image.FromFile(File);
    }

That makes it so it will not work unless they press OK so it won't give a null value if they close the dialog, but good tut for beginners. 🤝

Thanks a ton for the feedback. I will be sure to change that soon!
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
You should show text reading as well as that is one of the most commonly used methods in programming.
Why are you making a new variable, File, to store the file name?
And...openFileDialog1.InitialDirectory = "C:";...bad idea.
(Oct 21, 2010 12:36 AM)tflo Wrote:  You should show text reading as well as that is one of the most commonly used methods in programming.

Sure vanquish, I will make that tutorial today.
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by dummycartSep 9, 2021 03:38 AM 1 146 Sep 9, 2021 03:38 AM
Last Post: Lindros
[Python] by CiataLearnerDec 22, 2018 02:27 PM 1 212 Dec 22, 2018 02:27 PM
Last Post: notjapanese
[C] by Adam2017Dec 3, 2018 03:06 PM 2 167 Dec 3, 2018 03:06 PM
Last Post: Adam2017