2011/06/20

Kenny's XML Encrypter



Name: Kenny's XML Encrypter
Version: 1.1
Platform: Windows with .Net Framework 2.0 installed
Introduction:
A small tool that can encrypt/decrypt xml element and attribute value. You can click Help button to see how to use it.
You can set different keys to encrypt different elements or attributes in the same time. Just don't forget those keys.
This tool only works on those leaf elements.
Play around, and leave a message at here if you found any problems.

PS: The save is done automatically when you click the "Encrypt" or "Decrypt" button so please don't forget to keep an original copy of your XML file.

2011/06/14

Refresh WebBrowser in Winform

I put a WebBrowser control on my form and use it to load XML files. Here is the code:
OpenFileDialog FileDialog = new OpenFileDialog();
FileDialog.Title = "Open XML Document";
FileDialog.Filter = "XML file (*.xml)|*.xml";

if (FileDialog.ShowDialog() == DialogResult.OK)
{
 WebBrowser1.Navigate(FileDialog.FileName);
 WebBrowser1.Refresh();
}
I found that if I load the second XML, sometimes it will not refresh itself to show the new XML content. Instead, it still shows the previous one. I google the web to see if there is any other way to prevent the cache. Unfortunately, I found nothing. But this post inspires me and so I create a workaround.

2011/05/24

Integrated the EasyAlgo FlashUpload control into the ASP.Net web application

Updated (2012.10.31):
I've removed this article due to this.

FlashUpload flash control to manage user uploads. It is a very powerful flash control that can let you customize the control. The price is affordabile (or you can try it free without limitations), and their customer service is outstanding. I use google talk to chat with them when I encounter any problem (also request some features!) with no problem.

OK, no more advertisement. :) Below is an example of what I've done for customizing it. The reason I put a blog here is because their document is good but not organized very well. I spent lots of time on puting things together... You can select many ways to integrate the flash control into your web app. Here I choose javascript.

2011/03/04

[javascript] Text wrapping in the alert/confirm function

Wrap words in the javascript alert (or confirm).

This doesn't work.
alert("First Line<br/>Second Line")

This doesn't work.
alert("First Line\r\nSecond Line")

This will work
alert("First Line\\r\\nSecond Line")

2011/02/26

Simple file upload by using FileUpload control.

What I need:
1.User can upload only one file at a time.
2.No save file to disk; read it's content and then dispose it.
3.Only txt and csv file types are allowed.

Here is the aspx page
<asp:Panel ID="PanelUploadList" runat="server" ViewStateMode="Disabled">
    <hr />
    Please select a text file to upload:
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:RequiredFieldValidator ID="RequiredFieldValidatorFileUpload" runat="server"
        ControlToValidate="FileUpload1"
        ErrorMessage="Please select a file to upload!" Display="Dynamic">*asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator ID="RegularExpressionValidatorFileUpload" runat="server"
        ControlToValidate="FileUpload1" ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.txt|.csv)$"
        ErrorMessage="Only txt and csv file types are allowed!">*asp:RegularExpressionValidator>
    <asp:Button ID="ButtonUpload" runat="server" OnClick="ButtonUpload_Click"
        Text="Upload" />
    <hr />
</asp:Panel>

2011/01/21

Sort ListBox items (ascending/descending)

1. Create a class that implements IComparer<T> interface.
private class SortListItem : IComparer<ListItem>
{
    public int Compare(ListItem x, ListItem y)
    {
        return String.Compare(x.Value, y.Value);
    }
}

DateTime: Get the first date of the week

1. Create an extension method.
public static class DateTimeExtensions
{
    public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
    {
        int diff = dt.DayOfWeek - startOfWeek;
        if (diff < 0)
        {
            diff += 7;
        }

        return dt.AddDays(-1 * diff).Date;
    }
}

Enum - simple example

class Program
{
  enum DateTypeEnum
  {
    Year = 1,
    Quarter = 2,
    Month = 4,
    Day = 8
  }

  static void Main(string[] args)
  {
    int DateType = 7;
    Console.WriteLine("Your available paying periods are: ");

    //check all available paying methods
    if ((DateType & (int)DateTypeEnum.Year) == (int)DateTypeEnum.Year)
      Console.WriteLine(DateTypeEnum.Year);

    if ((DateType & (int)DateTypeEnum.Quarter) == (int)DateTypeEnum.Quarter)
      Console.WriteLine(DateTypeEnum.Quarter);

    if ((DateType & (int)DateTypeEnum.Month) == (int)DateTypeEnum.Month)
      Console.WriteLine(DateTypeEnum.Month);

    if ((DateType & (int)DateTypeEnum.Day) == (int)DateTypeEnum.Day)
      Console.WriteLine(DateTypeEnum.Day);

    Console.WriteLine();

    int FavoritePayMethod = 4;
    //retrieve enum
    DateTypeEnum BillPeriod = (DateTypeEnum)Enum.Parse(typeof(DateTypeEnum), FavoritePayMethod.ToString());
    Console.WriteLine("Your favorite paying period is \"{0}\".", BillPeriod);
  }
}