如何使 OpenFileDialog 变量 Global 全局



我最近刚刚开始使用 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);
        }
    }
}
}

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

  1. OpenFileDialog ,就像任何模态对话框一样,应在完成处理后处理掉。将引用存储在实例字段中会延长其生存期,直到下次创建新引用或关闭表单时(以先到者为准)。
  2. 您真正需要的是文件名,而不是OpenFileDialog携带的所有其他数据和资源,因此这是浪费。
  3. 从用户体验的角度来看,最好让用户有机会保存到不同的文件。您最好向用户提供SaveFileDialog,使用当前选定的文件名进行初始化。

坚持您当前的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实例时正确释放。

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;

最新更新