Code to add times to dropdownlist

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cirene

    Code to add times to dropdownlist

    I have a dropdownlist. I want to fill it with times (using vb.net code).

    I want the entries in the ddl to be from "8:00 AM" to "5:00 PM",
    incrementing every half hour.

    I know I could type in the values manually but something tells me that I
    could loop thru a time variable and add 1/2 hr, etc... then add it in a
    loop. (More efficient.)

    Any coders want to give this a shot? The less code the better. Thanks!


  • sloan

    #2
    Re: Code to add times to dropdownlist



    myDDL.Items.Add (new ListItem ("Something1 ", "Something2 ") );

    You can write a loop to go through the items.

    for(int i = 8 ; i <= 17 ; i ++ )
    {
    for (int j = 0 ; j < 2 ; j ++ )
    {

    //now you have
    //8 and 0 ( "8:00" )
    //8 and 1 (" 8:30")
    string hourAndMinute = Convert.ToStrin g(i);
    if(j == 0)
    {
    hourAndMinute += ":00";

    }
    else
    {
    hourAndMinute +=":30";
    }

    myDDL.Items.Add (new ListItem ( hourAndMinute , hourAndMinute ) );
    }

    }


    I'll leave it to you for the AM PM. And the convert (from) military time
    hours.

    But that should get you started.


    "Cirene" <cirene@nowhere .comwrote in message
    news:e2z2QFD7IH A.3696@TK2MSFTN GP04.phx.gbl...
    >I have a dropdownlist. I want to fill it with times (using vb.net code).
    >
    I want the entries in the ddl to be from "8:00 AM" to "5:00 PM",
    incrementing every half hour.
    >
    I know I could type in the values manually but something tells me that I
    could loop thru a time variable and add 1/2 hr, etc... then add it in a
    loop. (More efficient.)
    >
    Any coders want to give this a shot? The less code the better. Thanks!
    >

    Comment

    • Cirene

      #3
      Re: Code to add times to dropdownlist

      Thanks. So that I do not have to do too much conversion I was wondering if
      there was a way to have a time variable of 8:00 AM and just add 30 min to it
      over and over again until you reach 5:30 PM. Hmmm...

      "sloan" <sloan@ipass.ne twrote in message
      news:%23ty78OD7 IHA.3480@TK2MSF TNGP03.phx.gbl. ..
      >
      >
      myDDL.Items.Add (new ListItem ("Something1 ", "Something2 ") );
      >
      You can write a loop to go through the items.
      >
      for(int i = 8 ; i <= 17 ; i ++ )
      {
      for (int j = 0 ; j < 2 ; j ++ )
      {
      >
      //now you have
      //8 and 0 ( "8:00" )
      //8 and 1 (" 8:30")
      string hourAndMinute = Convert.ToStrin g(i);
      if(j == 0)
      {
      hourAndMinute += ":00";
      >
      }
      else
      {
      hourAndMinute +=":30";
      }
      >
      myDDL.Items.Add (new ListItem ( hourAndMinute , hourAndMinute ) );
      }
      >
      }
      >
      >
      I'll leave it to you for the AM PM. And the convert (from) military time
      hours.
      >
      But that should get you started.
      >
      >
      "Cirene" <cirene@nowhere .comwrote in message
      news:e2z2QFD7IH A.3696@TK2MSFTN GP04.phx.gbl...
      >>I have a dropdownlist. I want to fill it with times (using vb.net code).
      >>
      >I want the entries in the ddl to be from "8:00 AM" to "5:00 PM",
      >incrementing every half hour.
      >>
      >I know I could type in the values manually but something tells me that I
      >could loop thru a time variable and add 1/2 hr, etc... then add it in a
      >loop. (More efficient.)
      >>
      >Any coders want to give this a shot? The less code the better. Thanks!
      >>
      >
      >

      Comment

      • Mark Rae [MVP]

        #4
        Re: Code to add times to dropdownlist

        "Cirene" <cirene@nowhere .comwrote in message
        news:ew0%23xWD7 IHA.1428@TK2MSF TNGP06.phx.gbl. ..
        Thanks. So that I do not have to do too much conversion I was wondering
        if there was a way to have a time variable of 8:00 AM and just add 30 min
        to it over and over again until you reach 5:30 PM. Hmmm...
        Absolutely!

        1) Declare the starting DateTime variable

        2) Set up a while (...) loop

        3) Use the .AddMinutes(30) variable to increment the DateTime variable

        4) Add a new ListItem each time through the loop.


        --
        Mark Rae
        ASP.NET MVP


        Comment

        • sloan

          #5
          Re: Code to add times to dropdownlist


          Right ohh.

          But if you knew that (OP), then just let 'er fly (code it up) !





          "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
          news:%23T1qndD7 IHA.1080@TK2MSF TNGP06.phx.gbl. ..
          "Cirene" <cirene@nowhere .comwrote in message
          news:ew0%23xWD7 IHA.1428@TK2MSF TNGP06.phx.gbl. ..
          >
          >Thanks. So that I do not have to do too much conversion I was wondering
          >if there was a way to have a time variable of 8:00 AM and just add 30 min
          >to it over and over again until you reach 5:30 PM. Hmmm...
          >
          Absolutely!
          >
          1) Declare the starting DateTime variable
          >
          2) Set up a while (...) loop
          >
          3) Use the .AddMinutes(30) variable to increment the DateTime variable
          >
          4) Add a new ListItem each time through the loop.
          >
          >
          --
          Mark Rae
          ASP.NET MVP
          http://www.markrae.net

          Comment

          • Cowboy \(Gregory A. Beamer\)

            #6
            Re: Code to add times to dropdownlist

            Here is a sample that does what you need, and is highly flexible.

            First the page itself (test page) - there are two drop downs here, as the
            code is set up to setup as many time dropdowns as you need.

            <%@ Page Language="C#" AutoEventWireup ="true" CodeFile="Defau lt.aspx.cs"
            Inherits="_Defa ult" %>

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

            <html xmlns="http://www.w3.org/1999/xhtml">
            <head runat="server">
            <title>Untitl ed Page</title>
            </head>
            <body>
            <form id="form1" runat="server">
            <div>
            <asp:DropDownLi st ID="DropDownLis t1" runat="server">
            </asp:DropDownLis t>
            <asp:DropDownLi st ID="DropDownLis t2" runat="server">
            </asp:DropDownLis t>
            </div>
            </form>
            </body>
            </html>

            Now the .cs file

            using System;
            using System.Web.UI.W ebControls;

            public partial class _Default : System.Web.UI.P age
            {
            protected void Page_Load(objec t sender, EventArgs e)
            {
            DropDownList ddl = this.DropDownLi st1;
            LoadTimeDropDow nList(ref ddl, 8, 17, 30);

            ddl = this.DropDownLi st2;
            LoadTimeDropDow nList(ref ddl, 6, 22, 60);
            }

            private void LoadTimeDropDow nList(ref DropDownList ddl, int startHour,
            int endHour, int incrementInMinu tes)
            {
            DateTime now = DateTime.Now;
            DateTime startTime = new DateTime(now.Ye ar, now.Month, now.Day,
            startHour, 0, 0);
            DateTime endTime = new DateTime(now.Ye ar, now.Month, now.Day,
            endHour, 0, 0);

            LoadTimeDropDow nList(ref ddl, startTime, endTime,
            incrementInMinu tes);
            }

            private void LoadTimeDropDow nList(ref DropDownList ddl, DateTime
            startTime, DateTime endTime, int incrementInMinu tes)
            {
            DateTime now = DateTime.Now;
            bool haveIndex = false;

            while (startTime <= endTime)
            {
            ddl.Items.Add(s tartTime.ToShor tTimeString());
            startTime = startTime.AddMi nutes(increment InMinutes);

            if ((startTime now) && (!haveIndex))
            {
            ddl.SelectedInd ex = ddl.Items.Count - 1;
            haveIndex = true;
            }
            }

            }

            }

            This is a down and dirty try at this, but you should see two different drop
            downs, with different parameters. You can encapsulate this in a user control
            and use it over and over again, by creating parameters for start hour, end
            hour and increment.

            --
            Gregory A. Beamer
            MVP, MCP: +I, SE, SD, DBA

            Subscribe to my blog


            or just read it:


            *************** *************** **************
            | Think outside the box! |
            *************** *************** **************
            "Cirene" <cirene@nowhere .comwrote in message
            news:e2z2QFD7IH A.3696@TK2MSFTN GP04.phx.gbl...
            >I have a dropdownlist. I want to fill it with times (using vb.net code).
            >
            I want the entries in the ddl to be from "8:00 AM" to "5:00 PM",
            incrementing every half hour.
            >
            I know I could type in the values manually but something tells me that I
            could loop thru a time variable and add 1/2 hr, etc... then add it in a
            loop. (More efficient.)
            >
            Any coders want to give this a shot? The less code the better. Thanks!
            >

            Comment

            • Cowboy \(Gregory A. Beamer\)

              #7
              Re: Code to add times to dropdownlist

              I already posted the complete code, but here is the portions with the loop:

              private void LoadTimeDropDow nList(ref DropDownList ddl, int startHour,
              int endHour, int incrementInMinu tes)
              {
              DateTime now = DateTime.Now;
              DateTime startTime = new DateTime(now.Ye ar, now.Month, now.Day,
              startHour, 0, 0);
              DateTime endTime = new DateTime(now.Ye ar, now.Month, now.Day,
              endHour, 0, 0);

              LoadTimeDropDow nList(ref ddl, startTime, endTime,
              incrementInMinu tes);
              }

              private void LoadTimeDropDow nList(ref DropDownList ddl, DateTime
              startTime, DateTime endTime, int incrementInMinu tes)
              {
              DateTime now = DateTime.Now;
              bool haveIndex = false;

              while (startTime <= endTime)
              {
              ddl.Items.Add(s tartTime.ToShor tTimeString());
              startTime = startTime.AddMi nutes(increment InMinutes);

              if ((startTime now) && (!haveIndex))
              {
              ddl.SelectedInd ex = ddl.Items.Count - 1;
              haveIndex = true;
              }
              }
              }


              The overload makes it so you can pass in the date time objects, or not,
              depending on your own personal ideas. The DropDownList is added by ref here
              so you can have many dropdowns with different time parameters.

              --
              Gregory A. Beamer
              MVP, MCP: +I, SE, SD, DBA

              Subscribe to my blog


              or just read it:


              *************** *************** **************
              | Think outside the box! |
              *************** *************** **************
              "Cirene" <cirene@nowhere .comwrote in message
              news:ew0%23xWD7 IHA.1428@TK2MSF TNGP06.phx.gbl. ..
              Thanks. So that I do not have to do too much conversion I was wondering
              if there was a way to have a time variable of 8:00 AM and just add 30 min
              to it over and over again until you reach 5:30 PM. Hmmm...
              >
              "sloan" <sloan@ipass.ne twrote in message
              news:%23ty78OD7 IHA.3480@TK2MSF TNGP03.phx.gbl. ..
              >>
              >>
              >myDDL.Items.Ad d(new ListItem ("Something1 ", "Something2 ") );
              >>
              >You can write a loop to go through the items.
              >>
              >for(int i = 8 ; i <= 17 ; i ++ )
              >{
              > for (int j = 0 ; j < 2 ; j ++ )
              >{
              >>
              > //now you have
              > //8 and 0 ( "8:00" )
              > //8 and 1 (" 8:30")
              > string hourAndMinute = Convert.ToStrin g(i);
              > if(j == 0)
              >{
              > hourAndMinute += ":00";
              >>
              >}
              >else
              >{
              > hourAndMinute +=":30";
              >}
              >>
              > myDDL.Items.Add (new ListItem ( hourAndMinute , hourAndMinute ) );
              >}
              >>
              >}
              >>
              >>
              >I'll leave it to you for the AM PM. And the convert (from) military time
              >hours.
              >>
              >But that should get you started.
              >>
              >>
              >"Cirene" <cirene@nowhere .comwrote in message
              >news:e2z2QFD7I HA.3696@TK2MSFT NGP04.phx.gbl.. .
              >>>I have a dropdownlist. I want to fill it with times (using vb.net code).
              >>>
              >>I want the entries in the ddl to be from "8:00 AM" to "5:00 PM",
              >>incrementin g every half hour.
              >>>
              >>I know I could type in the values manually but something tells me that I
              >>could loop thru a time variable and add 1/2 hr, etc... then add it in a
              >>loop. (More efficient.)
              >>>
              >>Any coders want to give this a shot? The less code the better. Thanks!
              >>>
              >>
              >>
              >
              >

              Comment

              • Cirene

                #8
                Re: Code to add times to dropdownlist

                Wow - u guys are awesome - thanks!

                "Cowboy (Gregory A. Beamer)" <NoSpamMgbworld @comcast.netNoS pamMwrote in
                message news:%23CbBXjD7 IHA.4204@TK2MSF TNGP03.phx.gbl. ..
                Here is a sample that does what you need, and is highly flexible.
                >
                First the page itself (test page) - there are two drop downs here, as the
                code is set up to setup as many time dropdowns as you need.
                >
                <%@ Page Language="C#" AutoEventWireup ="true" CodeFile="Defau lt.aspx.cs"
                Inherits="_Defa ult" %>
                >
                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
                >
                <html xmlns="http://www.w3.org/1999/xhtml">
                <head runat="server">
                <title>Untitl ed Page</title>
                </head>
                <body>
                <form id="form1" runat="server">
                <div>
                <asp:DropDownLi st ID="DropDownLis t1" runat="server">
                </asp:DropDownLis t>
                <asp:DropDownLi st ID="DropDownLis t2" runat="server">
                </asp:DropDownLis t>
                </div>
                </form>
                </body>
                </html>
                >
                Now the .cs file
                >
                using System;
                using System.Web.UI.W ebControls;
                >
                public partial class _Default : System.Web.UI.P age
                {
                protected void Page_Load(objec t sender, EventArgs e)
                {
                DropDownList ddl = this.DropDownLi st1;
                LoadTimeDropDow nList(ref ddl, 8, 17, 30);
                >
                ddl = this.DropDownLi st2;
                LoadTimeDropDow nList(ref ddl, 6, 22, 60);
                }
                >
                private void LoadTimeDropDow nList(ref DropDownList ddl, int startHour,
                int endHour, int incrementInMinu tes)
                {
                DateTime now = DateTime.Now;
                DateTime startTime = new DateTime(now.Ye ar, now.Month, now.Day,
                startHour, 0, 0);
                DateTime endTime = new DateTime(now.Ye ar, now.Month, now.Day,
                endHour, 0, 0);
                >
                LoadTimeDropDow nList(ref ddl, startTime, endTime,
                incrementInMinu tes);
                }
                >
                private void LoadTimeDropDow nList(ref DropDownList ddl, DateTime
                startTime, DateTime endTime, int incrementInMinu tes)
                {
                DateTime now = DateTime.Now;
                bool haveIndex = false;
                >
                while (startTime <= endTime)
                {
                ddl.Items.Add(s tartTime.ToShor tTimeString());
                startTime = startTime.AddMi nutes(increment InMinutes);
                >
                if ((startTime now) && (!haveIndex))
                {
                ddl.SelectedInd ex = ddl.Items.Count - 1;
                haveIndex = true;
                }
                }
                >
                }
                >
                }
                >
                This is a down and dirty try at this, but you should see two different
                drop downs, with different parameters. You can encapsulate this in a user
                control and use it over and over again, by creating parameters for start
                hour, end hour and increment.
                >
                --
                Gregory A. Beamer
                MVP, MCP: +I, SE, SD, DBA
                >
                Subscribe to my blog

                >
                or just read it:

                >
                *************** *************** **************
                | Think outside the box! |
                *************** *************** **************
                "Cirene" <cirene@nowhere .comwrote in message
                news:e2z2QFD7IH A.3696@TK2MSFTN GP04.phx.gbl...
                >>I have a dropdownlist. I want to fill it with times (using vb.net code).
                >>
                >I want the entries in the ddl to be from "8:00 AM" to "5:00 PM",
                >incrementing every half hour.
                >>
                >I know I could type in the values manually but something tells me that I
                >could loop thru a time variable and add 1/2 hr, etc... then add it in a
                >loop. (More efficient.)
                >>
                >Any coders want to give this a shot? The less code the better. Thanks!
                >>
                >

                Comment

                Working...