0

我正在开发我的 C# ADO.NET 应用程序。我已将我的 SQL Server 数据库与 C# 应用程序连接起来,并且可以执行简单的 CRUD 操作。我想让我的应用程序在我的数据库中有人生日时打开我的提醒表单,所以我进行了查询,今天所有生日的人都在我的查询中,并且使用提醒表单中的属性我更改了标签的文本与过生日的人的姓名和姓氏。现在我只是不知道当我的查询中有多个人生日时如何更改下一个标签的文本...我不知道如何在我的 foreach 循环中获取下一个元素...

这是我的代码:

    Form2 forma = new Form2();
    TBirthDayEntities today_born = new TBirthDayEntities();

    public Form1()
    {
        InitializeComponent();
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Interval = 1000;
        timer1.Enabled = true;
        timer1.Start();
    }

    private Boolean provjera_rodj()
    {
        Boolean flag = false;
        int cnt = 0;


        IQueryable<TBD> query;
        using (var data = new TBirthDayEntities())
        {
            query = (from x in data.TBD
                     where x.BirthDay.Day == System.DateTime.Now.Day && x.BirthDay.Month == System.DateTime.Now.Month
                     select x);

            foreach (var x in query)
            {
                    today_born.TBD.Add(x);
                    cnt += 1;
                    flag = true;


            }
        }





        switch (cnt)
        {
            case 1:
                {
                    foreach (var x in today_born.TBD)
                    {
                                forma.p_label2 = x.FName + " " + x.LName;
                    }
                    break;
                }
            case 2:
                {
                    foreach (var x in today_born.TBD)
                    {
                        forma.p_label2 = x.FName + x.LName;
                        forma.p_label3 = x.FName + x.LName; //wrong
                    }
                    break;
                }

        }



        return flag;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Boolean flag = provjera_rodj();
        if (flag == true)
        {
            forma.Show();
            timer1.Stop();
        }

    }
4

1 回答 1

0
    switch (cnt)
    {
        case 1:
        case 2:
            {
                var lstLabel = new List<Label>()
                {
                    forma.p_label2
                    , forma.p_label3
                };
                for(int i = 0; i < today_born.TBD.Count; i++)
                {
                    var x in today_born.TBD[i];

                    lstLabel[x].Text = x.FName + x.LName;
                }
                break;
            }

    }

EDIT:

    switch (cnt)
    {
        case 1:
        case 2:
            {
                var lstLabel = new List<Action<string>>()
                {
                    new Action<string>(s =>forma.p_label2 = s)
                    , new Action<string>(s =>forma.p_label3 = s)
                };
                for(int i = 0; i < today_born.TBD.Count; i++)
                {
                    var x = today_born.TBD[i];

                    lstLabel[x](x.FName + x.LName);
                }
                break;
            }

    }
于 2013-07-12T07:33:34.490 回答