Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Populate ComboBox with DayOfWeek enum?

Populate ComboBox with DayOfWeek enum?

Scheduled Pinned Locked Moved C#
questioncsharpvisual-studiotutorial
4 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kbalias
    wrote on last edited by
    #1

    Good day I am using Visual Studio 2010 to develop a Windows app. I am busy with a module where the user can specify setting for days of the week, for example Mondays the lunch duration is 1 hour, Tuesday it is 30 minutes, and so on. I want to display the days of the week (Sunday, Monday, …) in a ComboBox so that the user can select a day. The DisplayMember must be the name, e.g Monday The ValueMember must be the ordinal number of the day in the DayOfWeek enum, e.g. 1 How do I populate the ComboBox using the DayOfWeek enumerator? I tried:

    private void Form1_Load(object sender, EventArgs e)
    {
    for (int j = 0; j < 7; j++)
    {
    comboDay.Items.Add((DayOfWeek)j);
    }
    }

    private void comboDay_SelectedIndexChanged(object sender, EventArgs e)
    {
    MessageBox.Show("Selected Day = " + comboDay.SelectedValue);
    }

    This shows the DayOfWeek names in the combobox, but the SelectedValue is always null. Any suggestions on better ways of doing this? Thanks. Kobus

    P 1 Reply Last reply
    0
    • K kbalias

      Good day I am using Visual Studio 2010 to develop a Windows app. I am busy with a module where the user can specify setting for days of the week, for example Mondays the lunch duration is 1 hour, Tuesday it is 30 minutes, and so on. I want to display the days of the week (Sunday, Monday, …) in a ComboBox so that the user can select a day. The DisplayMember must be the name, e.g Monday The ValueMember must be the ordinal number of the day in the DayOfWeek enum, e.g. 1 How do I populate the ComboBox using the DayOfWeek enumerator? I tried:

      private void Form1_Load(object sender, EventArgs e)
      {
      for (int j = 0; j < 7; j++)
      {
      comboDay.Items.Add((DayOfWeek)j);
      }
      }

      private void comboDay_SelectedIndexChanged(object sender, EventArgs e)
      {
      MessageBox.Show("Selected Day = " + comboDay.SelectedValue);
      }

      This shows the DayOfWeek names in the combobox, but the SelectedValue is always null. Any suggestions on better ways of doing this? Thanks. Kobus

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Use SelectedItem instead of SelectedValue.

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

      K 1 Reply Last reply
      0
      • P Pete OHanlon

        Use SelectedItem instead of SelectedValue.

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        K Offline
        K Offline
        kbalias
        wrote on last edited by
        #3

        Thanks I feel a fool for not thinking of that. Regards. Kobus

        P 1 Reply Last reply
        0
        • K kbalias

          Thanks I feel a fool for not thinking of that. Regards. Kobus

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          It's always the way. As soon as someone else looks at it, you see it.

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups