Wednesday, September 30, 2009

Open Outlook with New email - Console Application

Although there are some methodes in System.Web.Mail namespace for sending email , but sometimes customers like to open outlook automatically with a new mail inside and let user to complete the mail before sending, here I write a sample console application to do it,

note: add outlook com reference manually to your project.
 

using System;
using  System.Collections.Generic;
using  System.Text;
using  System.Diagnostics;
using  Outlook = Microsoft.Office.Interop.Outlook;

namespace Openoutlook
{
class Program
{
static void Main(string[] args)
{
Outlook.
Application objOutlk = new Outlook.Application();
object objMail = new object();
Outlook.
MailItem mic = (Outlook.MailItem)(objOutlk.CreateItem(Outlook.OlItemType.olMailItem));
//mic.To = //toTextBox.Text;
//mic.CC = ccTextBox.Text;
//mic.BCC = bccTextBox.Text;
mic.Subject ="Test Subject";// subjectTextBox.Text;
mic.Importance = Outlook.
OlImportance.olImportanceNormal;

// mic.HTMLBody = sb.ToString();
// Adds Attachment to the Mail Message.

// All you need to do is to declare this relative to the number of attachments you have.
mic.Attachments.Add( @"C:\temp\1.txt", Outlook.OlAttachmentType.olByValue, 1, "Attachment Name");
mic.Attachments.Add( @"C:\temp\2.txt", Outlook.OlAttachmentType.olByValue, 1, "Attachment Name");
//Open outlook and display new msg mail

mic.Display(mic);


}
}
}
----------------------------
I did not fill all field such as body , To or CC , you can fill the by your self.

Sunday, September 27, 2009

Adding Row Number to GridView - ASP.NET

GridView is a control which is used in all applications, most of the time showing a Row number in the left side of each row is needed. after adding a gridview on your aspx page then you should add template column , do the following things.
As the picture shows , you should set "Container.DataItemIndex + 1"  to Text property of Label within a asp:TemplateField in Columns .



Clear Form method - Windows Form

Clearing form data is one the methodes that developers sould write over and over, in this case writing a method which can be used in all forms is helpful, via this is a recursive method and depend on control type (textbox or dropdownlist ,...) it clear it's data.

public static void ClearForm(Control parent)
{
foreach (System.Windows.Forms.Control ctrControl in parent.Controls)
{
//Loop through all controls
if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.TextBox)))
{
//Check to see if it's a textbox
((System.Windows.Forms.TextBox)ctrControl).Text = string.Empty;
//If it is then set the text to String.Empty (empty textbox)
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RichTextBox)))
{
//If its a RichTextBox clear the text
((System.Windows.Forms.RichTextBox)ctrControl).Text = string.Empty;
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.ComboBox)))
{
//Next check if it's a dropdown list
((System.Windows.Forms.ComboBox)ctrControl).SelectedIndex = -1;
//If it is then set its SelectedIndex to 0
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.CheckBox)))
{
//Next uncheck all checkboxes
((System.Windows.Forms.CheckBox)ctrControl).Checked = false;
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RadioButton)))
{
//Unselect all RadioButtons
((System.Windows.Forms.RadioButton)ctrControl).Checked = false;
}
if (ctrControl.Controls.Count > 0)
{
//Call itself to get all other controls in other containers
ClearForm(ctrControl);
}
}
}