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.

handler

Source

Cheers!


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
                    return;
                }
                else if (m.WParam.ToInt32() == SC_MAXIMIZE)
                {
                    //my code for the maximize event
                    return;
                }
            }
            base.WndProc(ref m);


Cheers!

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>";


Cheers!

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">
      <controls>
        <add tagPrefix="uc" tagName="contact_form" src="~/uc/contact_form.ascx"/>
      </controls>
    </pages>

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" />
</div>


Cheers!

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:
<human_check>
  <qa>
    <q>2 + 2</q>
    <a>4</a>
  </qa>
  <qa>
    <q>1 + 2</q>
    <a>3</a>
  </qa>
  <qa>
    <q>6 + 3</q>
    <a>9</a>
  </qa>
  <qa>
    <q>3 + 5</q>
    <a>7</a>
  </qa>
</human_check>

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;
}



Cheers!