加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 更改按钮图像

发布时间:2020-12-15 21:15:35 所属栏目:百科 来源:网络整理
导读:在Form1中,我有几个按钮,上面有类似的图像,表示一个特定的设施,比方说一个网球场.但是,现在让我说我点击另一个表单中的另一个按钮来预订特定的法庭,如何将Form1上的按钮图像更改为另一个图像,以显示它已被预订? 解决方法 您可以使用事件. 预订操作将触发一
在Form1中,我有几个按钮,上面有类似的图像,表示一个特定的设施,比方说一个网球场.但是,现在让我说我点击另一个表单中的另一个按钮来预订特定的法庭,如何将Form1上的按钮图像更改为另一个图像,以显示它已被预订?

解决方法

您可以使用事件.

预订操作将触发一个事件,表明设施已被预订.
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
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读