开发者

How to set a combobox value

Yesterday i asked this question: Get current time and set a value in a combobox

Now i´m having another issue that i have to deal with. In the question that i asked yesterday i had three times zones in my combobox:

06:00 - 14:00 the combobox will get the value TimeZone 1

14:01 - 22:00 the combobox will get the value TimeZone 2

22:01 - 05:59 the combobox will get the value TimeZone 3

开发者_运维知识库

And i also have a datTimePicker were the user can choose a date. Let´s say that the user choose: 2011-02-02 and in the timezone combobox choose TimeZone 2. If this happens i want to create a method that only search for the time. between 14:01 - 22:00 (wich are TimeZone 2). And if the user choose TimeZone 3 it will only search for the time between 22:01 - 05:59.

Any ideas?


You could create a Period holder class containing the Type, the from and to time. This can be inserted into a collection like a IList which can be used as datasource for your combobox. Something like this example:

    [Test]
    public void CompositeDictionary()
    {

        //Create a dictionary of periods
        IList<PeriodHolder> periodHolders = new List<PeriodHolder>();
        periodHolders.Add(new PeriodHolder("Type1", "06:00", "14:00"));
        periodHolders.Add(new PeriodHolder("Type2", "14:01", "22:00"));
        periodHolders.Add(new PeriodHolder("Type3", "22:01", "05:59"));

        //Create the test combobox in a test form
        Form testForm = new Form();
        ComboBox testComoBox = new ComboBox();
        testForm.Controls.Add(testComoBox);

        testComoBox.DataSource = periodHolders;
        testComoBox.ValueMember = "PeriodName"; //The name of the Name property in PeriodHolder
        testComoBox.DisplayMember = "PeriodString"; //The name of the PeriodString property in PeriodHolder
        testComoBox.SelectedIndex = 1;


        string selectedType = testComoBox.ValueMember[testComoBox.SelectedIndex].ToString(); //Get the type so you can lookup in dictionary


        foreach (PeriodHolder periodHolder in periodHolders)
        {
            if (periodHolder.PeriodName == selectedType)
            {
                //Use period holder for whatever you need

                string fromTime = periodHolder.TimeFrom; //Extract fromTime from periodholder
                string toTime = periodHolder.TimeTo;     //Extract toTime from periodholder

                Console.WriteLine(periodHolder.PeriodString);

                break; //you've found it -- Don't look anymore
            }
        }



    }

    public class PeriodHolder
    {
        public PeriodHolder(string name, string from, string to)
        {
            PeriodName= name;
            TimeFrom =from;
            TimeTo = to;
        }
        public string PeriodName { get; set; }
        public string TimeFrom { get; set; }
        public string TimeTo { get; set; }

        public string PeriodString
        {
            get
            {
                return TimeFrom + " - " + TimeFrom;
            }
        }
    }


Create a method with 3 input parameters like GetTime(DateTimePickerValue, TimeZone, SearchedTimeDate) and return value bool.

Check out method TimeSpan to calculate time and dates.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜