首页 > 代码库 > 委托初始化方法实例

委托初始化方法实例

                string strBirthError = "";
                foreach (DataRow dr in dtInfo.Rows)
                {
                    card = new PatientNoBedCard();
                    //选中卡片设置效果在自定义控件中声明了无返回值的泛型委托Action 将PatientNoBedCard作为泛型的类型参数然后在这使用Lambda表达式来初始化委托
                    card.SelectChanged = new Action<PatientNoBedCard>(newCard =>
                    {
                        PatientNoBedCard oldbed = this.pnlPatientCards.Controls.Cast<PatientNoBedCard>().ToList().Find(it => it.IsChecked);
                        if (oldbed != null)
                        {
                            oldbed.IsChecked = false;
                        }
                        newCard.IsChecked = true;
                    });
                   card.ShowEnterOffice = new Action<PatientNoBedCard>(ShowOfficeBed);//声明和初始化委托方法1
                    //card.ShowEnterOffice = ShowOfficeBed;//C# 2.0提出了快捷语法方法2
                    //card.ShowEnterOffice += ShowOfficeBed;//方法3 添加委托方法
                    //card.ShowEnterOffice = delegate(PatientNoBedCard card1) { };//方法4 使用匿名方法
                    //card.ShowEnterOffice = (newCard => { }); //方法5 使用Lambda表达式
 
                    //设置控件对应属性
                    //card.LevelHl = dr["TENDLEVEL"].ToString();
                    card.InHosID = dr["INHOSID"].ToString();
                    card.YBLX = dr["CHARGEKIND"].ToString();
                    card.Sex = dr["SEX"].ToString();
                    card.PatientName = dr["PATIENTNAME"].ToString();
                    ……
                }

 

委托初始化方法实例