开发者

Changing Button Image

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 butto开发者_运维百科n 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?


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
    }
}


change the image property of the button to show another resource. Eg/

   using namespace.Properties;

   namespace namespace
   {

         private void button1_Click(object sender, EventArgs e)
         {
            button1.Image = Resources.pictureName;

         }
    } 

You could save the information of whether that court is booked in the database and then return a picture depending on a bool field in the database.

EG you have a database with a table called court, as such the fields are id(pk), name and isBooked(bool)

on page load you could have

      sqlconnection con = new sqlconnection("insert connstring here");
      sqlcommand com = new sqlcommand("select isBooked from court where id = @id", con);
      con.open();
      sqldatareader reader = com.executereader();
      while(reader.read())
      { 
          bool booked = (bool)reader["isBooked"];
      }


      if(booked = true)
          //one picture as above
      else
          //another picture 

forgive me for sloppy code its just an example


simply use this if you are not communicating with database

Form one button Click for showing other form here form2

 Form2 frmtwo = new Form2(this);
  frmtwo.ShowDialog();

then in second form constructor add this

 Form Frmtwo;
        public Form2(Form frm)
        {
            InitializeComponent();
            Frmtwo = frm;
        }

then add this code to the button click where you want to show image in first form

PictureBox pc = (PictureBox)Frmtwo.Controls["pictureBox1"];
pc.ImageLocation  = @"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\1.jpg";


Ok i assume you are launching the Booking Form from the Form1 where you are showing the button with Court. So the code will look somthing like this in the Form1 (Where you have that court image button):

FormBooking frm = new FormBooking();
frm.Controls["nameofbooking_button"].Click += (se,ev) =>{
            //The button with the court image
            CourtImageButton.Image = //Your new image
     }
frm.Show();

Now when that button will be clicked on the booking form you Court Image Button will change its image.

And if its 2005 that is .Net 2.0 so we have no lambda so here is the code:

FormBooking frm = new FormBooking();
frm.Controls["nameofbooking_button"].Click += new EventHandler(ChangeImage);
frm.Show();   

then some where in your Form1 class:

private void ChangeImage(object sender, EventArgs e)
{
            //The button with the court image
            this.CourtImageButton.Image = Image.FromFile(@"C:\courtbooked.png");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜