更改按钮图像

时间:2011-02-13 13:32:20

标签: c# .net winforms .net-2.0

在Form1中,我有几个按钮,上面有类似的图像,表示一个特定的设施,比方说一个网球场。但是,现在让我说我点击另一个表单中的另一个按钮来预订特定的法庭,如何将Form1上的按钮图像更改为另一个图像,以显示它已被预订?

4 个答案:

答案 0 :(得分:1)

您可以使用该事件。

预订操作将触发一个事件,表明设施已被预订 Form1将为其注册一个事件处理程序,并更改按钮的图像以反映设施的状态。

修改(如何对事件执行此操作):

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

答案 1 :(得分:0)

更改按钮的图像属性以显示其他资源。例如/

   using namespace.Properties;

   namespace namespace
   {

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

         }
    } 

您可以保存该法院是否在数据库中预订的信息,然后根据数据库中的bool字段返回图片。

EG 你有一个名为court的数据库,因此字段是id(pk),name和isBooked(bool)

在页面加载时你可以

      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 

原谅我的草率代码只是一个例子

答案 2 :(得分:0)

好的,我假设你正在从Form1中启动预订表格,你在那里显示有法院的按钮。所以代码在Form1中看起来像这样(你有那个宫廷图像按钮的地方):

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

现在,当您在预订表单上点击该按钮时,您可以更改其图像按钮。

如果它的2005是.Net 2.0所以我们没有lambda所以这里是代码:

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

然后在Form1类中的某些位置:

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

答案 3 :(得分:0)

如果您没有与数据库通信

,只需使用它

表格一键点击此处显示其他表格form2

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

然后在第二个表单构造函数中添加此

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

然后将此代码添加到按钮单击,以便以第一种形式显示图像

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