繁体   English   中英

在组合框中显示文本,后跟日期,月份和年份的数组

[英]Display text in the comboboxes followed by array of day, month, and year

我想创建日,月和年。 表单中有3个comboboxes ,并且这3个comboboxes划分如下:日,月和年。 但是,我想将文本添加到comboboxes的第一个列表中,然后再添加(或之前)天,月和年的数组。 因此,该文本将显示为index 0 但是,当我将文本添加到comboboxes ,第一个组合框是当天的日期,可以从第一个组合框中选择列表,但其余部分则无法选择列表。

我怎样才能解决这个问题?

这是我将文本添加到comboboxes

private void SetValues()
    {
        DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);

        this.comboBox1.Items.Add("Date");

        this.comboBox2.Items.Add("Month");

        this.comboBox3.Items.Add("Year");

        for (int i = 1; i < 32; i++)
        {
            this.comboBox1.Items.Add(i.ToString());
        }

        for (int i = 1; i < 13; i++)
        {
            this.comboBox2.Items.Add(info.GetMonthName(i));
        }

        for (int i = 1980; i < DateTime.Now.Year + 1; i++)
        {
            this.comboBox3.Items.Add(i.ToString());
        }

        SetCalendarIndex();
    }

这是完整的代码:

public partial class AgeConfirmation : Form
{
    public AgeConfirmation()
    {
        InitializeComponent();

        this.comboBox1.Leave += new EventHandler(CheckPossibleCalendar);

        this.comboBox2.Leave += new EventHandler(CheckPossibleCalendar);

        this.comboBox3.Leave += new EventHandler(CheckPossibleCalendar);
    }

    private void AgeConfirmation_Load(object sender, EventArgs e)
    {
        SystemManager.SetFullScreen(this);

        SetProperties();

        SetValues();
    }

    private void SetProperties()
    {
        this.pictureBox1.Size = new Size(SystemManager._workingRectangle.Width, SystemManager._workingRectangle.Height);

        this.label1.Text = "Date of Birth:";

        this.label1.Location = new Point((SystemManager._workingRectangle.Width / 2) - 75, (SystemManager._workingRectangle.Height / 2) - 100);

        this.label1.Parent = this.pictureBox1;

        this.comboBox1.Location = new Point((SystemManager._workingRectangle.Width / 2) - 175, (SystemManager._workingRectangle.Height / 2) - 50);

        this.comboBox2.Location = new Point((SystemManager._workingRectangle.Width / 2) - 50, (SystemManager._workingRectangle.Height / 2) - 50);

        this.comboBox3.Location = new Point((SystemManager._workingRectangle.Width / 2) + 75, (SystemManager._workingRectangle.Height / 2) - 50);

        this.button1.Parent = this.pictureBox1;

        this.button1.Location = new Point((SystemManager._workingRectangle.Width / 2) - 40, SystemManager._workingRectangle.Height / 2);
    }

    private void Submit(object sender, EventArgs e)
    {
        if (this.comboBox1.SelectedIndex.Equals(0) || this.comboBox2.SelectedIndex.Equals(0) || this.comboBox3.SelectedIndex.Equals(0))
        {
            SystemManager.ShowMessageBox("Date, Month, and Year required, please fill them!", "Information", 1);
        }

        else
        {
            int visitorAge = this.comboBox3.SelectedIndex - DateTime.Now.Year;

            SystemManager.AddAge(visitorAge, Convert.ToString(DateTime.Now));
        }
    }

    private void CheckPossibleCalendar(object sender, EventArgs e)
    {
        int day = DateTime.DaysInMonth(Convert.ToInt32(this.comboBox3.Text), this.comboBox2.SelectedIndex + 1);

        if (day <= this.comboBox1.SelectedIndex)
        {
            SystemManager.ShowMessageBox("You have entered wrong Date, Month, or Year!", "Warning", 2);

            SetCalendarIndex();
        }
    }

    private void SetValues()
    {
        DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);

        this.comboBox1.Items.Add("Date");

        this.comboBox2.Items.Add("Month");

        this.comboBox3.Items.Add("Year");

        for (int i = 1; i < 32; i++)
        {
            this.comboBox1.Items.Add(i.ToString());
        }

        for (int i = 1; i < 13; i++)
        {
            this.comboBox2.Items.Add(info.GetMonthName(i));
        }

        for (int i = 1980; i < DateTime.Now.Year + 1; i++)
        {
            this.comboBox3.Items.Add(i.ToString());
        }

        SetCalendarIndex();
    }

    private void SetCalendarIndex()
    {
        this.comboBox1.SelectedIndex = 0;

        this.comboBox2.SelectedIndex = 0;

        this.comboBox3.SelectedIndex = 0;
    }
}

任何帮助将非常感激!

谢谢。

编辑:

在此处输入图片说明

从上图可以看到,可以选择第一个组合框,其余的则不能。 当我运行该程序时,它将检测到第一个选定的组合框。 假设我选择了第一个组合框,当我选择第一个组合框时,在重新启动程序之前无法再选择其余组合框(第二个和第三个组合框)。

发生这种情况是因为我添加了代码:

this.comboBox1.Items.Add("Date");

this.comboBox2.Items.Add("Month");

this.comboBox3.Items.Add("Year");

到我没有添加上面的代码时,它已经可以工作了。 但是,组合框的第一个选定值将是数字,文本和数字。 我希望组合框的第一个选定值将是上面的代码,但是一旦我添加了上面的代码。 我从上图的下面描述的问题出现了。

将您的代码更改为以下代码并进行测试。

 public partial class AgeConfirmation : Form
    {
        public AgeConfirmation()
        {
            InitializeComponent();

        }

        private void AgeConfirmation_Load(object sender, EventArgs e)
        {
            SystemManager.SetFullScreen(this);

            SetProperties();

            SetValues();
        }

        private void SetProperties()
        {
            pictureBox1.Size = new Size(SystemManager._workingRectangle.Width, SystemManager._workingRectangle.Height);

            label1.Text = "Date of Birth:";

            label1.Location = new Point((SystemManager._workingRectangle.Width / 2) - 75, (SystemManager._workingRectangle.Height / 2) - 100);

            label1.Parent = pictureBox1;

            comboBox1.Location = new Point((SystemManager._workingRectangle.Width / 2) - 175, (SystemManager._workingRectangle.Height / 2) - 50);

            comboBox2.Location = new Point((SystemManager._workingRectangle.Width / 2) - 50, (SystemManager._workingRectangle.Height / 2) - 50);

            comboBox3.Location = new Point((SystemManager._workingRectangle.Width / 2) + 75, (SystemManager._workingRectangle.Height / 2) - 50);

            button1.Parent = pictureBox1;

            button1.Location = new Point((SystemManager._workingRectangle.Width / 2) - 40, SystemManager._workingRectangle.Height / 2);
        }

        private void Submit(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex.Equals(0) || comboBox2.SelectedIndex.Equals(0) || comboBox3.SelectedIndex.Equals(0))
            {
                SystemManager.ShowMessageBox("Date, Month, and Year required, please fill them!", "Information", 1);
            }

            else
            {
                int visitorAge = comboBox3.SelectedIndex - DateTime.Now.Year;

                SystemManager.AddAge(visitorAge, Convert.ToString(DateTime.Now));
            }
        }

        private void CheckPossibleCalendar(object sender, EventArgs e)
        {
            int day = DateTime.DaysInMonth(Convert.ToInt32(comboBox3.Text), comboBox2.SelectedIndex + 1);

            if (day <= comboBox1.SelectedIndex)
            {
                SystemManager.ShowMessageBox("You have entered wrong Date, Month, or Year!", "Warning", 2);

                SetCalendarIndex();
            }
        }

        private void SetValues()
        {
            DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);

            comboBox1.Items.Add("Date");

            comboBox2.Items.Add("Month");

            comboBox3.Items.Add("Year");

            for (int i = 1; i < 32; i++)
            {
                comboBox1.Items.Add(i.ToString());
            }

            for (int i = 1; i < 13; i++)
            {
                comboBox2.Items.Add(info.GetMonthName(i));
            }

            for (int i = 1980; i < DateTime.Now.Year + 1; i++)
            {
                comboBox3.Items.Add(i.ToString());
            }

            SetCalendarIndex();
        }

        private void SetCalendarIndex()
        {
            comboBox1.SelectedIndex = 0;

            comboBox2.SelectedIndex = 0;

            comboBox3.SelectedIndex = 0;
        }
    }

我已经测试了下面的代码,并且工作正常。

    using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;

namespace TestingDevExpress
{
    public partial class Form01 : DevExpress.XtraEditors.XtraForm
    {
        public Form01()
        {
            InitializeComponent();

        }

        private void SetProperties()
        {

            label1.Text = "Date of Birth:";

            label1.Parent = pictureBox1;

            button1.Parent = pictureBox1;

        }

        private void Submit(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex.Equals(0) || comboBox2.SelectedIndex.Equals(0) || comboBox3.SelectedIndex.Equals(0))
            {

                MessageBox.Show("Date, Month, and Year required, please fill them!", "Information");
            }

            else
            {
                int visitorAge = comboBox3.SelectedIndex - DateTime.Now.Year;

                MessageBox.Show(visitorAge + Convert.ToString(DateTime.Now));
            }
        }

        private void CheckPossibleCalendar(object sender, EventArgs e)
        {
            int day = DateTime.DaysInMonth(Convert.ToInt32(comboBox3.Text), comboBox2.SelectedIndex + 1);

            if (day <= comboBox1.SelectedIndex)
            {
                MessageBox.Show("You have entered wrong Date, Month, or Year!", "Warning");

                SetCalendarIndex();
            }
        }

        private void SetValues()
        {
            DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);

            comboBox1.Items.Add("Date");

            comboBox2.Items.Add("Month");

            comboBox3.Items.Add("Year");

            for (int i = 1; i < 32; i++)
            {
                comboBox1.Items.Add(i.ToString());
            }

            for (int i = 1; i < 13; i++)
            {
                comboBox2.Items.Add(info.GetMonthName(i));
            }

            for (int i = 1980; i < DateTime.Now.Year + 1; i++)
            {
                comboBox3.Items.Add(i.ToString());
            }

            SetCalendarIndex();
        }

        private void SetCalendarIndex()
        {
            comboBox1.SelectedIndex = 0;

            comboBox2.SelectedIndex = 0;

            comboBox3.SelectedIndex = 0;
        }

        private void Form01_Load_1(object sender, EventArgs e)
        {
            SetProperties();

            SetValues();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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