Click here to Skip to main content
15,867,330 members
Articles / Desktop Programming / WPF

Persian Diary in WPF

Rate me:
Please Sign up or sign in to vote.
4.81/5 (50 votes)
6 Jan 2010CPOL2 min read 136.5K   5.7K   61   37
With Persian Diary, you can navigate in Persian Calendar and store your memorandums
Persian Calendar in WPF

Introduction

With this software, you can navigate the Persian Calendar and you can add your memorandum for each day.

Using the Code

I've created this project with .NET Framework 3.5 SP1, so you need to install it to use this project.
I've also used a SQL database(mdf) to store the data, so at least you have to install SQL Express 2008.

Creating the UI

The best way for creating a software UI like this is to use a Stack.

Image 2

Then we should use UniformGrids and Grids in the Stack to create the UI.
For instance, the XAML code for 1 in the above figure is:

XML
<StackPanel x:Name="stackPanel">
    <StackPanel.BitmapEffect>
        <BlurBitmapEffect Radius="0"/>
    </StackPanel.BitmapEffect>
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Center" 
        x:Name="header" MinHeight="70">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.135*"/>
            <ColumnDefinition Width="0.102*"/>
            <ColumnDefinition Width="0.494*"/>
            <ColumnDefinition Width="0.14*"/>
            <ColumnDefinition Width="0.129*"/>
        </Grid.ColumnDefinitions>
        <TextBlock FlowDirection="RightToLeft" Language="fa-ir" 
            FontSize="36" x:Name="TextBlockThisMonth" HorizontalAlignment="Center" 
            Grid.Column="2" VerticalAlignment="Center" Margin="0,0,0,0" 
            FontFamily="Arial">تاریخ</TextBlock>

        <Button x:Name="previousMonth" Content=" ماه قبل &gt; " 
            Grid.Column="3" VerticalAlignment="Center" Click="previousMonth_Click" 
            HorizontalAlignment="Center" />
        <Button x:Name="nextMonth" Content=" &lt; ماه بعد " Grid.Column="1" 
            HorizontalAlignment="Center" VerticalAlignment="Center" 
            Click="nextMonth_Click" />

        <Button x:Name="previousYear" Content=" سال قبل &gt;&gt; " 
            Grid.Column="4" HorizontalAlignment="Center" VerticalAlignment="Center" 
            Click="previousYear_Click" />
        <Button x:Name="nextYear" Content=" &lt;&lt; سال بعد " 
            HorizontalAlignment="Center"  Click="nextYear_Click" 
            VerticalAlignment="Center"  />

    </Grid>
    <UniformGrid Columns="7" Rows="1" x:Name="dayNames" MinHeight="30">
        <Grid>
            <Rectangle Style="{DynamicResource RectangleStyleForHolydays}"/>
            <TextBlock HorizontalAlignment="Center" 
            VerticalAlignment="Center"><Run FlowDirection="RightToLeft" 
            Language="fa-ir" Text="جمعه"/></TextBlock>
        </Grid>
        <Grid>
            <Rectangle Style="{DynamicResource RectangleStyle1}"/>
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" 
            FontSize="14"><Run FlowDirection="RightToLeft" Language="fa-ir" 
            Text="پنج شنبه"/></TextBlock>
        </Grid>
        <Grid>
            <Rectangle Style="{DynamicResource RectangleStyle1}"/>
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" 
            FontSize="14"><Run FlowDirection="RightToLeft" Language="fa-ir" 
            Text="چهار شنبه"/></TextBlock>
        </Grid>
        <Grid>
            <Rectangle Style="{DynamicResource RectangleStyle1}"/>
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" 
            FontSize="14"><Run FlowDirection="RightToLeft" Language="fa-ir" 
            Text="سه شنبه"/></TextBlock>
        </Grid>
        <Grid>
            <Rectangle Style="{DynamicResource RectangleStyle1}"/>
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" 
            FontSize="14"><Run FlowDirection="RightToLeft" Language="fa-ir" 
            Text="دو شنبه"/></TextBlock>
        </Grid>
        <Grid>
            <Rectangle Style="{DynamicResource RectangleStyle1}"/>
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" 
            FontSize="14"><Run FlowDirection="RightToLeft" Language="fa-ir" 
            Text="یک شنبه"/></TextBlock>
        </Grid>
        <Grid>
            <Rectangle Style="{DynamicResource RectangleStyle1}" />
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" 
            FontSize="14"><Run FlowDirection="RightToLeft" Language="fa-ir" 
            Text="شنبه"/></TextBlock>
        </Grid>
    </UniformGrid>
    <!-- *********************************************************** -->
    (The XAML of 2)
     .
    .
    .
</StackPanel>

The XAML code for 2 in the above figure is:

XML
<UniformGrid Columns="7" Rows="1" Margin="0,10,0,0" 
	x:Name="calendarRow01" MinHeight="80">
    <Grid x:Name="GridJome0" Style="{DynamicResource GridStyle}">
        <Rectangle x:Name="RectangleJome0" 
			Style="{DynamicResource RectangleStyleForHolydays}" />
        <TextBlock x:Name="TextBlockJome0Miladi" 
        	Style="{DynamicResource TextBlockStyle6}">08 Dec</TextBlock>
        <TextBlock x:Name="TextBlockJome0Hijri" 
        	Style="{DynamicResource TextBlockStyle10}">۰۱ محرم</TextBlock>
        <TextBlock x:Name="TextBlockJome0" 
        	Style="{DynamicResource TextBlockStyle1}">جمعه</TextBlock>
    </Grid>
    <Grid x:Name="Grid5Shanbe0" Style="{DynamicResource GridStyle}">
        <Rectangle x:Name="Rectangle5Shanbe0" 
        	Style="{DynamicResource RectangleStyle2}" />
        <TextBlock x:Name="TextBlock5Shanbe0Miladi" Text="08 Dec" 
        	Style="{DynamicResource TextBlockStyle6}"/>
        <TextBlock x:Name="TextBlock5Shanbe0Hijri" Text="۰۱ محرم" 
        	Style="{DynamicResource TextBlockStyle10}"/>
        <TextBlock x:Name="TextBlock5Shanbe0" 
        	Style="{DynamicResource TextBlockStyle1}">پنج شنبه</TextBlock>
    </Grid>
    <Grid x:Name="Grid4Shanbe0" Style="{DynamicResource GridStyle}">
        <Rectangle x:Name="Rectangle4Shanbe0" 
        	Style="{DynamicResource RectangleStyle2}" />
        <TextBlock x:Name="TextBlock4Shanbe0Miladi" Text="08 Dec" 
        	Style="{DynamicResource TextBlockStyle6}"/>
        <TextBlock x:Name="TextBlock4Shanbe0Hijri" Text="۰۱ محرم" 
        	Style="{DynamicResource TextBlockStyle10}"/>
        <TextBlock x:Name="TextBlock4Shanbe0" 
        	Style="{DynamicResource TextBlockStyle1}">چهار شنبه</TextBlock>
    </Grid>
    <Grid x:Name="Grid3Shanbe0" Style="{DynamicResource GridStyle}">
        <Rectangle x:Name="Rectangle3Shanbe0" 
        	Style="{DynamicResource RectangleStyle2}" />
        <TextBlock x:Name="TextBlock3Shanbe0Miladi" Text="08 Dec" 
        	Style="{DynamicResource TextBlockStyle6}"/>
        <TextBlock x:Name="TextBlock3Shanbe0Hijri" Text="۰۱ محرم" 
        	Style="{DynamicResource TextBlockStyle10}"/>
        <TextBlock x:Name="TextBlock3Shanbe0" 
        	Style="{DynamicResource TextBlockStyle1}">سه شنبه</TextBlock>
    </Grid>
    <Grid x:Name="Grid2Shanbe0" Style="{DynamicResource GridStyle}">
        <Rectangle x:Name="Rectangle2Shanbe0" 
        	Style="{DynamicResource RectangleStyle2}" />
        <TextBlock x:Name="TextBlock2Shanbe0Miladi" Text="08 Dec" 
        	Style="{DynamicResource TextBlockStyle6}"/>
        <TextBlock x:Name="TextBlock2Shanbe0Hijri" Text="۰۱ محرم" 
        	Style="{DynamicResource TextBlockStyle10}"/>
        <TextBlock x:Name="TextBlock2Shanbe0" 
        	Style="{DynamicResource TextBlockStyle1}">دو شنبه</TextBlock>
    </Grid>
    <Grid x:Name="Grid1Shanbe0" Style="{DynamicResource GridStyle}">
        <Rectangle x:Name="Rectangle1Shanbe0" 
        	Style="{DynamicResource RectangleStyle2}" />
        <TextBlock x:Name="TextBlock1Shanbe0Miladi" 
        	Style="{DynamicResource TextBlockStyle6}" Text="08 Dec" Margin="0,0,10,5"/>
        <TextBlock x:Name="TextBlock1Shanbe0Hijri" 
        	Style="{DynamicResource TextBlockStyle10}" Text="۰۱ محرم" />
        <TextBlock x:Name="TextBlock1Shanbe0" 
        	Style="{DynamicResource TextBlockStyle1}">یک شنبه</TextBlock>
    </Grid>
    <Grid x:Name="GridShanbe0"  Style="{DynamicResource GridStyle}">
        <Rectangle x:Name="RectangleShanbe0" 
        	Style="{DynamicResource RectangleStyle2}" />
        <TextBlock x:Name="TextBlockShanbe0" 
        	Style="{DynamicResource TextBlockStyle1}" >شنبه</TextBlock>
        <TextBlock x:Name="TextBlockShanbe0Miladi" Text="08 Dec"  
        	Style="{DynamicResource TextBlockStyle6}"/>
        <TextBlock x:Name="TextBlockShanbe0Hijri" Text="۰۱ محرم" 
        	Style="{DynamicResource TextBlockStyle10}"/>
    </Grid>
</UniformGrid>

The XAML code for 3 in the above figure is:
XML
<Grid x:Name="Grid5Shanbe0" Style="{DynamicResource GridStyle}">
    <Rectangle x:Name="Rectangle5Shanbe0" 
    	Style="{DynamicResource RectangleStyle2}" />
    <TextBlock x:Name="TextBlock5Shanbe0Miladi" Text="08 Dec" 
    	Style="{DynamicResource TextBlockStyle6}"/>
    <TextBlock x:Name="TextBlock5Shanbe0Hijri" Text="۰۱ محرم" 
    	Style="{DynamicResource TextBlockStyle10}"/>
    <TextBlock x:Name="TextBlock5Shanbe0" 
    	Style="{DynamicResource TextBlockStyle1}">پنج شنبه</TextBlock>
</Grid>

The XAML code for 4 in the above figure is:

XML
<Grid x:Name="footer" MinHeight="70">

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0.121*"/>
        <ColumnDefinition Width="0.132*"/>
        <ColumnDefinition Width="0.07*"/>
        <ColumnDefinition Width="0.119*"/>
        <ColumnDefinition Width="0.3*"/>
        <ColumnDefinition Width="0.084*"/>
        <ColumnDefinition Width="0.064*"/>
        <ColumnDefinition Width="0.11*"/>
    </Grid.ColumnDefinitions>

    <Button x:Name="goToToday" Content="  پرش به امروز  " 
    	Grid.Column="3" HorizontalAlignment="Center" VerticalAlignment="Center" 
        Click="goToToday_Click"  />

    <WindowsFormsHost Grid.Column="0" FontSize="13" VerticalAlignment="Center" 
    	HorizontalAlignment="Center" Width="70" Height = "25">
        <wf:MaskedTextBox Text="سال"  x:Name="textBoxYear" Mask="0000"/>
    </WindowsFormsHost>

    <ComboBox Grid.Column="1" FontSize="13" HorizontalAlignment="Center" 
    	VerticalAlignment="Center" IsSynchronizedWithCurrentItem="True" 
        	Width="85" x:Name="comboBoxMonths">
        <ComboBoxItem Content="فروردین" IsSelected="True"/>
        <ComboBoxItem Content="اردیبهشت"/>
        <ComboBoxItem Content="خرداد"/>

        <ComboBoxItem Content="تیر"/>
        <ComboBoxItem Content="مرداد"/>
        <ComboBoxItem Content="شهریور"/>

        <ComboBoxItem Content="مهر"/>
        <ComboBoxItem Content="آبان"/>
        <ComboBoxItem Content="آذر"/>

        <ComboBoxItem Content="دی"/>
        <ComboBoxItem Content="بهمن"/>
        <ComboBoxItem Content="اسفند"/>
    </ComboBox>

    <Button x:Name="goToDate" Content="  برو  " Grid.Column="2" 
    	VerticalAlignment="Center" HorizontalAlignment="Center" 
        Click="goToDate_Click" />
    <TextBlock Text="تنظیم ماه قمری" Grid.Column="7" HorizontalAlignment="Center" 
    	VerticalAlignment="Center"/>
    <TextBox x:Name="TextBoxHijriAdjustment" Text="" Grid.Column="6" 
    	HorizontalAlignment="Stretch" VerticalAlignment="Center" Width="30" />
    <Button x:Name="ButttonHijriAdjustment" Content="  تنظیم  " 
    	Grid.Column="5" HorizontalAlignment="Center" VerticalAlignment="Center" 
        Click="ButttonHijriAdjustment_Click"  />
        
</Grid>

I also created a couple of Storyboards to show and hide the below panel.
This panel updates the database with your input data.

Image 3

The XAML of Storyboards is given below:

XML
<Storyboard x:Key="showEventGrid">
    <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="EventGrid" 
    	Storyboard.TargetProperty="(UIElement.Visibility)">
        <DiscreteObjectKeyFrame KeyTime="00:00:00" 
        Value="{x:Static Visibility.Visible}"/>
        <DiscreteObjectKeyFrame KeyTime="00:00:00.5000000" 
        Value="{x:Static Visibility.Visible}"/>
    </ObjectAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
    	Storyboard.TargetName="EventGrid" 
        Storyboard.TargetProperty="(UIElement.Opacity)">
        <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
        <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="1"/>
    </DoubleAnimationUsingKeyFrames>
    <ThicknessAnimationUsingKeyFrames BeginTime="00:00:00" 
    	Storyboard.TargetName="EventGrid" Storyboard.TargetProperty
        	="(FrameworkElement.Margin)">
        <SplineThicknessKeyFrame KeyTime="00:00:00" Value="0,0,0,0"/>
        <SplineThicknessKeyFrame KeyTime="00:00:00.5000000" Value="0,0,0,0"/>
    </ThicknessAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
    	Storyboard.TargetName="stackPanel" 
        Storyboard.TargetProperty="(UIElement.BitmapEffect)
        	.(BlurBitmapEffect.Radius)">
        <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="6"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="hideEventGrid">
    <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" 
    	Storyboard.TargetName="EventGrid" 
        Storyboard.TargetProperty="(UIElement.Visibility)">
        <DiscreteObjectKeyFrame KeyTime="00:00:00.5000000" 
        Value="{x:Static Visibility.Hidden}"/>
    </ObjectAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
    	Storyboard.TargetName="EventGrid" 
		Storyboard.TargetProperty="(UIElement.Opacity)">
        <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="0"/>
    </DoubleAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
    	Storyboard.TargetName="stackPanel" 
        Storyboard.TargetProperty="(UIElement.BitmapEffect).(BlurBitmapEffect.Radius)">
        <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="0"/>
    </DoubleAnimationUsingKeyFrames>
    <ThicknessAnimationUsingKeyFrames BeginTime="00:00:00" 
    	Storyboard.TargetName="EventGrid" 
        Storyboard.TargetProperty="(FrameworkElement.Margin)">
        <SplineThicknessKeyFrame KeyTime="00:00:00.5000000" Value="0,0,0,0"/>
    </ThicknessAnimationUsingKeyFrames>
</Storyboard>

The C# Code

In Persian Calendar, we have 12 months that I have shown in the following code snippet:

C#
//Season { MonthName(nomber of days in the month) ...}
Spring { Farvardin(31), OrdiBehesht(31), Khordad(31) }
Summer { Tir(31), Mordad(31), Shahrivar(31) }
Autumn { Mehr(30), Aban(30), Azar(30) }
Winter { Day(30), Bahman(30), Esfand(30 or 29) }

Fortunately there is a class in .NET for creating Persian Calendar and I've used it.

C#
PersianCalendar persianCalendar = new PersianCalendar();

The main method in this project to create a Persian month is calculateMonth.

C#
/// <summary>
/// The main method to show the calendar
/// This method shows `thisMonth` in `thisYear`
/// </summary>
void calculateMonth(int thisYear, int thisMonth)
{
    try
    {
        yearForNavigating = thisYear;
        monthForNavigating = thisMonth;

        DateTime tempDateTime = persianCalendar.ToDateTime(yearForNavigating, 
        	monthForNavigating, 15, 01, 01, 01, 01);

        int thisDay = 1;
        TextBlockThisMonth.Text = "";
        TextBlockThisMonth.Text =
            monthForNavigating.convertToPersianMonth() + " " +
            yearForNavigating.convertToPersianNumber();

        //Different between first place of calendar and first place of this month
        //اختلاف بین خانه شروع ماه و اولین خانه تقویم            
        string DayOfWeek = persianCalendar.GetDayOfWeek(persianCalendar.
        	ToDateTime(thisYear, thisMonth, 01, 01, 01, 01, 01)).ToString();
        int span = calculatePersianSpan(DayOfWeek.convertToPersianDay());

        decreasePersianDay(ref thisYear, ref thisMonth, ref thisDay, span);

        string persianDate;//حاوی تاریخ روزهای شمسی Contains the date of Persian
        string miladiDate;//حاوی تاریخ روزهای میلادی Contains the date of Christian
        string hijriDate;//حاوی تاریخ روزهای قمری Contains the date of Hijri

        string tooltip_context = "";//Contains the text of tooltip

        ////////////////////////////////////

        for (int i = 0; i < 6 * 7; i++)
        {
            tempDateTime = persianCalendar.ToDateTime(thisYear, thisMonth, 
            	thisDay, 01, 01, 01, 01);

            miladiDate = tempDateTime.Day.ToString() + " " + 
            	englishMonthName(tempDateTime.Month) 
            	+ " " + tempDateTime.Year.ToString();
            hijriDate = hijriCalendar.GetDayOfMonth
		(tempDateTime).convertToPersianNumber() 
                + " " + hijriCalendar.GetMonth
			(tempDateTime).convertToHigriMonth() + " " 
                + hijriCalendar.GetYear(tempDateTime).convertToPersianNumber();
            persianDate = thisDay.convertToPersianNumber();

            DayOfWeek = persianCalendar.GetDayOfWeek(tempDateTime).ToString();

            if (thisMonth == monthForNavigating)
            {
                tooltip_context = "";
                if (thisDay == currentDay && thisMonth == currentMonth && 
                	thisYear == currentYear)//بررسی تاریخ امروز
                {
                    tooltip_context = GetTextOfMemo
			(thisYear, thisMonth, thisDay, "Shamsi");
                    if (tooltip_context == "")
                        tooltip_context = GetTextOfMemo(tempDateTime.Year, 
                        	tempDateTime.Month, 
                        	tempDateTime.Day, "Miladi");
                    if (tooltip_context == "")
                        tooltip_context = GetTextOfMemo
				(hijriCalendar.GetYear(tempDateTime), 
                        	hijriCalendar.GetMonth(tempDateTime), 
                            	hijriCalendar.GetDayOfMonth(tempDateTime), "Hijri");

                    if (DayOfWeek.convertToPersianDay() == "جمعه")//بررسی جمعه بودن روز
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyleForHolydays_CurrentDay", "TextBlockStyle3", 
                            "TextBlockStyle12", "TextBlockStyle8", tooltip_context);
                    else
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyleToday", 
			"TextBlockStyle5", "TextBlockStyle10", 
                            "TextBlockStyle6", tooltip_context);
                }
                else if (searchShamsiTable(thisYear, thisMonth, thisDay))
                {
                    tooltip_context = GetTextOfMemo
			(thisYear, thisMonth, thisDay, "Shamsi");
                    if (isHoliday(thisYear, thisMonth, thisDay, "Shamsi"))
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyle5", "TextBlockStyle3", "TextBlockStyle12", 
                            "TextBlockStyle8", tooltip_context);
                    else
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyle4", "TextBlockStyle1", "TextBlockStyle10", 
                        	"TextBlockStyle6", tooltip_context);
                }
                else if (searchHijriTable(hijriCalendar.GetYear(tempDateTime), 
                	hijriCalendar.GetMonth(tempDateTime), 
                    hijriCalendar.GetDayOfMonth(tempDateTime)))
                {
                    tooltip_context = GetTextOfMemo
					(hijriCalendar.GetYear(tempDateTime), 
                    	hijriCalendar.GetMonth(tempDateTime), hijriCalendar.
                        GetDayOfMonth(tempDateTime), "Hijri");
                    if (isHoliday(thisYear, thisMonth, thisDay, "Hijri"))
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyle5", "TextBlockStyle3", "TextBlockStyle12", 
                            "TextBlockStyle8", tooltip_context);
                    else
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyle4", "TextBlockStyle5", "TextBlockStyle10", 
                            "TextBlockStyle6", tooltip_context);
                }
                else if (searchMiladiTable(tempDateTime.Year, tempDateTime.Month, 
                	tempDateTime.Day))
                {
                    tooltip_context = GetTextOfMemo
			(tempDateTime.Year, tempDateTime.Month, 
                    	tempDateTime.Day, "Miladi");
                    if (isHoliday(thisYear, thisMonth, thisDay, "Miladi"))
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyle5", "TextBlockStyle3", "TextBlockStyle12", 
                            "TextBlockStyle8", tooltip_context);
                    else
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyle4", "TextBlockStyle5", "TextBlockStyle10", 
                        	"TextBlockStyle6", tooltip_context);
                }
                else
                {
                    if (DayOfWeek.convertToPersianDay() == 
					"جمعه")//بررسی جمعه بودن روز
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyleForHolydays", "TextBlockStyle3", 
                            "TextBlockStyle12", 
                            "TextBlockStyle8", tooltip_context);
                    else
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyle2", "TextBlockStyle5", "TextBlockStyle10", 
                            "TextBlockStyle6", tooltip_context);
                }
            }
            else
            {
                if (DayOfWeek.convertToPersianDay() == "جمعه")//بررسی جمعه بودن روز
                    changeProperties(i, persianDate, hijriDate, miladiDate, 
                    	"RectangleStyleForOtherHolydays", "TextBlockStyle4", 
                        "TextBlockStyle13", "TextBlockStyle9", tooltip_context);
                else
                    changeProperties(i, persianDate, hijriDate, miladiDate, 
                    	"RectangleStyle3", 
                    	"TextBlockStyle2", "TextBlockStyle11", "TextBlockStyle7", 
                        tooltip_context);
            }

            increasePersianDay(ref thisYear, ref thisMonth, ref thisDay, 1);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK, 
        	MessageBoxImage.Error);
    }
}

Then I've used LINQ to update the database.
First I've added a LINQ to SQL class:

Image 4

Then I designed DataClasses1.dbml:

Image 5

Then we can update the database easily with a LINQ query.
For instance, I wrote this method to search shamsi table.

C#
/// <summary>
/// Searches the Database shamsi table
/// </summary>
/// <returns>If the input data is found in the table return true, else 
/// return false</returns>
bool searchShamsiTable(int year, int month, int day)
{
    bool result = false;
    try
    {
        int? id = null;
        id = (from q in db.shamsis
              where (q.year == year && q.day == day && q.month == month) 
              	|| (q.anniversary == true && q.month == month && q.day == day)
              select q.id).First();
        if (id != null) result = true;
    }
    catch { result = false; }
    return result;
} 

Further reading about LINQ to SQL can be done here.
Other methods are simple and easy to learn and I've explained them with some comments.

Point of Interest

The point of interest in this project (for me) was, using a WinForm control in WPF.
I've used a WinForm Masked TextBox for input year.

For using a WinForm Masked TextBox in a WPF App: I added these DLLs to my references:

  • WindowsFormsIntegration.dll
  • System.Windows.Forms.dll 

Then I added the following line within Window tag in the XAML source:

XML
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

At last, I added the MaskedTextBox control to my WPF App with WindowsFormsHost tag.
XML
<WindowsFormsHost Grid.Column="0" FontSize="13" 
	VerticalAlignment="Center" HorizontalAlignment="Center" 
    Width="70" Height = "25">
	<wf:MaskedTextBox x:Name="textBoxYear" Mask="0000"/>
</WindowsFormsHost>

Further reading about WindowsFormHost can be done here.

History

  • 12th February, 2009: First post
  • 6 th January , 2010 : Database was changed to a XML file and some Bugs were resolved.

License

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


Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionتشکر Pin
Member 133476385-Aug-17 8:12
Member 133476385-Aug-17 8:12 
QuestionLIKEEEEEE Pin
Rahman Hamidi13-Jul-16 22:04
professionalRahman Hamidi13-Jul-16 22:04 
Questionhijri calendar Pin
snow_cap5-Feb-13 11:04
snow_cap5-Feb-13 11:04 
QuestionRegarding Gregorian calendar Pin
gyanprakashis22-Oct-11 21:40
gyanprakashis22-Oct-11 21:40 
GeneralMy vote of 5 Pin
Dariush Tasdighi19-Mar-11 22:24
Dariush Tasdighi19-Mar-11 22:24 
QuestionCan You Add Database Version of the Program? Pin
parvinSh29-Nov-10 2:50
parvinSh29-Nov-10 2:50 
AnswerRe: Can You Add Database Version of the Program? Pin
Mohammad Dayyan29-Nov-10 5:37
Mohammad Dayyan29-Nov-10 5:37 
GeneralRe: Can You Add Database Version of the Program? Pin
parvinSh29-Nov-10 20:27
parvinSh29-Nov-10 20:27 
GeneralRe: Can You Add Database Version of the Program? Pin
Mohammad Dayyan30-Nov-10 1:43
Mohammad Dayyan30-Nov-10 1:43 
GeneralRe: Can You Add Database Version of the Program? Pin
parvinSh30-Nov-10 20:28
parvinSh30-Nov-10 20:28 
GeneralGreat work ! Pin
saddozai14-Jun-10 16:36
saddozai14-Jun-10 16:36 
GeneralExcellent work! Pin
Mehran Taherimoud27-Mar-10 2:30
Mehran Taherimoud27-Mar-10 2:30 
Generaluse something like "persian diary" in web application Pin
mohammad amin sharifi17-Nov-09 1:21
mohammad amin sharifi17-Nov-09 1:21 
GeneralRe: use something like "persian diary" in web application Pin
Mohammad Dayyan17-Nov-09 2:40
Mohammad Dayyan17-Nov-09 2:40 
GeneralHi Pin
soheila315510-Aug-09 7:49
soheila315510-Aug-09 7:49 
GeneralRe: Hi Pin
Mohammad Dayyan10-Aug-09 7:51
Mohammad Dayyan10-Aug-09 7:51 
GeneralNOT a standard routin & optimized code Pin
Member 53148905-May-09 11:58
Member 53148905-May-09 11:58 
GeneralRe: NOT a standard routin & optimized code Pin
Mohammad Dayyan16-May-09 16:37
Mohammad Dayyan16-May-09 16:37 
GeneralThank you Pin
zhujinlong1984091314-Apr-09 17:48
zhujinlong1984091314-Apr-09 17:48 
GeneralPersian to English Pin
fatih_senel23-Feb-09 11:00
fatih_senel23-Feb-09 11:00 
GeneralRe: Persian to English Pin
Mohammad Dayyan23-Feb-09 22:38
Mohammad Dayyan23-Feb-09 22:38 
GeneralRe: Persian to English [modified] Pin
anen3-Feb-10 19:13
anen3-Feb-10 19:13 
GeneralRe: Persian to English Pin
anen5-Feb-10 10:16
anen5-Feb-10 10:16 
GeneralRe: Persian to English Pin
Mohammad Dayyan5-Feb-10 10:22
Mohammad Dayyan5-Feb-10 10:22 
GeneralRe: Persian to English Pin
anen5-Feb-10 10:47
anen5-Feb-10 10:47 

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.