Saturday, May 13, 2006

force number input in .NET



This built-in function can be used to force user to input numbers:

if (!char.IsNumber(e.KeyPress))
messagebox.show("plz enter numbers only")

also, "decimal.tryparse" can be used to check if the input is a number after input

Friday, May 12, 2006

my babies


some of them are alomost 2 months old, majority are 3 wks or 1 wks old, happy begging for food all the time, never enough~

They energize my study and work as well as relax me when I am tired:)

Fibonacci Sequence number calculation (int16)

this chunk of code calculates Fibonacci Sequence number base on user input using C#.NET:

private void btnGetNumber_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text == "" txtInput.Text == null) //to avoid empty input
{
MessageBox.Show("Please input a positive Integer between 0 and 62", "Error: Empty Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtInput.Clear();
txtInput.Focus();
}
else
{
int theNum = Convert.ToInt16(txtInput.Text);
if (theNum == 0) //to avoid zero input
{ MessageBox.Show("Please input a positive Integer between 0 and 62", "Error: Zero Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtInput.Clear();
txtInput.Focus();
}
else if (theNum <> 46) //to avoid overflow result, if the variables are defined for bigger values, this condition should be commented out and use OverflowException
{
MessageBox.Show("Please input a positive Integer between 0 and 62", "Error: Result Overflew", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtInput.Clear();
txtInput.Focus();
}
else //all conditions met, calculate the result
{
int numA = 0; int numB = 1; int numC;
for (int i = 1; i <= theNum; i++)

{
numC = numA;
numA = numB;
numB = numC + numB;
}
//print result to screen
lblAnswer.Text = numA.ToString();
} } }
//uncomment the code below for catching overflow exception when the variables are
//defined as uint64 or ulong, that is, too big to guess the limit
//catch (OverflowException)
//{
// MessageBox.Show("Please try again with another smaller positive Integer", "Error: Result OverFlow", MessageBoxButtons.OK, MessageBoxIcon.Error);
// txtInput.Clear();
// txtInput.Focus();
//}
//throw error for string or decimal inputs
catch (FormatException)
{
MessageBox.Show("Please input a positive Integer between 0 and 62", "Error: String or decimal Input", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtInput.Clear();
txtInput.Focus();
}
//throw error for unknown errors
catch (Exception ee) { MessageBox.Show("Error: Unknown", ee.Message); Application.Exit(); } }

validate positive integer from user input

uint holds only positive numbers, this event checks if the input is a validate positive integer, otherwise throw error using C#.NET

private void button1_Click(object sender, EventArgs e)

{

uint value = 0;

if (!uint.TryParse(textBox1.Text, out value) value == 0)

{MessageBox.Show("Wrong format");

return;}

textBox2.Text = value.ToString();

}

Tuesday, May 02, 2006

PHP sample login code


just done a sample, hope its helpful for newbies like me
Click here to download phploginsample.rar. Don't forget to create a db called news_users, with 3 columns "username" (varchar/char, PK), "passwd" (varchar/char), and "access_level"(smallint, 0 or 1).

lesson learnt from PHP Parse error


silly word wrap, some environment doesnt support automatically word wrap when copy and paste code. so the solution is:

put "//" in front of every line of comment to prevent the error caused by a multiline comment led by only have "//" in front of first line.

copy-pasters be aware!