2011年12月28日 星期三

[HTML5] A sample for drag and drop.




<html>
 <body>
  <form>
<ul id="list1" ondragstart="onDragStart(event)"
ondragenter="onDragEnter(event)"
ondragover="onDragOver(event)"
ondrop="onDrop(event)">
<li id="apple1" draggable="true">CCCCC</li>
<li id="orange1" draggable="true">OOOOO</li>
<li id="grape1" draggable="true">ooooo</li>
</ul>
<ul id="list2" ondragstart="onDragStart(event)"
ondragenter="onDragEnter(event)"
ondragover="onDragOver(event)"
ondrop="onDrop(event)">
<li id="apple2" draggable="true">APPLE</li>
<li id="orange2" draggable="true">ORANGE</li>
<li id="grape2" draggable="true">GRAPE</li>
</ul>
</form>

  <script type="text/javascript">
function onDragStart(event)
{
if (event.target.tagName.toLowerCase() == "li")
{
event.dataTransfer.setData('Text', event.target.id);
}
else
{
event.preventDefault();
}
}

function onDrop(event)
{
var iid = event.dataTransfer.getData('Text');
var li = document.getElementById(iid);

if (li && li.parentNode != event.currentTarget)
{
li.parentNode.removeChild(li);
event.currentTarget.appendChild(li);
}
event.stopPropagation();
}

function onDragOver(event)
{
event.preventDefault();
}

function onDragEnter(event)
{
var types = event.dataTransfer.types;
for (var i=0; i<types.length; i++)
{
if (types[i] == 'Text')
{
event.preventDefault();
return;
}
}
}
  </script>
 </body>
</html>




















Reference: http://www.books.com.tw/exep/prod/booksfile.php?item=0010480450

2011年12月27日 星期二

[HTML5] A simple clock.

This is a shabby clock.



<html>
 <body>
  <canvas id="canvas1" width="300" height="200"></canvas>
  <script type="text/javascript">

  setInterval(drawClock, 1000);

function drawClock()
{
// get canvas and datetime
var canvas = document.getElementById("canvas1");
  var cx = canvas.getContext("2d");
  var currentDate = new Date();
  var hours = currentDate.getHours();
  var mins = currentDate.getMinutes();
  var secs = currentDate.getSeconds();
 
  cx.clearRect(0, 0, canvas.width, canvas.height);
 
  cx.fillStyle = "red";
  cx.fillRect(0, 0, 2 * hours * 60 / 24, 20);
  cx.fillText("Hour: " + hours, 130, 20);
 
  cx.fillStyle = "blue";
  cx.fillRect(0, 20, 2 * mins, 20);
  cx.fillText("Minute: " + mins, 130, 40);
 
  cx.fillStyle = "green";
  cx.fillRect(0, 40, 2 * secs, 20);
  cx.fillText("Second: " + secs, 130, 60);
}
  </script>
 </body>
</html>




This is its output.

2011年12月26日 星期一

[HTML5] Hello Canvas!

This is a simple example to draw on a HTML5 canvas with javascript.


<html>
<body>
<canvas id="canvas1" width="300" height="200"></canvas>
<script type="text/javascript">
// get canvas
var canvas = document.getElementById("canvas1");
var context = canvas.getContext("2d");

// fill rectagle
context.fillRect(0,0,150,100);

// fill text
context.fillText("Hello, Canvas!", 155, 110);

// draw a line
context.beginPath();
context.moveTo(0, 100);
context.lineTo(300, 100);
context.moveTo(150, 0);
context.lineTo(150, 200);
context.rect(0, 0, 300, 200);
context.stroke();
context.closePath();
</script>
</body>
</html>


The result such like below,


Reference: http://www.books.com.tw/exep/prod/booksfile.php?item=0010480450

2011年12月16日 星期五

[SQLite] Truncate table

In SQL server or mysql, you can delete all data from a table and set auto-increment identity to zero by the command below:

TRUNCATE TABLE [TableName];

In SQLite, there is no command for truncate tables, you should remove data and set identity by yourself.

DELETE FROM  [TableName]; 
UPDATE sqlite_sequence SET seq=0 WHERE name=' TableName'; 

2011年12月14日 星期三

[.Net] New Line in TextBox

In ASP.net, you should use \n for getting your mouse cursor to new line.
But in WindowsApplication, you should use \r\n to get new line.

\n is new line character for Linux system occupy 1 byte.
\r\n is new line character for Windows system occupy 2 bytes.

2011年12月13日 星期二

[.Net] Create SQLite database and datatable with .net framework

This is sample code for creating a new database file and table in SQLite.

            string m_dataSource = "test1.s3db";
            SQLiteConnection.CreateFile(m_dataSource);
            DbProviderFactory factory = SQLiteFactory.Instance;
            using (DbConnection conn = factory.CreateConnection())
            {
                // Create DB
                conn.ConnectionString = "Data Source=" + m_dataSource;
                conn.Open();
                // Create Table
                string sql = "create table [aa] ([id] INTEGER PRIMARY KEY, [s] TEXT COLLATE NOCASE)";
                DbCommand cmd = conn.CreateCommand();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
            }



Reference:
http://hirocat.pixnet.net/blog/post/40514692-%E5%A6%82%E4%BD%95%E9%AB%98%E6%95%88%E4%BD%BF%E7%94%A8sqlite-.net-(c%23)