1

I've just recently started with C# and as a project I decided I would try and make a image converter, but I can't seem to get the variable "open" accessible in my button1 context, I'm not looking for comments on how bad my code is, I am just starting.. I just want to get this done and add on to it, I will improve the code later on, thanks. So how can I make it accessible?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public void button2_Click(object sender, EventArgs e)
    {
        OpenFileDialog open = new OpenFileDialog();
        // image filters
        open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
        if (open.ShowDialog() == DialogResult.OK)
        {
            // image in picture box
           pictureBox1.Image = new Bitmap(open.FileName);
           pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
            // image file path
           string path = Directory.GetCurrentDirectory();
           textBox1.Text = Path.GetDirectoryName(open.FileName);
        }

       }


    public void Form1_Load(object sender, EventArgs e)
    {
        this.AutoSize = true;
        this.AutoSizeMode = AutoSizeMode.GrowAndShrink;

        flowLayoutPanel1 = new FlowLayoutPanel();
        flowLayoutPanel1.AutoSize = true;
        flowLayoutPanel1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
        this.Controls.Add(flowLayoutPanel1);
    }

    public void button1_Click(object sender, EventArgs e)
    {
        int selectedIndex = comboBox1.SelectedIndex;
        Object selectedItem = comboBox1.SelectedItem;

        if ((string)comboBox1.SelectedItem == "*.jpg")
        {
           pictureBox1.Image.Save(@"" + textBox1.Text + open.FileName + "", System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }
}
}
Shad
  • 64
  • 8
  • 1
    mind you to please share the idea of making `open` accessible? Reason for asking is that it doesn't feel right from seeing your code. The variable could just be declared as a class variable or you provide a private property; however, the `OpenFileDialog` is a modal dialog and returns a result - you normally wouldn't want to access the variable afterwards again, especially not from a different context. – Quality Catalyst Jan 21 '15 at 04:09

2 Answers2

1

You could move the variable out of the method and into the class, making it an instance field of the class. Then any method would be able to see it. However, there are a few things wrong with that approach:

  1. The OpenFileDialog, like any modal dialog, should be disposed of when you are done with it. Storing the reference in an instance field extends its lifetime until the next time you create a new one or your form is closed, whichever comes first.
  2. All you really need is the file name, not all the other data and resources that the OpenFileDialog carries around, so it's wasteful.
  3. From a user experience point of view, it would be better to give the user a chance to save to a different file anyway. You would be better off presenting a SaveFileDialog to the user, initializing with the currently selected file name.

Sticking with your current UI design, the following is the right way to do what you want:

private string fileName;

public void button2_Click(object sender, EventArgs e)
{
    using (OpenFileDialog open = new OpenFileDialog())
    {
        // image filters
        open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
        if (open.ShowDialog() == DialogResult.OK)
        {
            // image in picture box
           filename = open.FileName;
           pictureBox1.Image = new Bitmap(open.FileName);
           pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
           // image file path
           string path = Directory.GetCurrentDirectory();
           textBox1.Text = Path.GetDirectoryName(open.FileName);
        }
    }
}

public void button1_Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(fileName))
    {
        return;
    }

    int selectedIndex = comboBox1.SelectedIndex;
    Object selectedItem = comboBox1.SelectedItem;

    if ((string)comboBox1.SelectedItem == "*.jpg")
    {
       pictureBox1.Image.Save(@"" + textBox1.Text + fileName + "", System.Drawing.Imaging.ImageFormat.Jpeg);
    }
}

Note the use of using to ensure that the OpenFileDialog instance is disposed of properly when it's no longer needed.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
1

C# don't have global variables, but you can have a workaround for that,

public class GlobalObjects
{
    private static OpenFileDialog ofd;

    public static OpenFileDialog OpenFileDlg
    {
        get
        {
            if (ofd == null)
                ofd = new OpenFileDialog();
            return ofd;
        }
    }

}

and call it like,

var fileDlg = GlobalObjects.OpenFileDlg;
Rohit Prakash
  • 1,975
  • 1
  • 14
  • 24
  • @Shad didn't ask for global variables. Your suggestion adds a static concept to the dialog usage which he didn't ask for either. Plus keep on mind: the dialogs are often used once only and dispose afterwards. – Quality Catalyst Jan 21 '15 at 04:34