简体   繁体   中英

C# How can I make a loop which changes 3 labels

I am working on a project which has 3 labels and 3 buttons. ButtonBiezacy changes the labels that they show previous/current/next day of the week. How can I make that the buttonPoprzedni will be changing all 3 labels properly (so If I click buttonPoprzedni once it will change labelWczoraj from "wczoraj: piątek" into "wczoraj: czwartek" etc. and for each click it will be changing)?

Is using the switch in this situation was a good idea?

public string NazwaDnia(int zmienna)
{
    switch (zmienna)
    {
        case 1:
            return "poniedziałek";

        case 2:
            return "wtorek";

        case 3:
            return "środa";

        case 4:
            return "czwartek";

        case 5:
            return "piątek";

        case 6:
            return "sobota";

        case 7:
            return "niedziela";

        default:
            return "";

    }
}

private void updateLabelWczoraj()
{
    DateTime Dzisiaj = DateTime.Now;
    int NumerDnia = (int)Dzisiaj.DayOfWeek;
    labelWczoraj.Text = "wczoraj: " + NazwaDnia(NumerDnia - 1);
}

private void updateLabelDzisiaj()
{
    DateTime Dzisiaj = DateTime.Now;
    int NumerDnia = (int)Dzisiaj.DayOfWeek;
    labelDzisiaj.Text = "dzisiaj: " + NazwaDnia(NumerDnia);
}

private void updateLabelJutro()
{
    DateTime Dzisiaj = DateTime.Now;
    int NumerDnia = (int)Dzisiaj.DayOfWeek;
    labelJutro.Text = "jutro: " + NazwaDnia(NumerDnia + 1);
}

private void buttonBieżący_Click(object sender, EventArgs e)
{
    buttonPoprzedni.Enabled = true;
    buttonNastępny.Enabled = true;
    updateLabelWczoraj();
    updateLabelDzisiaj();
    updateLabelJutro();
}

private void buttonPoprzedni_Click(object sender, EventArgs e)
{

}

private void buttonNastępny_Click(object sender, EventArgs e)
{

}

You could use this

     Dictionary<int, string> daysDictionary = new Dictionary<int, string>()
                                                {
                                                   {1, "poniedziałek"},
                                                   {2, "wtorek"},
                                                   {3, "środa"},
                                                   {4, "czwartek"},
                                                   {5, "piątek"},
                                                   {6, "sobota"},
                                                   {7, "niedziela"}
                                                };
     return daysDictionary[zmienna];

You could also make the dictionary a static field on your class so it doesn't get created every time you click the button (although performance probably won't really be an issue).

You could also put the whole Dictionary declaration on one line, but I think it is more readable this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM