2011-02-13 287 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";