Sunday, August 31, 2008

Alt Keys in C#

This sample code show on how to use Alt and Ctrl keys.


Try this code insert to your Form1_Keycode Event:


if ( e.Alt && e.KeyCode == Keys.S)
{//Pressed Alt+S}

else if (e.Alt && e.KeyCode == Keys.A)
{//Pressed Alt+A}

else if (e.Control && e.KeyCode == Keys.Y)
{//Pressed Ctrl+Y}

else if (e.Control && e.KeyCode == Keys.R)
{//Pressed Ctrl+R}

else if (e.Control && e.KeyCode == Keys.E)
{//Pressed Ctrl+E}

Hope this one can help you.

Thursday, August 28, 2008

Numeric TextBox and One Period

Hello guys.

I have sample here to show how to implement a numeric only to your textbox. tnx to mr.solitaire for the idea.

Put this code to your textbox in keypress.


bool dot = false; /for period
bool flag = false; /for minus

if (e.KeyChar == '\b') return;
if (e.KeyChar == '.' & textBox1.Text.IndexOf('.') > 0) dot = true;
if (textBox1.Text.StartsWith(".")) /if its dot first then
{ textBox1.Text = "0."; textBox1.SelectionStart = 3; }
if (e.KeyChar < '-' e.KeyChar > '9' dot == true) flag = true;
if (textBox1.Text.Length > 0 & e.KeyChar == '-') flag = true;
if (e.KeyChar == '/') flag = true;
if (flag == true) e.Handled = true;


Hope this one will help.

Check Item in listview if exist

Hello guys.

This sample code will show on how to check if the item in the listview is already existing.

// create a bool
bool found = false;

foreach (ListViewItem LVI1 in listView1.Items)
{
if (LVI1.Text == textBox1.Text)
{ found = true; }
}

if (!found)
{
ListViewItem LVI = new ListViewItem(textBox1.Text, 0);
LVI.SubItems.Add(textBox2.Text);
LVI.SubItems.Add(textBox3.Text);
listView1.Items.Add(LVI);
}
else
{ MessageBox.Show("Found " + textBox1.Text ); }

Thursday, August 21, 2008

Encrypt and Decrypt in C#

How to encrypt and decrypt text in C#?

I have simple way to do it.

Creat a form. 3 textbox and 2 command button.


private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = Convert.ToBase64String(Encoding.Unicode.GetBytes(textBox1.Text));
}


private void button2_Click(object sender, EventArgs e)
{
textBox3.Text = Encoding.Unicode.GetString(Convert.FromBase64String(textBox2.Text));
}