Iteration over dynamically added nested controls in C#

A generic function to use for any control type in any container at any levels:

public static List<T> FindControls<T>(Control container, bool dig) where T : Control
{
    List<T> retVal = new List<T>();
    foreach (Control item in container.Controls)
    {
        if (item is T)
            retVal.Add((T)item);
        if (dig && item.Controls.Count > 0)
            retVal.AddRange(FindControls<T>(item, dig));
    }
    return retVal;
}

The dig variable controls whether or not to iterate through child container controls.
For example if you want to find all PictureBox controls starting from your first GroupBox (gbFirst),
you must call

FindControls<PictureBox>(gbFirst, true);

If set dig to false, only controls in the input cotainer will be enumerated.

Get Computer Name, Local IP Address and User Name in C#


//returns COMPUTERNAME
string computerName = Environment.MachineName; 

//returns "computername\username"
string computerAndUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 

//returns username
string userName2 = Environment.UserName; 



//Returns A string that contains the DNS host name of the local computer.
string hostName = Dns.GetHostName();  

//IPHostEntry-  Internet host address information.
//Dns.GetHostEntry(String)- Resolves a host name or IP address to an IPHostEntry instance.
IPHostEntry host = Dns.GetHostEntry(hostName);

//Gets or sets a list of IP addresses that are associated with a host.
IPAddress[] ipAddresses = host.AddressList;

string ipAddress = String.Empty;
foreach (IPAddress ip in ipAddresses)
{
    if (ip.AddressFamily.ToString()=="InterNetwork")
    {
        ipAddress = ip.ToString(); 
        break;
    }
}

Console.WriteLine(ipAddress);

CSS Sprite Technique

CSS-

<style>
    a.traffic {
        display: block;
        height: 50px;
        overflow: hidden;
        position: relative;
        width: 50px;
    }

    a.traffic span {
        background: url('TrafficSprite.png') no-repeat;
        display: block;
        height: 100%;
        left: 0;
        position: absolute;
        top: 0;
        width: 100%;
    }

    a.traffic:hover span {
        background-position:  -48px 0;
    } 

</style>

HTML-

 <a class="traffic" href=""><span></span></a>

Here is the sample image-
TrafficSprite

JavaScript form validation

<form name="myform" onsubmit="return validateForm()">
   <!-- No space or no other tag can exist between input and img tags. -->
   <input type="text" name="FullName"/><img style="position:absolute; display:none;"   
       src="~/Assets/MyImages/FormValidationError.png" />
</form>
<script>
    function validateForm() {

        var form = document.forms["myform"];
        var fullNameTextBox = form["FullName"];            
        var fullName =fullNameTextBox.value;      
        if (fullName == null || fullName == "") {
            alert("Name must be filled out.");
            
            var errorImageElement= fullNameTextBox.nextSibling; // no space/tag allowed between input and img
            //OR, var errorImageElement= fullNameTextBox.nextElementSibling; //space(s) allowed between input                  
                                                                             and img
   
            errorImageElement.style.display = 'inline';
            //OR, errorImageElement.setAttribute('class', 'visible');
            return false;
        }

        return true;
    }

    //Code to validate file input.
    //if (document.getElementById("UploadFile").files.length == 0) {
    //    console.log("no files selected");
    //}
</script>

Create file in C#

private static void CreateFile(string path)
{
    if ((!File.Exists(path)))
    {
        FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
        fs.Close();
        fs.Dispose();
        fs = null;
    }
}

JavaScript form validation

<!DOCTYPE html>
<html>
<head>
<script>
function validateForm() {
    var x = document.forms["myForm"]["fname"].value;
    if (x == null || x == "") {
        alert("Name must be filled out");
        return false;
    }
}
</script>
</head>
<body>

<form onsubmit="return validateForm()">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>

</body>
</html>

HTML Checkbox Tips and Tricks

//Checkbox checked change event handler
$('#CheckBox').click(function () 
{
    var thisCheck = $(this);

    if (thisCheck.is(':checked')) 
    {
       $('#span2').hide();
    }
    else
    {
       $('#span2').show();
    }
});

//Get checkbox value (checked/unchecked)
var checkBoxValue = $('#CheckBox').is(':checked');

Usage of custom culture in ASP.NET MVC Razor

using System.Globalization;

namespace MvcCultureTest.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            CultureInfo CurrentCultureInfo = new CultureInfo("en-US"); //declare culture
            ViewBag.CultureName = CurrentCultureInfo;
            ViewBag.MyNumber = 1235054;
            return View();
        }
    }
}




@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body>
    <div> 
        @ViewBag.MyNumber.ToString("N", ViewBag.CultureName); 
    </div>
</body>
</html>