Code Heaven For C# Developer

Monday, August 30, 2010

Generic class in C#

A generic class is a class that have a type parameter representing the data type of the class variables.




create the object of this class----

Labels:

Wednesday, August 25, 2010

Serialization and deserialization.....

Suppose i have a class Article which i want to Serialize and Deserialize.In this class i hav three fields named public string sTitle, public string sAuthor, public string sText;

Here i will use two more namespaces--

using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

For this first of all place attribute named [Serializable()] upon this class and inherite this class to ISerilizable

[Serializable()]
public class Articles : ISerializable
{

}

In this class there are two constructor one for default and other for Iserilizable interface.I will create two more methods one for Serilization and other for Deserialization---

So the Whole Code will be like this....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace WindowsFormsApplication1
{
[Serializable()]

public class Articles : ISerializable
{

public string sTitle;
public string sAuthor;
public string sText;

public Articles()
{ }

public Articles(SerializationInfo info, StreamingContext context)
{

sTitle = Convert.ToString(info.GetValue("Title", typeof(String)));

sAuthor = Convert.ToString(info.GetValue("Author", typeof(String)));

sText = Convert.ToString(info.GetValue("Text", typeof(String)));

}

public void serialize(string fileName)
{

FileStream s;

s = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);

BinaryFormatter B = new BinaryFormatter();

B.Serialize(s, this);

s.Close();

}

public Articles deSerialize(string fileName)
{

FileStream Fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);

BinaryFormatter F = new BinaryFormatter();

Articles s1 = (Articles)F.Deserialize(Fs);

Fs.Close();

return s1;

}

#region ISerializable Members



public void GetObjectData(SerializationInfo info, StreamingContext context)
{

info.AddValue("Title", sTitle);

info.AddValue("Author", sAuthor);

info.AddValue("Text", sText);

}

#endregion

}
}

Now i am using button click event for using these methods...

private void button1_Click(object sender, EventArgs e)
{
Articles c1 = new Articles();

c1.sTitle = "My Title";

c1.sAuthor = "Author Name";

c1.sText = "This is my Text";

c1.serialize(@"C:\123.txt");
}

private void button2_Click(object sender, EventArgs e)
{
Articles c2 = new Articles();
Articles c3 = c2.deSerialize(@"C:\123.txt");

MessageBox.Show(c3.sTitle + c3.sAuthor + c3.sText);
}

Labels:

Tuesday, August 24, 2010

Select Color from Combo Box

How to select color from Drop down???






for this on form load i hav to add color on drop down list---

private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add(System.Drawing.Color.AliceBlue.Name);
comboBox1.Items.Add(System.Drawing.Color.Bisque.Name );
comboBox1.Items.Add(System.Drawing.Color.Red.Name);
}

Now fire comboBox1_DrawItem event----

void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Rectangle rect = e.Bounds;
if (e.Index >= 0)
{
string n = ((ComboBox)sender).Items[e.Index].ToString();
Font f = new Font("Arial", 9, FontStyle.Regular);
Color c = Color.FromName(n);
Brush b = new SolidBrush(c);
g.DrawString(n, f, Brushes.Black, rect.X, rect.Top);
g.FillRectangle(b, rect.X + 180, rect.Y + 5, rect.Width - 10, rect.Height - 10);
}
}

enjoy............

Labels:

Monday, August 23, 2010

=== Program that splits on lines with Regex (C#) ===

using System;
using System.Text.RegularExpressions;

class Program
{
static void Main()
{
string value = "cat\r\ndog\r\nanimal\r\nperson";
//
// Split the string on line breaks.
// ... The return value from Split is a string[] array.
//
string[] lines = Regex.Split(value, "\r\n");

foreach (string line in lines)
{
Console.WriteLine(line);
}
}
}

=== Output of the program ===

cat
dog
animal
person

Labels:

Wednesday, August 11, 2010

Sending simple mail via smtp

here i am going to send a simple mail on button click.I am creating a method containing From Address,To Address,Message Subject,Message details and MailServer(like "smtp.gmail.com")

public void SendMail(string mailFrom, string mailTo, string subject, string message, string mailServer)
{

StringBuilder strBuild = new StringBuilder();
MailMessage mailmessage = new MailMessage();
mailmessage.From = new MailAddress(mailFrom);
mailmessage.Priority = MailPriority.Normal;
SmtpClient client = new SmtpClient();
client.Credentials = new
NetworkCredential("webmaster@topglobalcapital.com","marcmel23");
client.Port = 587;
client.EnableSsl = true;
client.Host = mailServer;
try
{

mailmessage.To.Add(mailTo);
mailmessage.Bcc.Add("anubhav@ltechindia.com");
mailmessage.IsBodyHtml = true;
mailmessage.Subject = subject;
strBuild.Append(message);
mailmessage.Body = strBuild.ToString();
client.EnableSsl = true;
client.Send(mailmessage);
}
catch (Exception ex)
{

}

}

now i am going to call the method on button click

private void button1_Click(object sender, EventArgs e)
{
SendMail("newmember@TopglobalCapital.com", "acnubhav@gmail.com", "hi", "how r u ", "smtp.gmail.com");
}

Labels: