Thursday 26 April 2012

Simple Form Design With Menu Strip in c#





     To Make a application like this….. Kindly Follow the below steps...

  1.       Open VS 2008 à file à new à project à WindowsFormApplication 
 2.       Rename it as FormDesign.
 3.       Select View à Toolbox  à  TableLayoutPanel
 4.       Design it for your needs….
                 I created one and designed that  looks like below…

       5.       Where u want to put image just put PictureBox there from ToolBox..
 6.       Right click on PictureBox and select properties ..
 7.       In properties under image select Local Resource and then  browse  image and select it that will look like below…

      8.       Then I added one panel at right top of the form and added label in panel and rename the text of the label into “knowledge share”  and style of the font by it properties…
It looks like below…

        9.       Then add Panel for other table cells.
 10.   Insert MenuStrip from ToolBox for left bottom panel and set its dock property is left…
 11.   Then add menu items like PictureBox1, PictureBox2, PictureBox3.
 12.   Then add three PictureBoxes for right bottem panel...
It Looks Like below…

       13.   Then assign Pictures for(picture box 1, 2, 3) them  and change their “sizemode” property into zoom for better view of large size images…..
 14.   Then double click on the menu items and type the below codes for each menu items.

private void picture1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox2.Visible = true;
            pictureBox3.Visible = false;
            pictureBox4.Visible = false;
        }

private void picture2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox3.Visible = true;
            pictureBox2.Visible = false;
            pictureBox4.Visible = false;
        }

private void picture3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox4.Visible=true;
            pictureBox2.Visible=false;
            pictureBox3.Visible=false;
  }
      15.   Then compile and run…


Click Here To Download Source

Friday 6 April 2012

Simple Mail Function in C# using SMTP

Open a new Windows Application .... named it mailtest

Design Form1like below,

Double click on button one "Send"...

Type the Below Code...



System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
            mm.To.Add(new              System.Net.Mail.MailAddress("r.shanmugam89@gmail.com","shanmugam"));
           
            mm.From = new System.Net.Mail.MailAddress("Simple@mailfunction.com");
            mm.Sender = new System.Net.Mail.MailAddress("username""password");
            mm.Subject = "This is Test Email";
            mm.Body = "<h3>This is Testing SMTP Mail Send By Me</h3>";
            mm.IsBodyHtml = true;
            mm.Priority = System.Net.Mail.MailPriority.High; // Set Priority to sending mail
            System.Net.Mail.SmtpClient smtClient = new System.Net.Mail.SmtpClient("hostname",port number);            
            smtCliend.Credentials = new NetworkCredential( "username""password" );
            smtClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
 smtClient.Send(mm);


Here, Username must be your email id, password must be that email password...
          Host Name is from which server you are sending mail,
              eg, gmail, yahoo... or from any of mail servers...
Gmail SMTP Host name is smtp.gmail.com...
          Default port number 25..


Double Click on button2 "Exit" and the code below...


Application.Exit();


Source Code:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;

namespace mailtest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
            mm.To.Add(new              System.Net.Mail.MailAddress("shanmugamXXXX@gmail.com","shanmugam"));
           
            mm.From = new System.Net.Mail.MailAddress("Simple@mailfunction.com");
            mm.Sender = new System.Net.Mail.MailAddress("username", "password");
            mm.Subject = "This is Test Email";
            mm.Body = "<h3>This is Testing SMTP Mail Send By Me</h3>";
            mm.IsBodyHtml = true;
            mm.Priority = System.Net.Mail.MailPriority.High; // Set Priority to sending mail
            System.Net.Mail.SmtpClient smtClient = new System.Net.Mail.SmtpClient("hostname",port number);
            //smtCliend.Host = "mail.shanmugam.com";
            //smtCliend.Port = 25;    // smtp port no 
            //smtCliend.Credentials = CredentialCache.DefaultNetworkCredentials;
            smtCliend.Credentials = new NetworkCredential( "username""password" );
            smtClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            try
            {
                smtClient.Send(mm);
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.ToString());
            }
           
        }
    }
}





Wednesday 4 April 2012

DataType Enum


Open a Windows Forms Application in Visual studio 2008, named it Enum2Combo
Then,


Type the Below code in Form1.cs, then compile and run…

Sample Program

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Enum2Combo
{
    public partial class Form1 : Form
    {
        enum Metrocities
        {
            Chennai,
            Mumbai,
            NewDelhi,
            Kolkatta,
        }

        public Form1()
        {
            InitializeComponent();
        }


        //Double Click On Load Button in Design

        private void button1_Click(object sender, EventArgs e)
        {
            Array arr;
            arr = Enum.GetValues(typeof(Metrocities));
            comboBox1.Items.Clear();
            foreach (Metrocities m in arr)
            {
                comboBox1.Items.Add(m);
            }

        }

       
    }
}

Here, comboBox1.Items.Clear(); used to avoid multiple insert.. That is Enum Values inserted only once in combobox…


Output:

                 Before Button Load Click




                                                     After Button Load Click



Tuesday 3 April 2012

Sample Program Creation in c# Console Application


The sample program to find the factorial of a given number in c#...

Open a Console Application in Visual studio 2008, named it Factorial…
Then, Type the Below code in Program.cs, then compile and run…

Sample Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            int no = 0, fact = 1;
            Console.WriteLine("Enter the value to find factorial....");
            no = int.Parse(Console.ReadLine());
            Console.WriteLine("The factorial value is....");
            for (int i = 1; i <= no; i++)
            {
                fact = fact * i;
            }
            Console.WriteLine(fact);
            Console.ReadLine();
        }
    }
}

Here, no = int.Parse(Console.ReadLine()); represents the explicit conversion.

That is, converting the string value into integer value…

To Build:  Cntl+Shift+B
To Run:    Press F5

 Output:

Monday 2 April 2012

Loading Record to Datagridview from sql server tables in c#...

Open a new Windows Project.....
 Open Form1.cs Design...
 Add a Datagriview from Toolbox...
 add a Button named LoadTable,
 Double Click on Button LoadTable, it will direct to form1.cs ...
...
...
private void button1_Click(object sender, EventArgs e)
        {

        }

Then,
Right click on Namespace in Solution Explorer,
then click, add  new item  Application Configuration File, 

then type the below code, to connect to sqlserver database.

...
...
 <connectionstrings>
    <add connectionstring="Data source=.;database=MassMail;Integrated Security=true;Initial Catalog=MassMail" name="sql" providername="System.Data.SqlClient">
  </add></connectionstrings>
...
...

here connection string name is "sql"
database name is "massmail"
server name is "." it means choosing default server...

To Connect to sql server, Right click on Namespace in Solution Explorer, then click add  new item Class named Common.
Type the Below Code,

...
...
class Common
    {
        public static SqlConnection GetConnection()
        {
            SqlConnection sqlcon = new SqlConnection();
            string constr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            sqlcon.ConnectionString = constr;
            return sqlcon;
        }
    }
...
...

Then type the below code in Form1.cs 

Right click on Namespace in Solution Explorer, the click add Reference, then add system.configuration,
After that,

using System.Windows.Forms;
using System.Data.SqlClient;
...
...
private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection sqlcon = Common.GetConnection();
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand("Select * from ContactsTable", sqlcon);
            SqlDataAdapter sqlda=new SqlDataAdapter(sqlcmd);
            DataSet ds=new DataSet();
            sqlda.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];       
               
        }
}

then Compile and run...


Reading a Line, Writing a line in C# Console Application

For Reading a line ,
Console.ReadLine();

For Writing a Line,
Console.WriteLine("...tour text...");

Sample Code:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter text to read: ");
            string str = Console.ReadLine();
            Console.WriteLine("Entered Text : ");
            Console.WriteLine(str);
            Console.ReadLine();
        }
    }
}