Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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.
Posted
Comments
[no name] 7-Sep-14 15:42pm    
An "idea" about what? How is this a problem to begin with? What have you tried? what was the problem with what you have tried? Are you unable to check to see if the image in your picture box is null? Are you unable to show some text as an error message? Are you unable to hide the error message?
Member 11004573 7-Sep-14 16:06pm    
Respected Was Aday,

Thank your For Reply i am unable to hide my Error when the Image is Selected in The Picture Box.

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).
 
Share this answer
 
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.
C#
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));
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900