C#删除按钮消息问题 [英] C# Remove Button Message Problem

查看:64
本文介绍了C#删除按钮消息问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone!



i在Windows窗体应用程序中创建一个图片框并制作两个按钮1:浏览图像和2:删除图像都正常工作。 但是现在我想要两件事,



1:启动应用程序时如果用户点击删除按钮然后删除按钮显示错误消息和错误我应该在文本形式中的删除按钮下方显示消息。 请先选择图像。然后选择图像后消息自动消失。



2:如果用户选择图像然后再次将其删除第二次按下按钮然后再次出现相同的消息图片信息应自动消失。



如果有人对此有任何想法,请帮助我。

Hello Everyone!

i create a picture Box in Windows Forms Application and make two button 1:Browse image and 2:Remove Image both work Right. but now i want Two Things,

1:on starting the Application if user click on Remove Button Then Remove Button Show Error Message and Error Message Should Me Show Below the Remove Button In The Form Of Text. "Please Select Image First". And Then message Automatically Disappear after Selecting Image.

2:If User Select image and then remove it and again Second time Press Button Then Same Message Will Occur Again and after Selecting image Message Should Be Automatically Disappear.

Please Help me If Anyone Have Any Idea About this.

推荐答案

如果您使用布尔变量,例如 isImageSelected 来记录图像选择,这非常简单。在 isImageSelected == false (这是它的初始值)时,删除图像按钮应该无效(并且应显示错误消息)。另一方面,如果 isImageSelected == true ,则删除图像按钮应该有效。

当然你必须正确更新这样一个布尔变量(例如,它应该在图像移除后立即设置为false)。
It is really simple if you use a boolean variable, say isImageSelected to record image selection. On isImageSelected==false (that's its initial value) the Remove Image button should be ineffective (and the error message should be shown). On the other hand, if isImageSelected==true then the Remove Image button should be effective.
Of course you have to properly update such a boolean variable (for instance it should be set to false immediately after image removal).


你让生活更轻松如果您创建一个ViewModel类,其中包含与View(您的表单)相关的所有内容的属性。虽然看起来有点冗长,但它允许您轻松扩展或更改View的功能。下面的课程可能是一个起点。您可以使用这些属性通过BindingSource绑定到窗体上控件的不同属性。 INotifyPropertyChanged实现可确保在ViewModel更改时自动更新View。

You make life easy for yourself if you create a ViewModel class which contains properties for everything relevant to the View (your Form). Although it seems a little verbose, it allows you to easily extend or change the functionality of your View. The class below could be a starting point. You use these properties to bind to the different properties of your controls on the Form, through a BindingSource. The INotifyPropertyChanged implementation makes sure that your View is updated automatically when your ViewModel changes.
public class MainViewModel : INotifyPropertyChanged
{
    private Image mImage;
    
    public event PropertyChangedEventHandler PropertyChanged;

    public Image Image
    {
        get { return mImage; }
        set
        {
            if (mImage != value)
            {
                mImage = value;
                OnPropertyChanged();
                OnPropertyChanged("RemoveButtonEnabled");
                OnPropertyChanged("ErrorMessageVisible");
            }
        }
    }

    public bool RemoveButtonEnabled
    {
        get
        {
            return Image != null;
        }
    }

    public bool ErrorMessageVisible
    {
        get
        {
            return Image == null;
        }
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}


这篇关于C#删除按钮消息问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆