Sunday, November 25, 2012

Windows - Removing AutoPlay items from the Options list

AutoRun dialog pops up each time you connect an USB storage device or insert a CD/DVD/BRay disc. You can remove unwanted items by changing the registry Handlers.

1. Go to Start and click Run, in the dialog box type regedit and hit Enter.
2. Navigate to HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ AutoplayHandlers \ Handlers and remove unwanted items by deleting them from the registry.




Friday, November 2, 2012

Winforms/C# - Overriding default minimize and maximize form events

Overriding the default system behavior is simple. Just need to capture machine events and modify them as we wish.

//overrides minimize and maximize default behavior
        private const int WM_SYSCOMMAND = 0x0112;
        private const int SC_MINIMIZE = 0xf020;
        private const int SC_MAXIMIZE = 0xf030;

        protected override void WndProc(ref Message m)
            if (m.Msg == WM_SYSCOMMAND)
                if (m.WParam.ToInt32() == SC_MINIMIZE)
                    m.Result = IntPtr.Zero;
                    //my code for the minimize event
                else if (m.WParam.ToInt32() == SC_MAXIMIZE)
                    //my code for the maximize event
            base.WndProc(ref m);


Thursday, October 25, 2012

PHP/JS - Echo a javascript function

Normally, if you want to echo a javascript function you should use escaping characters otherwise it won't work.

Compilable, but not working:

echo "<li><a href='#' id='lnk_admin' name='lnk_admin'
            onclick='window.location.replace(my_page.php'); '>Click Me</a></li>";

Compilable, AND working:
echo "<li><a href='#' id='lnk_admin' name='lnk_admin'
            onclick=\"window.location.replace('my_page.php');\">Click Me</a></li>";


Thursday, September 27, 2012

ASP.NET - Registering an user control in web.config

Instead of registering an user control (.ascx) on top of every page we need it, there's a way to register it on the web.config file one time only.
For example, here I am registering an user control of a contact form:

    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
        <add tagPrefix="uc" tagName="contact_form" src="~/uc/contact_form.ascx"/>

And then I can use it anywhere on my website:

<div style="width: 1000px; min-height: 510px;">
<uc:contact_form ID="my_contact_form" runat="server" />


Friday, September 21, 2012

ASP.NET/C#/XML - Get one random node from XML file

I will show the way I retrieve a single random node from XML file using ASP.NET/C#. In this case I will use an XML file used to store math problems and solution for human verification on an online "Contact Us" form.

XML file format:
    <q>2 + 2</q>
    <q>1 + 2</q>
    <q>6 + 3</q>
    <q>3 + 5</q>

C# code:
public static string get_math_problem(){

Random rnd = new Random();
XDocument questions = XDocument.Load(@"C:\FILE_LOCATION\human_check.xml");

var qas = questions.Descendants("qa");
int qaCount = qas.Count();
return qas.ElementAt(rnd.Next(0, qaCount - 1)).Element("q").Value;