Click here to Skip to main content
15,867,835 members
Articles / Programming Languages / Visual Basic
Article

Calendar Function

Rate me:
Please Sign up or sign in to vote.
1.00/5 (10 votes)
7 Apr 2004CPOL 67.4K   1.2K   15   3
Function that returns a calendar in code-behind at run-time not a web-control but similar

Sample Image - screenshot.gif

Introduction

This is not a web-Control is only a function that returns a Calendar at Run-Time in Code-Behind you can put it any were in this example it apears inside a datagrid:

The function on it self is very easy to understand it gos as falow:

VB.NET
Function cria_calendario_nesta_celula2(Celula _
   as System.Web.UI.WebControls.TableCell,_
   nomes_dos_campos as string, label_nome as string)
      Dim Tabela as New System.Web.UI.WebControls.Table
      Tabela.id = "Tabela" & nomes_dos_campos
      Dim Linha as New System.Web.UI.WebControls.TableRow
      Linha.id = "Linha" & nomes_dos_campos
      Dim Label as New System.Web.UI.WebControls.Label
      Label.id = "Label" & nomes_dos_campos
      Dim _cellx as New System.Web.UI.WebControls.TableCell
      if label_nome = "S" ' Cria uma label com o nome do campo
VB.NET
_cellx = new TableCell()
 _cellx.Controls.Add(new LiteralControl(nomes_dos_campos.ToString))
 Celula.Controls.Add(_cellx)
End if

'Para o Calendario da Data
VB.NET
_cellx = new TableCell()
Dim Calendar_tmp as New System.Web.UI.WebControls.Calendar
Calendar_tmp.CssClass="Under"
Calendar_tmp.id = "Calendar_" & nomes_dos_campos

Calendar_tmp.EnableViewState = true
Calendar_tmp.TodaysDate = System.Date.Today
Calendar_tmp.VisibleDate = System.Date.Today

Calendar_tmp.TitleFormat = TitleFormat.MonthYear
Calendar_tmp.SelectionMode = CalendarSelectionMode.Day
Calendar_tmp.TodaysDate = System.Date.Today
Calendar_tmp.NextPrevFormat = NextPrevFormat.CustomText
VB.NET
'Estas duas propriedades fazem um encoder
'em html dai que admitem qualquer tag ....
VB.NET
Calendar_tmp.NextMonthText = "<img border='0'" & _
    " src='../img/calendar_seta_v.gif' WIDTH='20' HEIGHT='10'>"
Calendar_tmp.PrevMonthText = "<img border='0'" & _
    " src='../img/calendar_seta_e.gif' WIDTH='20' HEIGHT='10'>"
'Estas duas propriedades fazem um encoder em html
'dai que admitem qualquer tag ....

Calendar_tmp.CellPadding  = 0
Calendar_tmp.CellSpacing  = 0
Calendar_tmp.Height = Unit.Pixel(100)
Calendar_tmp.Width = Unit.Pixel(150)
Calendar_tmp.ShowGridlines = True

Calendar_tmp.ApplyStyle(S_Calendar_01) '1
Calendar_tmp.TitleStyle.MergeWith(S_Calendar_08) '8
Calendar_tmp.NextPrevStyle.MergeWith(S_Calendar_09) '9
Calendar_tmp.DayHeaderStyle.MergeWith(S_Calendar_02) '2
Calendar_tmp.DayStyle.MergeWith(S_Calendar_03) '3
Calendar_tmp.WeekendDayStyle.MergeWith(S_Calendar_04) '4
Calendar_tmp.SelectedDayStyle.MergeWith(S_Calendar_05) '5
Calendar_tmp.TodayDayStyle.MergeWith(S_Calendar_06) '6
Calendar_tmp.OtherMonthDayStyle.MergeWith(S_Calendar_07) '7



AddHandler Calendar_tmp.SelectionChanged, _
            AddressOf CAL_Selecionou_uma_data
AddHandler Calendar_tmp.DayRender,_
            AddressOf Tarefas_Calendar_OnDayRender



'Para as DropDownList's da Data e Ano
VB.NET
Dim DDL_tmp_Ano as New System.Web.UI.WebControls.DropDownList
DDL_tmp_Ano.ApplyStyle(S_DropDownList_03)
Dim DDL_tmp_Mes as New System.Web.UI.WebControls.DropDownList
DDL_tmp_Mes.ApplyStyle(S_DropDownList_02)
Dim ii_ddl, ii_ddl2, jj_ddl as integer
ii_ddl2 = 0
jj_ddl = 2020
For ii_ddl = 1900 to jj_ddl
 DDL_tmp_Ano.Items.Insert(ii_ddl2, new ListItem(ii_ddl))
 ii_ddl2 = ii_ddl2 +1
next
ii_ddl = 1
jj_ddl = 12
For ii_ddl = 0 To jj_ddl
 DDL_tmp_Mes.Items.Insert(ii_ddl, new ListItem(ii_ddl))
Next

DDL_tmp_Mes.Items.remove(0)
DDL_tmp_Ano.Items.remove(0)
DDL_tmp_Ano.Items.remove(1)
DDL_tmp_Ano.SelectedIndex = Calendar_tmp.TodaysDate.Year - 1900
DDL_tmp_Mes.SelectedIndex = Calendar_tmp.TodaysDate.Month - 1
AddHandler DDL_tmp_Mes.SelectedIndexChanged,_
             AddressOf DDL_Selecionou_uma_data
AddHandler DDL_tmp_Ano.SelectedIndexChanged,_
             AddressOf DDL_Selecionou_uma_data
DDL_tmp_Mes.id = "Calendar_Mes_" & nomes_dos_campos
DDL_tmp_Ano.id = "Calendar_Ano_" & nomes_dos_campos

'Para a TextBox da Data
VB.NET
Dim TB as New System.Web.UI.WebControls.TextBox
TB.ApplyStyle(S_TextBox_03)
TB.text = Calendar_tmp.TodaysDate.ToShortDateString()
TB.id = nomes_dos_campos
TB.Columns= 10
TB.rows= 10
TB.MaxLength= 10
TB.Enabled="False"


'Para o Butao das DDL e Calendario
VB.NET
Dim Butt_tmp as New System.Web.UI.WebControls.imageButton
Butt_tmp.ImageAlign = ImageAlign.AbsBottom
Butt_tmp.BorderWidth = Unit.Pixel(1)
Butt_tmp.BorderStyle = BorderStyle.Ridge
Butt_tmp.ImageUrl="calendar_icon.gif"
Butt_tmp.ToolTip="Abre / Fecha Calendário"
Butt_tmp.Width = Unit.Pixel(22) 'Unit.Point(10)
Butt_tmp.Height = Unit.Pixel(21) 'Unit.Point(10)


Dim Butt_load as New System.Web.UI.WebControls.Button
Butt_load.id = "Butt_load" & nomes_dos_campos
Butt_load.CssClass="Button_Load"
Butt_load.CausesValidation="false"
Butt_load.visible="false"
Butt_load.Font.name = "Verdana"
Butt_load.BackColor = System.Drawing.Color.Green
Butt_load.ForeColor = System.Drawing.Color.White
Butt_load.Font.Bold = "true"
Butt_load.text = "OK"
AddHandler Butt_load.click, AddressOf Butt_load_Selecionou_uma_data


Butt_tmp.CssClass="Button"
Butt_tmp.CausesValidation="false"
Butt_tmp.id = "Butt_" & nomes_dos_campos
Butt_tmp.Font.name = "webdings"
'Para o botao de aparece/desaparece calendario
VB.NET
AddHandler Butt_tmp.click, AddressOf Butt_click
'Adiciona os controls da Data - TextBox - DropDownList - Calendario
VB.NET
 DDL_tmp_Mes.visible="false"
 DDL_tmp_Ano.visible="false"
 Calendar_tmp.visible="false"

 _cellx.id = "CelulaCalendario" & nomes_dos_campos
 _cellx.Controls.Add(TB)
 _cellx.Controls.Add(Butt_tmp)
 _cellx.Controls.Add(Calendar_tmp)
 _cellx.Controls.Add(DDL_tmp_Mes)
 _cellx.Controls.Add(DDL_tmp_Ano)
 _cellx.Controls.Add(Butt_load)
 _cellx.VerticalAlign = VerticalAlign.Middle
 _cellx.HorizontalAlign = HorizontalAlign.Left
 Linha.Cells.Add(_cellx)
 Tabela.Rows.Add(Linha)


 if label_nome = "Place" then

 Celula.Controls.Add(TB)
 Celula.Controls.Add(Butt_tmp)
 Celula.Controls.Add(Calendar_tmp)
 Celula.Controls.Add(DDL_tmp_Mes)
 Celula.Controls.Add(DDL_tmp_Ano)
 Celula.Controls.Add(Butt_load)
 Celula.VerticalAlign = VerticalAlign.Middle
 Else
 if Label_nome = "N"
 Celula.Controls.Add(_cellx)
 End if
 End if
End Function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Portugal Portugal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralEnglish please! Pin
Björn T.J.M. Spruit18-Feb-07 23:32
Björn T.J.M. Spruit18-Feb-07 23:32 
Could you please translate your text to English so it is readable to everyone ?
I actually need this code atm...
The problem I'm trying to solve is that I want to add a calendar to a webpage in runtime if my vb.net code behind detects it's required.
Lets say my code checks if a date value is required.
If so, show a calendar that will post back the date selection.
QuestionEnglish? Pin
White X Dragon9-Jul-05 16:53
White X Dragon9-Jul-05 16:53 
GeneralQuick suggestions: Pin
Shog98-Apr-04 15:25
sitebuilderShog98-Apr-04 15:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.