Code to add times to dropdownlist

C

Cirene

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!
 
S

sloan

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.ToString(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.
 
C

Cirene

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...
 
C

Cowboy \(Gregory A. Beamer\)

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="Default.aspx.cs"
Inherits="_Default" %>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>

Now the .cs file

using System;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownList ddl = this.DropDownList1;
LoadTimeDropDownList(ref ddl, 8, 17, 30);

ddl = this.DropDownList2;
LoadTimeDropDownList(ref ddl, 6, 22, 60);
}

private void LoadTimeDropDownList(ref DropDownList ddl, int startHour,
int endHour, int incrementInMinutes)
{
DateTime now = DateTime.Now;
DateTime startTime = new DateTime(now.Year, now.Month, now.Day,
startHour, 0, 0);
DateTime endTime = new DateTime(now.Year, now.Month, now.Day,
endHour, 0, 0);

LoadTimeDropDownList(ref ddl, startTime, endTime,
incrementInMinutes);
}

private void LoadTimeDropDownList(ref DropDownList ddl, DateTime
startTime, DateTime endTime, int incrementInMinutes)
{
DateTime now = DateTime.Now;
bool haveIndex = false;

while (startTime <= endTime)
{
ddl.Items.Add(startTime.ToShortTimeString());
startTime = startTime.AddMinutes(incrementInMinutes);

if ((startTime > now) && (!haveIndex))
{
ddl.SelectedIndex = 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.
 
C

Cowboy \(Gregory A. Beamer\)

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

private void LoadTimeDropDownList(ref DropDownList ddl, int startHour,
int endHour, int incrementInMinutes)
{
DateTime now = DateTime.Now;
DateTime startTime = new DateTime(now.Year, now.Month, now.Day,
startHour, 0, 0);
DateTime endTime = new DateTime(now.Year, now.Month, now.Day,
endHour, 0, 0);

LoadTimeDropDownList(ref ddl, startTime, endTime,
incrementInMinutes);
}

private void LoadTimeDropDownList(ref DropDownList ddl, DateTime
startTime, DateTime endTime, int incrementInMinutes)
{
DateTime now = DateTime.Now;
bool haveIndex = false;

while (startTime <= endTime)
{
ddl.Items.Add(startTime.ToShortTimeString());
startTime = startTime.AddMinutes(incrementInMinutes);

if ((startTime > now) && (!haveIndex))
{
ddl.SelectedIndex = 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.
 
C

Cirene

Wow - u guys are awesome - thanks!

Cowboy (Gregory A. Beamer) said:
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="Default.aspx.cs"
Inherits="_Default" %>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>

Now the .cs file

using System;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownList ddl = this.DropDownList1;
LoadTimeDropDownList(ref ddl, 8, 17, 30);

ddl = this.DropDownList2;
LoadTimeDropDownList(ref ddl, 6, 22, 60);
}

private void LoadTimeDropDownList(ref DropDownList ddl, int startHour,
int endHour, int incrementInMinutes)
{
DateTime now = DateTime.Now;
DateTime startTime = new DateTime(now.Year, now.Month, now.Day,
startHour, 0, 0);
DateTime endTime = new DateTime(now.Year, now.Month, now.Day,
endHour, 0, 0);

LoadTimeDropDownList(ref ddl, startTime, endTime,
incrementInMinutes);
}

private void LoadTimeDropDownList(ref DropDownList ddl, DateTime
startTime, DateTime endTime, int incrementInMinutes)
{
DateTime now = DateTime.Now;
bool haveIndex = false;

while (startTime <= endTime)
{
ddl.Items.Add(startTime.ToShortTimeString());
startTime = startTime.AddMinutes(incrementInMinutes);

if ((startTime > now) && (!haveIndex))
{
ddl.SelectedIndex = 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,915
Members
47,456
Latest member
JavierWalp

Latest Threads

Top