如果用户对图片框中的图像进行了更改,例如使用旋转 R、旋转 L 按钮,然后用户单击退出按钮,我希望它显示一个消息框,上面写着“您想将更改保存到以下物品?” 我似乎做不到,这就是我所拥有的。
问问题
188 次
3 回答
2
您需要设置一个标志来确定是否单击了按钮。然后检查该标志Exit_Click
:
private void Exit_Click(object sender, EventArgs e)
{
if (_rotrButtonClicked &&
MessageBox.Show("Would you like to save this file?",
"Media Player",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// save the changes
}
}
您可以将标志声明为private
表单上的字段:
private bool _rotrButtonClicked;
然后在RotRButton_Click
集合中:
_rotrButtonClicked = true;
于 2013-11-04T16:02:19.160 回答
1
您应该将逻辑封装到一个可以为您跟踪更改状态的类中。例如
public class ImageMutator
{
private bool HasChanged { get; set; }
private PictureBox myPictureBox {get;set}
public ImageMutator(PictureBox pictureBox)//Most abstract type that has functionality
{
myPictureBox = pictureBox;
}
public void RotateRight()
{
HasChanged = true;
//manipulate myPictureBox
}
public void RotateLeft()
{
HasChanged = true;
//manipulate myPictureBox
}
//other methods
public void ConfirmChange()
{
if (HasChanged)
{
var save = (MessageBox.Show("Would you like to save this file?", "Media Player", MessageBoxButtons.YesNo) == DialogResult.Yes);
if (save)
{
//Save
}
}
}
}
然后您可以将此类添加为表单的成员并在退出时确认:
public partial class Form1 : Form
{
private ImageMutator mutator ;/private member "has-a" relationship
public Form1()
{
InitializeComponent();
mutator = new ImageMutator(pictureBox);//whatever image type is
}
private void Exit_Click(object sender, EventArgs e)
{
mutator.ConfirmChange();//Only saves if mutation occurred
}
}
于 2013-11-04T16:17:48.877 回答
0
当用户单击旋转按钮时,您必须跟踪更改。在退出按钮中,您只需检查变量的值以了解是否有更改:
private bool hasChanges = false;
private void RotRButton_Click(object sender, EventArgs e) {
hasChanges = true;
}
private void Exit_Click(object sender, EventArgs e)
{
if (hasChanges)
{
if (MessageBox("Would you like to save this file?", "Media Player", MessageBoxButtons.YesNo) == DialogResult.Yes) {
//Do something
}
}
}
于 2013-11-04T16:07:15.850 回答