需要得到一个等效的项目在列表框中,当我输入一个数字

本文关键字:一个 数字 列表 输入 项目 | 更新日期: 2023-09-27 18:03:11

我是c#的新手,我一直在努力处理我的程序。我在做一个别名生成器程序。当我在textbox(例如1-12)中输入一个数字时,我应该在ListBox中得到等效的Item。

lblAlias.Content = lstBox1.Items(Convert.ToInt16)(txtMOB.Text)[0 - 1].ToString();

需要得到一个等效的项目在列表框中,当我输入一个数字

我肯定是不是一个WPF程序员(我使用WinForms,对不起),但它看起来像…

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        int month, day;
        if (int.TryParse(txtMOB.Text, out month) && int.TryParse(txtDOB.Text, out day))
        {
            if(month >= 1 && month <= 12 && day >= 1 && day <=31)
            {
                lblAlias.Content = ((ListBoxItem)lstBox1.Items[month - 1]).Content.ToString()
                    + " " + ((ListBoxItem)lstBox2.Items[day - 1]).Content.ToString();
            }
            else
            {
                MessageBox.Show("Month must be between 1 and 12, and Day must be between 1 and 31!");
            }
        }
        else
        {
            MessageBox.Show("Invalid Month and/or Day!");
        }
    }