Changing Button Image

11,824

You can use events for that.

Booking action will fire an event that will indicate that a facility is booked.
Form1 will have an event handler registered to it and change the button's image to reflect the state of the facility.

Edit (how to do this with events):

public class FacilityStateChangeEventArgs : EventArgs
{
    public FacilityStateChangeEventArgs(bool booked)
    {
        this.Booked = booked;
    }

    public bool Booked { get; protected set; }
    // ... other properties if you need them
}

public class Facility
{
    private bool booked = false;
    public bool Booked
    {
        get
        {
            return this.booked;
        }
        protected set
        {
            if (this.booked == value) return;


            // Changes the state and fires the event.
            this.booked = value;
            FireChange();
        }
    }

    public event EventHandler<FacilityStateChangeEventArgs> StateChange;

    // You will use this method when booked gets changed
    public void FireChange()
    {
        if (this.StateChange != null) this.StateChange(this, new FacilityStateChangeEventArgs(this.Booked));
    }
}

// The form with the image button.
public class FormWithButton
{
    Button button1 = new Button();

    public void Whatever()
    {
        // You will get the facility from your bussiness instances.
        Facility facility = new Facility();

        facility.StateChange += new EventHandler<FacilityStateChangeEventArgs>(facility_StateChange);
    }

    void facility_StateChange(object sender, FacilityStateChangeEventArgs e)
    {
        if (e.Booked) button1.Image = null; // booked image
        else button1.Image = null; // free image
    }
}
Share:
11,824
pacheco
Author by

pacheco

Updated on June 04, 2022

Comments

  • pacheco
    pacheco almost 2 years

    In Form1 i have several buttons with a similar image on them to indicate a particular facility, let's say a tennis court. However let's say now i click on another button in another form to book that particular court, how can i change the button image on Form1 to another image, in order to show that it is booked?

  • pacheco
    pacheco about 13 years
    Thanks for the reply, how do i go about using this events and event handler?
  • pacheco
    pacheco about 13 years
    Hi, thanks for the reply. What if the button that i would like to change is in another form?
  • Daniel Casserly
    Daniel Casserly about 13 years
    The database option is the best because the information would be taken from the db. Check out the edit ill put on the original post
  • pacheco
    pacheco about 13 years
    Hi, i'm thinking of doing it through db, after you suggested it but i'm using OleDb though, how would the codes differ?
  • pacheco
    pacheco about 13 years
    Hi, thanks for the reply. How do i insert my image if let's say its called courtbooked.png in resources in the this.CourtImageButton.Image = ? statement?
  • pacheco
    pacheco about 13 years
    I get the following error: Object reference not set to an instance of an object.
  • Daniel Casserly
    Daniel Casserly about 13 years
    not sure not used oledb, i beleive it is just the sqlconnection/command and datareader etc changes to oledbsqlconnection but i have never used oledb so i cant really help sorry
  • pacheco
    pacheco about 13 years
    this line: frm.Controls["Court1Button"].Click += new EventHandler(ChangeImage);
  • Shekhar_Pro
    Shekhar_Pro about 13 years
    make sure that Court1Button is the name of the Button and not the Instance name
  • Jaroslav Jandek
    Jaroslav Jandek about 13 years
    @pacheco: I have added code to my answer 3 weeks ago, you may have missed it.
  • Alireza Noori
    Alireza Noori about 13 years
    On the designer right click on the button (Court1Button as mentioned) and select Properties. Then look for name it is the name that you should use in the specified answer. (Note that it should be the exact same name (case sensitive)