selecting date in a listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • juster21
    New Member
    • Feb 2008
    • 24

    selecting date in a listbox

    I have a listbox populated with all the week ending dates of 2008 where Sunday is the last day of the week (1/6/08, 1/13/08, etc.)

    I need to select the most current week ending date upon the form load. So, since today is 2/5/08, the date selected should be 2/3/08. I have struggled with this one for a while so any and all help is appreciated. Thanks!!
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well, if the week "ends" on a sunday, then that means from the current day, you want to go backwards looking for a sunday.

    Code:
    DateTime dt = DateTime.Now;
    while (dt.DayOfWeek != DayOfWeek.Sunday)
    {//when this loop exits you will have the date of the sunday you want
       dt = dt.AddDays(-1);
    }
    //use dt to build the string to look for in your listbox

    Comment

    • juster21
      New Member
      • Feb 2008
      • 24

      #3
      Originally posted by Plater
      Well, if the week "ends" on a sunday, then that means from the current day, you want to go backwards looking for a sunday.

      Code:
      DateTime dt = DateTime.Now;
      while (dt.DayOfWeek != DayOfWeek.Sunday)
      {//when this loop exits you will have the date of the sunday you want
         dt = dt.AddDays(-1);
      }
      //use dt to build the string to look for in your listbox

      I was hoping to have this in vb.net....can you assist?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well ok, it's really almost identical code, you shoulda been able to gleen it from what I posted, considering I did like all the work on that part for you :-P
        [code=vbnet]
        Dim dt As DateTime = DateTime.Now
        While dt.DayOfWeek <> DayOfWeek.Sunda y
        'when this loop exits you will have the date of the sunday you want
        dt = dt.AddDays(-1)
        End While
        'use dt to build the string to look for in your listbox
        [/code]

        Comment

        • juster21
          New Member
          • Feb 2008
          • 24

          #5
          Originally posted by Plater
          Well ok, it's really almost identical code, you shoulda been able to gleen it from what I posted, considering I did like all the work on that part for you :-P
          [code=vbnet]
          Dim dt As DateTime = DateTime.Now
          While dt.DayOfWeek <> DayOfWeek.Sunda y
          'when this loop exits you will have the date of the sunday you want
          dt = dt.AddDays(-1)
          End While
          'use dt to build the string to look for in your listbox
          [/code]
          Thanks Plater!! I'll try to be more ambitious next time!! Thanks again!!

          Comment

          • juster21
            New Member
            • Feb 2008
            • 24

            #6
            loading listbox with value

            Ok....I need to know how to select a date in a listbox based on a returned value of the week ending date.

            I will want this to occur in the load event of the form.
            Code:
              Public Sub frmDataEntry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                    gMDB = New OleDb.OleDbConnection
                    gMDB.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\Cred\Production\Production.mdb"
                    gMDB.Open()
                    lblID.Text = System.Environment.UserName.Trim 
                    GetTeam() 'Mod 1/31/08,loads teams to lstTeam.
                '--Need code to select current week ending date. something like...???
                    lstWeekEndingDate.SelectedItem.Equals(GetDate)
                End Sub
            Last edited by juster21; Feb 5 '08, 08:04 PM. Reason: adding code sample

            Comment

            Working...