如何使OpenFileDialog变量全局?

时间:2015-01-21 03:55:55

标签: c#

我刚刚开始使用C#,作为一个项目,我决定尝试制作一个图像转换器,但我似乎无法在button1上下文中获取变量“open”,我不是在寻找关于我的代码有多糟糕的评论,我刚刚开始..我只想完成它并添加它,我将在以后改进代码,谢谢。那么如何让它可以访问呢?

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

2 个答案:

答案 0 :(得分:1)

您可以将变量移出方法并进入类,使其成为类的实例字段。然后任何方法都能看到它。但是,这种方法存在一些问题:

  1. OpenFileDialog与任何模态对话框一样,在完成后应予以处理。在实例字段中存储引用会延长其生命周期,直到您下次创建新表单或表单关闭时为止,以先到者为准。
  2. 你真正需要的只是文件名,而不是OpenFileDialog所带来的所有其他数据和资源,所以它很浪费。
  3. 从用户体验的角度来看,最好让用户有机会保存到不同的文件。您最好向用户提供SaveFileDialog,并使用当前选定的文件名进行初始化。
  4. 坚持使用您当前的UI设计,以下是正确的方法:

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

    请注意使用using确保OpenFileDialog实例在不再需要时正确处置。

答案 1 :(得分:0)

C#没有全局变量,但您可以为此设置解决方法,

public class GlobalObjects
{
    private static OpenFileDialog ofd;

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

}

并称之为,

var fileDlg = GlobalObjects.OpenFileDlg;