2011-02-13 112 views
4

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

回答

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

感谢您的答复,我怎么去关于使用这个事件和事件处理程序? – pacheco 2011-02-13 13:39:52

+0

@pacheco:我在3周前的回答中添加了代码,您可能错过了它。 – 2011-03-07 08:47:24

0

改变按钮显示另一个资源的图像性能。 EG/

using namespace.Properties; 

    namespace namespace 
    { 

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

     } 
    } 

您可以节省该法院是否在数据库预订的信息,然后返回根据数据库中的一个布尔场的照片。

EG 你有一个表称为法院的数据库,因为这样的字段是ID(PK),名称和isBooked(布尔)

在页面加载时,你可以有

 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 

原谅我为sl code的代码,它只是一个例子

+0

嗨,谢谢你的回复。如果我想改变的按钮是另一种形式,该怎么办? – pacheco 2011-02-13 13:44:23

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,所以我们没有拉姆达所以这里是代码:

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

然后在Form1类的一些地方:

​​
0

简单,如果使用该您没有与数据库进行通信

Form one button点击在此处显示其他表单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";