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));
}



Wednesday, June 25, 2008

How to Insert with Progress Bar C-Sharp

Hello guyz. In this code show you on how to use the progress bar with Insert command.

First Step:

OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "your connectionstring";
conn.Open();

int i;
int x = 1000; //insert 1000 rows
Bar.Maximum = x; //Bar ( Your progressbar)

for (i = 0; i < x; i++)
{
string sQL = "Insert Into table1" & _ "(empid,dates,username) values ('COLLADO','11/4/1982','Sayre')";

OleDbCommand cmd = new OleDbCommand(sQL, conn);
cmd.ExecuteNonQuery();

Bar.Value = Bar.Value + 1;
pr = Bar.Value;
pr1 = pr / x;
pr2 = pr1 * 100;
lblpercent.Text = pr2.ToString(); //show the percent in label
Application.DoEvents();
}

//Get the total row in the table
string sQL1 = "select * from table1";
OleDbCommand cmd1 = new OleDbCommand(sQL1, conn);
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(cmd1);
da.Fill(ds);
string ss = ds.Tables[0].Rows.Count.ToString();

Friday, May 16, 2008

How to change background color of Listview in C#

Hello guyz. I have simple code here to change the background color of Listview and it's cells during runtime.

Step 1:
Create first a Listview.

Step 2:
Put this code at your form_load event.

listView1.Items.Add("Hello World");
listView1.Items[0].SubItems[0].BackColor = Color.Blue;

Wednesday, May 14, 2008

How to Clear all Text in a TextBox in C#

Im using C sharp 3.0

Try This Codes:


private void button1_Click(object sender, EventArgs e)
{
foreach(Control sayre in this.Controls)
{
if (sayre is TextBox)
{
(sayre as TextBox).Clear();
}
}
}

How to Get Date and Time Difference in C#

Hello guyz. I have example here on how to get the difference of date and time in C#.

Step: 1
Create a timer then set the interval to 10 and enabled to true.

Step: 2
Create 3 Label. Label1,Label2,Label3.

Step: 3
Create 2 Datepicker. Name it dtfrom and dtto.

Step: 4
Create a Button1

Then Copy this Code at your form:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{label1.Text = dtfrom.Value.ToString();}
private void timer1_Tick(object sender, EventArgs e)
{label2.Text = string.Format("{0:G}", DateTime.Now);}
private void button1_Click(object sender, EventArgs e)
{
TimeSpan ts = new TimeSpan();
DateTime dt1,dt2 = new DateTime();
dt1 = dtfrom.Value;
dt2 = Convert.ToDateTime(label2.Text);
ts=dt1.Subtract(dt2);
label3.Text = ts.ToString(); //Answer
}


Thursday, May 8, 2008

Different Date Format in C#

DateTime sai = DateTime.Now; //Get Date and Time


Console.WriteLine("d format: {0:d}", sai);
Console.WriteLine("D format: {0:D}", sai);

Console.WriteLine("t format: {0:t}", sai);

Console.WriteLine("T format: {0:T}", sai);

Console.WriteLine("f format: {0:f}", sai);

Console.WriteLine("F format: {0:F}", sai);

Console.WriteLine("g format: {0:g}", sai);

Console.WriteLine("G format: {0:G}", sai);

Console.WriteLine("m format: {0:m}", sai);

Console.WriteLine("M format: {0:M}", sai);

Console.WriteLine("r format: {0:r}", sai);

Console.WriteLine("R format: {0:R}", sai);

Console.WriteLine("s format: {0:s}", sai);

Console.WriteLine("u format: {0:u}", sai);

Console.WriteLine("U format: {0:U}", sai);

Console.WriteLine("y format: {0:y}", sai);

Console.WriteLine("Y format: {0:Y}", sai);

Console.Read();





Monday, April 28, 2008

Opening Single Window at MDI Form C#

Hello guys. This simple example codes will open a single window. I have 2 ways of opening a window in MDI Form in C#.

1) Creating a Instance in a Form
2) Just create a variable of a Form


Example 1:

-Create a MDI Form and a Form1.

*At you Form1 write this codes:

private static Form1 sForm = null;
public static Form1 Instance()
{
if (sForm == null)
{ sForm = new Form1 (); }
return sForm;
}

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
sForm = null;
}

*In the MDI Form button for showing a Form1 write this codes:

Form sForm = new Form1.Instance();
sForm.MdiParent=this;
sForm.show();
sForm.Activate();


Example 2:

-Create a MDI Form and a Form1.

*Write this codes at your MDI Form:

//Create a variable:
private Form1 frm1 = new Form1();
//Button showing a Form1:
frm1.MdiParent=this;
frm1.show();



I hope this 2 ways of example can help you for those dont known on how to open a single window at C#.

Thursday, April 10, 2008

How to Pass values to another Form

Ok. Lets say you have 2 Forms. Form1 and Form2.
In Visual Basic 6 this is simple codes with 1 line only: Form2.text1.text = Form1.text1.text

Thats so Simple.

But in C# not 1 line code. I have 2 ways to get a text from Form1/Form2.

FIRST WAY:

1.) Goto Form1 then Double Click it. At the code type this.

public string CaptionText
{get {return textBox1.Text;}
set { textBox1.Text = value; }}


note: the value of your textbox1.text = sayre;

2.) Goto Form2 then Double click it. At the code type this.

// At your command button In Form2
private void button1_Click(object sender, EventArgs e)
{
Form1 sForm1 = new Form1();
textBox1.Text = sForm1.CaptionText;
}


SECOND WAY:

1.) Goto Form2 then Double click it. At the code type this.

public Form2(string sTEXT)
{
InitializeComponent();
textBox1.Text = sTEXT;
}


2.) Goto Form1 then Double click it. At the code type this.
//At your command button in Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 sForm = new Form2(textBox1.Text);
sForm.Show();
}