2011-02-01 67 views
1

我想讓VB.net及其日曆更舒適一些。我已將代碼編碼到有幾個事件顯示的日曆,如果您選擇一天或更改月份,則日曆上方的標籤會反映這些更改。我想要做的是允許用戶添加新的事件到計劃中。目前我不知道如何讓新的事件顯示在日曆上。如何獲取日曆上顯示的新事件

代碼:

Public Class Calendar 
    Inherits System.Web.UI.Page 

    Dim schedule As New Hashtable 
    Dim _scheduleData As Hashtable 


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     _scheduleData = GetSchedule() 

     Calendar1.Caption = "Personal Schedule" 
     Calendar1.FirstDayOfWeek = WebControls.FirstDayOfWeek.Sunday 
     Calendar1.NextPrevFormat = NextPrevFormat.ShortMonth 
     Calendar1.TitleFormat = TitleFormat.MonthYear 
     Calendar1.ShowGridLines = True 
     Calendar1.DayStyle.HorizontalAlign = HorizontalAlign.Left 
     Calendar1.DayStyle.VerticalAlign = VerticalAlign.Top 
     Calendar1.DayStyle.Height = New Unit(75) 
     Calendar1.DayStyle.Width = New Unit(100) 
     Calendar1.OtherMonthDayStyle.BackColor = Drawing.Color.DarkSlateBlue 
     Calendar1.TodaysDate = New DateTime(2011, 2, 1) 
     Calendar1.VisibleDate = Calendar1.TodaysDate 

    End Sub 

    Private Function GetSchedule() As Hashtable 

     schedule("2/14/2011") = "Valentines Day" 
     schedule("2/1/2011") = "Presentation" 
     schedule("5/16/2011") = "Birthday" 

     Return schedule 

    End Function 

    Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender 
     If Not _scheduleData(e.Day.Date.ToShortDateString()) Is Nothing Then 

      Dim lit = New Literal() 
      lit.Text = "<br />" 
      e.Cell.Controls.Add(lit) 

      Dim lbl = New Label() 
      lbl.Text = _scheduleData(e.Day.Date.ToShortDateString()) 
      lbl.Font.Size = New FontUnit(FontSize.Small) 
      e.Cell.Controls.Add(lbl) 

     End If 

    End Sub 


    Private Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged 
     LabelAction.Text = "Selection changed to: " + Calendar1.SelectedDate.ToShortDateString 

    End Sub 


    Private Sub Calendar1_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles Calendar1.VisibleMonthChanged 
     LabelAction.Text = "Month changed to: " + e.NewDate.ToShortDateString() 

    End Sub 

    Private Sub addButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles addButton.Click 
     Dim day As Date 
     Dim name As String 

     day = dayText.Text 
     name = nameText.Text 

     schedule(day) = name 

    End Sub 



End Class 

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Calendar.aspx.vb" Inherits="WebApplication2.Calendar" %> 

<!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></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

     <asp:Label ID="LabelAction" runat="server"></asp:Label> 
     <br /> 
     <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar> 
     <asp:Label ID="Label1" runat="server" Text="Date"></asp:Label> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:Label ID="Label2" runat="server" Text="Name of Event"></asp:Label> 
     <br /> 
     <asp:TextBox ID="dayText" runat="server" Width="66px"></asp:TextBox> 
&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:TextBox ID="nameText" runat="server"></asp:TextBox> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:Button ID="addButton" runat="server" Text="Add Event" /> 

    </div> 
    </form> 
</body> 
</html> 
+0

您是否將時間表保存在數據庫或xml文件中 – Kronass 2011-02-01 05:09:53

回答

0

我解決了它,但使用的字典

定義調度爲以下

Dim schedule As New Dictionary(Of Date, String) 

對於GetSchedule我改成了這個

Private Sub GetSchedule() 
    schedule(New Date(2011, 2, 14)) = "Valentines Day" 
    schedule(New Date(2011, 1, 2)) = "Presentation" 
    schedule(New Date(2011, 5, 16)) = "Birthday" 
End Function 

在一個月活動添加到檢查所有的事件,如果他們是可見的一個月內

Private Sub Calendar1_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles Calendar1.VisibleMonthChanged 
     GetSchedule() 
     Dim StartDate As Date = Calendar1.VisibleDate 
     Dim EndDate As New Date(Calendar1.VisibleDate.Year, Calendar1.VisibleDate.Month, Date.DaysInMonth(Calendar1.VisibleDate.Year, Calendar1.VisibleDate.Month)) 
     For Each Item As KeyValuePair(Of Date, String) In schedule 
      If Item.Key >= StartDate And Item.Key <= EndDate Then 

       MsgBox(Item.Value) 
       'Add view actions of this month 
      End If 
     Next 
    End Sub 

請測試一下,看看它是如何工作與你

注:它將爲可見的一個月工作,它不會顯示其他月份日期