Friday, November 6, 2009

Creating XML File on Visual Studio 2010

Hello Guys,

This code snippet will show you on how to create a simple xml file in Visual Studio 10 Beta 2.

Step 1: Add a namespace to your code behind System.Xml and System.Text

Step 2: In your event/method to create an xml file

//Create a path were to save the xml file
String path = Server.MapPath(@”FolderName\MyFileName.xml”);

//Instantiate an XmlWriterSettings
var xmlWrite = new XmlWriterSettings();

//And other Declaration
xmlWrite.Indent = true;
xmlWrite.OmitXmlDeclaration = true;
xmlWrite.Encoding = Encoding.ASCII;

//Then try to write a data in xml file
Using (var write = XmlWriter.Create(path,xmlWrite))
{
write.WriteComment(“This is a basic sample on how to create xml file”);
write.WriteStartElement(“Head”);
write.WriteStartElement(“Header”);
write.WriteStartAttribute(“Header”);
write.WriteValue(HeaderValue);
write.WriteEndAttribute();
write.WriteEndElement();
write.WriteStartElement(“Footer”);
write.WriteStartAttribute(“Footer”);
write.WriteValue(FooterValue);
write.WriteEndAttribute();
write.WriteEndElement();
write.Flush();
}

That’s it. This sample created on asp.net project.

Simple CRUD on Visual Studio 2010

Hello Guys,

I have sample here on how to create a simple Create, Read, Update, and Delete on Database. This sample created on Visual Studio 2010 Beta 2 using C# and Asp.Net. I did’nt include a validation on this sample. I only want to show on how to Select, Insert, Update, Delete on Database. For those who beginners I hope it will help.

You can now download my Sample File at this Link.

Sunday, June 21, 2009

How to Drag Object

Hi, this simple code will show you on how to drag a form with out window title bar.

1. Just input this code in your form code behind:


//Drag Forms w/out titlebar
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd,
int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
//End

2. Input a Panel in your form as your title bar.


private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}

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