Hack Forums
[C#] C# OFD [Intermediate Level Tutorial] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] C# OFD [Intermediate Level Tutorial] (/showthread.php?tid=755260)



C# OFD [Intermediate Level Tutorial] - Pulser - Oct 20, 2010

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);
            
            


          
        }



* - corey - Oct 20, 2010

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!


* - Pulser - Oct 20, 2010

(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.


* - xSilent - Oct 20, 2010

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. 🤝


* - Pulser - Oct 20, 2010

(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!


* - tflo - Oct 21, 2010

You should show text reading as well as that is one of the most commonly used methods in programming.


* - Zer0Fact0r - Oct 21, 2010

Why are you making a new variable, File, to store the file name?
And...openFileDialog1.InitialDirectory = "C:";...bad idea.


* - Pulser - Oct 21, 2010

(Oct 21, 2010 12:36 AM)Vanquish 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.