使用PictureBox保存表单的状态?

时间:2022-02-10 07:10:37

I have a small program in winforms; it's just a program where I can have pictures, but I have a problem. When I have a picture, I close the program and I open it again, the pictures don't stay where I have put them, in the PictureBox.

我在winforms有一个小程序;这只是一个我可以拍照的程序,但我有一个问题。当我有一张照片时,我关闭程序并再次打开它,图片不会停留在PictureBox中。

More simply, I want to keep the state when I close the program, like saving.

更简单的说,我想在关闭程序时保持状态,比如保存。

Here my code :

这是我的代码:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }       
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            OpenFileDialog f = new OpenFileDialog();
            f.ShowDialog();


            var chemin = f.FileName;
            pictureBox1.ImageLocation = chemin;

        }

    }
}

Please help me, I can't go on with this problem...

请帮帮我,我不能继续这个问题......

1 个解决方案

#1


0  

The simplest way to do this is to use the Application Settings. Right-click on your Project and select Properties. Then go to Settings. In the right hand side, you will see a panel with a grid with only one line. Change Setting in the Name column to ImageLocation and leave the other three values (Type, Scope and Value) as their defaults (string, user and blank).

最简单的方法是使用“应用程序设置”。右键单击您的项目,然后选择属性。然后转到“设置”。在右侧,您将看到一个面板,其中只有一行网格。将Name列中的Change设置更改为ImageLocation,并将其他三个值(Type,Scope和Value)保留为默认值(字符串,用户和空白)。

In design view of your form under properties double-click the FormClosing event to create a new handler. Now enter:

在属性的设计视图下,双击FormClosing事件以创建新的处理程序。现在输入:

        if (pictureBox1.ImageLocation != null)
        {
            Properties.Settings.Default.ImageLocation = pictureBox1.ImageLocation;
            Properties.Settings.Default.Save();
        }

Finally in the constructor for the form enter the following after InitializeComponent():

最后在表单的构造函数中,在InitializeComponent()之后输入以下内容:

        if (Properties.Settings.Default.ImageLocation != null)
        {
            pictureBox1.ImageLocation = Properties.Settings.Default.ImageLocation;
        }

HTH

HTH

#1


0  

The simplest way to do this is to use the Application Settings. Right-click on your Project and select Properties. Then go to Settings. In the right hand side, you will see a panel with a grid with only one line. Change Setting in the Name column to ImageLocation and leave the other three values (Type, Scope and Value) as their defaults (string, user and blank).

最简单的方法是使用“应用程序设置”。右键单击您的项目,然后选择属性。然后转到“设置”。在右侧,您将看到一个面板,其中只有一行网格。将Name列中的Change设置更改为ImageLocation,并将其他三个值(Type,Scope和Value)保留为默认值(字符串,用户和空白)。

In design view of your form under properties double-click the FormClosing event to create a new handler. Now enter:

在属性的设计视图下,双击FormClosing事件以创建新的处理程序。现在输入:

        if (pictureBox1.ImageLocation != null)
        {
            Properties.Settings.Default.ImageLocation = pictureBox1.ImageLocation;
            Properties.Settings.Default.Save();
        }

Finally in the constructor for the form enter the following after InitializeComponent():

最后在表单的构造函数中,在InitializeComponent()之后输入以下内容:

        if (Properties.Settings.Default.ImageLocation != null)
        {
            pictureBox1.ImageLocation = Properties.Settings.Default.ImageLocation;
        }

HTH

HTH