Monday, February 28, 2005

C#: only specific characters to be typed into a TextBox

private void TextBox1_KeyPress(object sender, ystem.Windows.Forms.KeyPressEventArgs e)
{
e.Handled = e.KeyChar < '0' e.KeyChar > '9';
}

C#: restrict a program to a single instance

static void Main()
{
Process ThisProcess = Process.GetCurrentProcess();
Process [] AllProcesses = Process.GetProcessesByName(ThisProcess.ProcessName);
if (AllProcesses.Length > 1)
{
MessageBox.Show(ThisProcess.ProcessName + " is already running", ThisProcess.ProcessName, MessageBoxButtons.OK, MessageBoxIcon.Error);
} else {
Application.Run(new MainForm());
}
}

C#: progress bar in status

/// SNIPPET #1 ///
private ProgressBar m_ctrlProgress;
private System.Windows.Forms.StatusBarPanel panelProgress;
private void MainForm_Load(object sender, System.EventArgs e)
{
m_ctrlProgress = new ProgressBar();
m_ctrlProgress.SetBounds(104, 2, panelProgress.Width, statusBar.Height - 4); statusBar.Controls.Add(m_ctrlProgress);
m_ctrlProgress.Minimum = 0;
m_ctrlProgress.Maximum = 100;
m_ctrlProgress.Value = 0;
}
public void SetProgressRange(int nMin, int nMax)
{
m_ctrlProgress.Minimum = nMin;
m_ctrlProgress.Maximum = nMax;
}
public void SetProgressValue(int nValue)
{
m_ctrlProgress.Value = nValue;
}

/// SNIPPET #2 ///
// application level
ProgressStatusBar psb;
ProgressPanel pp;
public Form1() {
pp = new ProgressPanel();
pp.AutoSize = StatusBarPanelAutoSize.Spring;
psb = new ProgressStatusBar();
psb.Parent = this;
psb.Panels.AddRange(new ProgressPanel[] {pp});
ResizeRedraw = true;
this.Controls.Add(psb);
}

private void button1_Click(object sender, System.EventArgs e)
{
psb.UpdateValue(pp, pp.Value+10);
Invalidate(true);
}

C#: Retrieving Browser Capabilities

Boolean ActiveXControls = <%=Request.Browser.ActiveXControls.ToString()%>
Boolean AOL = <%=Request.Browser.AOL.ToString()%>
Boolean BackgroundSounds = <%=Request.Browser.BackgroundSounds.ToString()%>
Boolean Beta = <%=Request.Browser.Beta.ToString()%>
String Browser = <%=Request.Browser.Browser%>
Boolean CDF = <%=Request.Browser.CDF.ToString()%>
Boolean Cookies = <%=Request.Browser.Cookies.ToString()%>
Boolean Crawler = <%=Request.Browser.Crawler.ToString()%>
Boolean Frames = <%=Request.Browser.Frames.ToString()%>
Boolean JavaApplets = <%=Request.Browser.JavaApplets.ToString()%>
Boolean JavaScript = <%=Request.Browser.JavaScript.ToString()%>
Int32 MajorVersion = <%=Request.Browser.MajorVersion.ToString()%>
Double MinorVersion = <%=Request.Browser.MinorVersion.ToString()%>
String Platform = <%=Request.Browser.Platform%>
Boolean Tables = <%=Request.Browser.Tables.ToString()%>
String Type = <%=Request.Browser.Type%>
Boolean VBScript = <%=Request.Browser.VBScript.ToString()%>
String Version = <%=Request.Browser.Version%>
Boolean Win16 = <%=Request.Browser.Win16.ToString()%>
Boolean Win32 = <%=Request.Browser.Win32.ToString()%>

C#: directory size

public static long DirSize(DirectoryInfo d)
{
long Size = 0;
// Add file sizes.
FileInfo[] fis = d.GetFiles();
foreach (FileInfo fi in fis)
{
Size += fi.Length;
}
// Add subdirectory sizes.
DirectoryInfo[] dis = d.GetDirectories();
foreach (DirectoryInfo di in dis)
{
Size += DirSize(di);
}
return(Size);
}

//using:
DirectoryInfo dir_info = new DirectoryInfo(path_to_the_directory);
long Size = DirSize(dir_info);

HTML: desable the theme on the page

<meta equiv="MSThemeCompatible" content="No">

JAVASCRIPT: Trim

String.prototype.trim = function() {
// skip leading and trailing whitespace
// and return everything in between
var trimmedString = this
trimmedString = trimmedString.replace(/^\s+/,"");
trimmedString = trimmedString.replace(/\s+$/,"");
return trimmedString;
}
//using:
var trimmed_str=field_name.value.trim();

C#: restart ASP.NET site using code!

HttpRuntime.UnloadAppDomain();

C#: files operations

using System.IO;

//create directory:
Directory.CreateDirectory("C:\\testcsharp");
//create subdirectory:
Directory.CreateDirectory("C:\\testcsharp\\dump");
//move:
Directory.Move("C:\\testcsharp\\dump", "C:\\dump");
File.Move( "c:\\gooli.jpg", "c:\\temp\\gooli.jpg");
//delete:
Directory.Delete("C:\\dump");
File.Delete( "c:\\gooli.jpg" );
//copy:
File.Copy( "c:\\gooli.jpg", "c:\\temp\\abc.jpg");
//size of the file:
FileInfo fi = new FileInfo("C:\\gooli.jpg");Console.WriteLine("File size of gooli.jpg: {0}", fi.Length);
//delete a directory recursively:
Directory.Delete("C:\\testcsharp", true);

C#: running processes on a machine

using System.Diagnostics;

public static Process[] GetProcesses();
public static Process[] GetProcesses(string);
Sample:
Process[] procList = Process.GetProcesses();
for ( int i=0; i<20; i++)
{
string strProcName = procList[i].ProcessName;
int iProcID = procList[i].Id;
}

If you want processes on the local machine, you use GetProcesses(), else you use GetProcesses(string machinename).

C#: start a new process

System.Diagnostics.Process.Start("http://www.c-sharpcorner.com" );

HTML: link style

A,A:LINK,A:VISITED,A:ACTIVE,A:FOCUS{
color:#16420A;
text-decoration:underline;
xxx: expression(this.hideFocus = true);
}

JAVASCRIPT: createPopup

var macamMailPopup = window.createPopup();

function openMacamMailPopup() {
var popupBody = macamMailPopup.document.body;
popupBody.style.backgroundColor = "lightyellow";
popupBody.style.border = "solid black 1px";
popupBody.innerHTML = document.getElementById("macamMailPopupHtml").innerHTML;
macamMailPopup.show(0, 0, 250, 85, document.getElementById("macamMailComment"));
}

C#: Run a new instance of IE from an application

run new instance of IE:
using SHDocVw;
public void OpenBrowser(string url)
{
object o = null;
SHDocVw.InternetExplorer ie = newSHDocVw.InternetExplorerClass();
IWebBrowserApp wb = (IWebBrowserApp) ie;
wb.Visible = true;
//Do anything else with the window here that you wish
wb.Navigate(url, ref o, ref o, ref o, ref o);
}