martedì 30 settembre 2008

CSharp NumericTextBox control

I've post here a simple custom control that it accepts only numeric input.
The only one methods in the OnKeyPress (overrided) and accept only digit or the backspace
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using DrLuiji.Controls
{
public class NumericTextBox : TextBox
{
protected
override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (!((Char.IsDigit(e.KeyChar))||(e.KeyChar.Equals('\b'))))
{
e.Handled = true;
ErrorBeep();
}
}
public void ErrorBeep()
{
//Console.Beep();
}
}
}

To use it you just drag & drop the control from the toolbox to your form.

Have fun.

lunedì 22 settembre 2008

Retrieve system folder c#

Here you can find a way to retrieve the system folder.
Very simple, call the System.Environment.GetFolderPath with the corrisponding enum System.Environment.SpecialFolder.See the sample below.

private string GetSystemSpecialFolderDirectory()
{
//Program files
string sProgramFilesFolder = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.ProgramFiles);

//Personal
string sPersonalFolder = System.Environment.GetFolderPath (
System.Environment.SpecialFolder.Personal);

//Desktop
string sDesktopFolder = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.DesktopDirectory);

//Application Data
string sApplicationDataFolder = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.ApplicationData);

//System:
string sSystemFolder = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.System);


MessageBox.Show("Program files: " + sProgramFilesFolder +
"\r\nPersonal: " + sPersonalFolder +
"\r\nDesktop: " + sDesktopFolder +
"\r\nApplication Data: " + sApplicationDataFolder +
"\r\nSystem: " + sSystemFolder);
}

Very simple isn't it ?