Freelancer

Tuesday, December 10, 2013

Textbox with placeholder by C#

It's here. A textbox with placeholder. Look like input control in html. Create a file named STextBox.cs and paste this code:

// -----------------------------------------------------------------------------
// File name: STextBox.cs
// Author: Smart Goat
// Email: dmtmd2010@yahoo.com.vn
// Blog: knowledgesharez.net
// Date: 2013-12-09
// Description: a text box with placeholder, similar the web input control.
//   - Properties:
//      + Placeholder: string - The content of the placeholder
//   - Methods:
//      + IsEmpty(): bool - Check if the text box is empty (recommended)
// -----------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Windows.Forms;

namespace FooBar
{
    class STextBox : TextBox
    {
        private bool textInputted;
        private Color backupForeColor = Color.Empty;
        private string placeholder = string.Empty;

        public string Placeholder
        {
            get { return placeholder; }
            set
            {
                placeholder = value;
                SetPlaceHolder();
            }
        }

        public bool IsEmpty()
        {
            return string.IsNullOrEmpty(Text) || !textInputted;
        }

        private void SetPlaceHolder()
        {
            if (string.IsNullOrEmpty(Text))
            {
                Text = placeholder;
                ForeColor = Color.Gray;
                textInputted = false;
            }
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            textInputted = !string.IsNullOrEmpty(Text);
        }

        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            if (!textInputted)
            {
                Text = string.Empty;
                ForeColor = backupForeColor;
            }
        }

        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            SetPlaceHolder();
        }

        protected override void OnForeColorChanged(EventArgs e)
        {
            base.OnForeColorChanged(e);
            if (backupForeColor == Color.Empty)
            {
                backupForeColor = ForeColor;
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.Control && e.KeyCode == Keys.A)
            {
                SelectAll();
            }
        }
    }
}

Sunday, December 8, 2013

Simple photoshop script

This is my simple script to join images using photoshop:

// create a new document size 30000x500 pixels at 72ppi
var doc = app.documents.add (30000, 500, 72);

// prepare the real width of the joined image
var wid = new UnitValue(0, "px");

// loop through all images
// 3 is the number of images
// images are store in c:\img folder
// images' file name: img1.jpg, img2.jpg, ...
for (i = 1; i <= 3; i++) {
    // take a file
    var fileRef = File("C:\\imgs\\img" + i + ".jpg");

    // open the image in a new document
    var imgdoc = app.open(fileRef);

    // resize the opened image to height 500px
    var ws = 500 * imgdoc.width / imgdoc.height;
    imgdoc.resizeImage(ws, 500);

    // copy the resized image to working document
    imgdoc.selection.selectAll();
    imgdoc.selection.copy();
    app.activeDocument = doc;
    doc.paste();

    // get the top layer
    var pslayer = doc.layers[0];

    // move the pasted image to the right position
    var position = pslayer.bounds;
    position[0] = wid - position[0];
    position[1] = 100 - position[1];
    pslayer.translate(position[0], -position[1]);

    // close the opened image and update the real width
    app.activeDocument = imgdoc;
    wid += imgdoc.width;
    imgdoc.close(SaveOptions.DONOTSAVECHANGES);
}

Monday, December 2, 2013

[Tutorial] Move form with no border

Just add the below snippet to your code (C#):

private const int WM_NCHITTEST = 0x84; // Mouse capture.
private const int HTCLIENT = 0x1;      // Client area.
private const int HTCAPTION = 0x2;     // Title bar.

// This function intercepts all the commands sent to the application.
// It checks if user click (WM_NCHITTEST) on the form area (HTCLIENT)
// It fakes the result as the title bar (HTCAPTION)
// This makes the form thinks user clicked on the title bar.
// So we can click and move form by (fake) title bar.
protected override void WndProc(ref Message message)
{
    base.WndProc(ref message);

    if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
    {
        message.Result = (IntPtr)HTCAPTION;
    }
} 

Thursday, October 17, 2013

[Tutorial] Add offline javadoc to eclipse

If you are working with eclipse, the javadocs come from internet. And eclipse takes a bit delay to show the docs. You can config your eclipse to use offline javadocs. This will make eclipse faster and you can see the docs without internet. These are the step:
1. Download javadocs:
Java 6: http://docs.oracle.com/javase/6/docs/
Java 7: http://docs.oracle.com/javase/7/docs/
Download the javadocs zip file and put in any where that you want.
2. Config eclipse:
add offline javadoc
Select Preferences
add offline javadoc to eclipse
Select Installed JREs
add offline javadoc to eclipse
Select the java version that you want to add docs and then select Edit
add offline javadoc to eclipse
Select rt.jar package and then select Javadoc Location
Select Javadoc in archive and browse the zip that you downloaded
Done

Sunday, October 6, 2013

How the CPU works

Every computer, smartphone, ... has a CPU inside. It's the brain of these devices. But how it works? After surfing around for a while, I found this. Take a look at how a CPU works:


Summary:
First, all devices have their own machine code to tell them what to do.
CPU takes a string of bits (machine code - designed by the manufacturer) and output another string of bits that do some special tasks or tell the devices what to do.
'The strings of bits' - intructions, are stored in RAM (random access memory). CPU read (input) these instructions and execute an output. The output maybe tell the hard disk to write somethings. Maybe store somethings to RAM again.

Wednesday, September 25, 2013

[Tip] Show all sheets in Excel 2013

If you have tried Excel 2013, you would see a new style of manage sheets. For somebody, it's strange and hard to use if there are many sheets.
Today I will show you how to view all sheets and move to any sheet you want. It's very easy!

Excel sheets
Excel sheets



Right click on the arrow

excel sheets tooltip

And here is what you'll see!

excell all sheets