tags:

views:

165

answers:

5

I have a combobox that passes along values in text, but they can have different meanings, e.g.:

  • 8:30 - 9:30 => slot 1
  • 10:30 - 11:30 => slot 2
  • 12:30 - 13:30 => slot 3

or

  • 8:30 - 11:30 => slots 1 and 2.
  • 10:30 - 13:30 => slots 2 and 3

Basically, I want to pass these values into a database. So if the number of hours required is two, then the option will automatically display 8:30 - 11:30 or 10:30-13:30 already. Once you click the event button, I want the program to automatically calculate how many slots are required, and the number of the slots that need to be entered. At the moment, I can only think of doing it using a gargantuan amount of if statements.

Clarification:

A job will take a number of hours. 1, 2, 3, 4 or 5. The hours that each job can be worked on are 8:30-9:30, 10:30-11:30 etc. If a job takes more than one hour, it will take up more than one 'job slot'. However, the combo box I am using right now dynamically generates the possible hours and places them as text into a combo box. Therefore, if a job takes more than one hour, instead of displaying 8:30-9:30, 10:30-11:30 etc. it will display 8:30-11:30, 10:30-13:30 etc.

I'm just wondering (just had an idea), how do you take a substring in C#? If I could take a substring, I could give each possible beginning and end a value and do it another way.

Any help would be really useful.

A: 

You could use a bit mask, I guess.

  • 8:30 - 9:30 = 1
  • 10:30 - 11:30 = 2
  • 12:30 - 13:30 = 4
  • xxx - yyy = 8
  • etc...

Then you could encode arbitrary combinations of time slots with a single integer. 8:30 - 11:30 = 1 + 2 = 3. 10:30 - 13:30 = 2 + 4 = 6.

EDIT: never mind... I don't think I understand your question after all! Please clarify!

Chris Farmer
+1  A: 

Personally, I would create an object that is a time display type object:

class TimeDisplay
{
    TimeSpan startTime;
    TimeSpan duration;

    public override string ToString()
    {
        return startTime.ToString("hh:mm") + " - " + (startTime+duration).ToString("hh:mm");
    }
}

Then, I would populate the combobox with a list of these objects. That's about as much as I could gather from your question.

rein
At least you tried... Note that you don't always need ToString(), ComboBox et al have a very powerful Format event.
Henk Holterman
ToString() is useful for Debug.WriteLines too :)
rein
A: 

I'm not sure I totally understand, but I'll give it a stab.

The following function accepts an integer duration specifying the number of required slots, and outputs a list of the time strings. You could add directly to your combobox instead if you wanted.

List<string> GetSlots(int duration)
{
    var slots = new List<string>();
    int[] startTimes = new int[] { 8, 10, 12 }; // etc.

    foreach(int h in startTimes)
    {
        // assuming all start times are on the h:30 
        slots.Add(h + ":30 - " + (h + duration) + ":30"); 
    }

    return slots;
}

I used int for simplicity, but you could use TimeSpan or DateTime and add just a little complication.

Joel Potter
+1  A: 

I've left out the routine/obvious bits

class JobSlot
{
    public JobSlot (DateTime start, int hours) { ... }
    // ...
    public DateTime End { get { return Start + Hours; } } // not the correct way to add a time, but you get the picture
    public string SlotName { get { return Start.ToString () + "-" + End.ToString  (); } }
    public string ToString () { return SlotName (); }
}

LoadComboBoxWithSlots (int hours)
{
    List < JobSlot > slots;
    for ( DateTime start = FirstStartTime; start <= LastEndTime - hours; start += SlotStartTimeOffset )
        slots.Add (new JobSlot (start, hours));

    selectSlotComboBox.DataSource = slots;
    selectSlotComboBox.DisplayMember = "SlotName";
    selectSlotComboBox.Bindings.Add ("SelectedItem", bindingSource, "Slot")
}

That should get you started. You can do additional processing on the JobSlot binding to extract the number of slots, which should be slot.Hours / SlotStartTimeOffset.

XXXXX
A: 

Definately use a small class instead of trying to parse straight text within a text box. Use datetime types to identify the start and end times, throw all the instances into a list, and bind the list to the combo box.

You can override the tostring method of the class to print the start/end times, or you can have public properties on the class, and use the DisplayMember and ValueMember properties of the ComboBox.

When an item is selected from the ComboBox, you get access to the SelectedItem property of the ComboBox which will by the Type of the class you've created. Then you can use DateTime properties and Date math to determine the number of hours the particular job will take.

There should be no need for a single If statement (except maybe error handling).A quick tip if you use this method:

 if (typeof(YourClass) != combo.SelectedItem.GetType()) { // error }
Josh Smeaton