Velz c#
Friday, September 4, 2009
How to send meeting request from c# asp.net
Outlook.Application appln = new Microsoft.Office.Interop.Outlook.Application();
Outlook.AppointmentItem agendaMeeting = (Outlook.AppointmentItem)
appln.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem);
if (agendaMeeting != null)
{
agendaMeeting.MeetingStatus =
Microsoft.Office.Interop.Outlook.OlMeetingStatus.olMeeting;
agendaMeeting.Location = "Conference Room";
agendaMeeting.Subject = "Discussing the Agenda";
agendaMeeting.Body = "Let's discuss the agenda.";
agendaMeeting.Start = new DateTime(2009,09, 04, 13, 0, 0);
agendaMeeting.Duration = 60;
Outlook.Recipient recipient =
agendaMeeting.Recipients.Add("xx@streme.tv");
agendaMeeting.Recipients.Add("xxx.c@streme.tv");
recipient.Type =
(int)Outlook.OlMeetingRecipientType.olRequired;
((Outlook._AppointmentItem)agendaMeeting).Send();
Monday, August 11, 2008
DataGrid, Auto- resize the datagrid based on content
Using the Code
Syntax
Calling this method is very straightforward. It takes only two parameters:
public void SizeColumnsToContent(DataGrid dataGrid,
int nRowsToScan)
- dataGrid—The DataGrid object to be resized
- nRowsToScan—The number of rows to scan when searching for the widest value for a given column. Passing a value of -1 indicates that all rows should be scanned.
SizeColumnsToContent Method
public void SizeColumnsToContent(DataGrid dataGrid,
int nRowsToScan)
{
// Create graphics object for measuring widths.
Graphics Graphics = dataGrid.CreateGraphics();
// Define new table style.
DataGridTableStyle tableStyle = new DataGridTableStyle();
try
{
DataTable dataTable = (DataTable)dataGrid.DataSource;
if (-1 == nRowsToScan)
{
nRowsToScan = dataTable.Rows.Count;
}
else
{
// Can only scan rows if they exist.
nRowsToScan = System.Math.Min(nRowsToScan,
dataTable.Rows.Count);
}
// Clear any existing table styles.
dataGrid.TableStyles.Clear();
// Use mapping name that is defined in the data source.
tableStyle.MappingName = dataTable.TableName;
// Now create the column styles within the table style.
DataGridTextBoxColumn columnStyle;
int iWidth;
for (int iCurrCol = 0;
iCurrCol < dataTable.Columns.Count; iCurrCol++)
{
DataColumn dataColumn = dataTable.Columns[iCurrCol];
columnStyle = new DataGridTextBoxColumn();
columnStyle.TextBox.Enabled = true;
columnStyle.HeaderText = dataColumn.ColumnName;
columnStyle.MappingName = dataColumn.ColumnName;
// Set width to header text width.
iWidth = (int)(Graphics.MeasureString
(columnStyle.HeaderText,
dataGrid.Font).Width);
// Change width, if data width is
// wider than header text width.
// Check the width of the data in the first X rows.
DataRow dataRow;
for (int iRow = 0; iRow < nRowsToScan; iRow++)
{
dataRow = dataTable.Rows[iRow];
if (null != dataRow[dataColumn.ColumnName])
{
int iColWidth = (int)(Graphics.MeasureString
(dataRow.ItemArray[iCurrCol].ToString(),
dataGrid.Font).Width);
iWidth = (int)System.Math.Max(iWidth, iColWidth);
}
}
columnStyle.Width = iWidth + 4;
// Add the new column style to the table style.
tableStyle.GridColumnStyles.Add(columnStyle);
}
// Add the new table style to the data grid.
dataGrid.TableStyles.Add(tableStyle);
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
Graphics.Dispose();
}
}
Thursday, July 31, 2008
Interface - An Analysis Part II
It's really easy to implement one or more interfaces, and technically because it is not a class, multiple interfaces can be implemented at once. An interface is implemented as if it was a class:
class AClass : Class, ISomething, IInterface, IMoreInterface { ..members }
Interfaces provided by Microsoft There are hundreds of interfaces that shipped with the .NET Framework SDK. Those interfaces are essential within the framework as they hold's the framework's structure and ensure compatibility between objects. They also allow 3rd party additions of objects to work flawlessly with Microsoft's prewritten library of objects.
IEnumerator and IEnumerable are the two most commonly used interfaces and we will discuss them in detail now.
These Interfaces reside in the System.Collections namespace.
Firstly let's have a look at each interface we are dealing with here. IEnumerator is an interface that has following members:Property: CurrentMethod: MoveNextMethod: ResetMSDN describes the IEnumerator class as "Supports a simple iteration over a collection.", as well as:
"IEnumerator is the base interface for all enumerators. Enumerators only allow reading the data in the collection. Enumerators cannot be used to modify the underlying collection.
Initially, the enumerator is positioned before the first element in the collection. Reset also brings the enumerator back to this position. At this position, calling Current throws an exception. Therefore, you must call MoveNext to advance the enumerator to the first element of the collection before reading the value of Current."
IEnumerable is an interface that has 1 method called GetEnumerator. The MSDN documentation describes the GetEnumerator method as "Exposes the enumerator, which supports a simple iteration over a collection."... It then goes on to say "IEnumerable must be implemented to support the ForEach semantics of Microsoft Visual Basic. COM classes that allow enumerators also implement this interface".
As you can see, MSDN plays an important role in .NET development by providing the developers with correct information. In this case, MSDN provides what each method or property in the interface should do when implemented by a class.
So now that we know about the interfaces we are dealing with, let's start making some collection objects. What we are going to make today is a collection called Customers, which will contain Customer objects. I chose this because this collection seemed to be used a lot.
First, we need to make a Customer class:
class Customer { public Customer(int customerID) { this.customerID=customerID; } private int customerID; public int CustomerID { get { return customerID; } set { customerID =value; } } private string name; public string Name { get { return name; } set { name =value; } } }
This is a fairly simple class, and contains just two properties. One returns the customers name, and the other returns the customers id. Now, we want to make a Customer collection class that is enumerable but at the same time has methods to sort customers by different fields.
On top of the methods implemented by the interfaces, it will also have a method called SortByName(), which will sort the items in the collection by the field 'name' and reset the collection.
There will be another method called SortByID(), which will sort the items in the collection by the field 'customerID' and then reset the collection.
Here's the code for this collection class:
class Customers : System.Collections.IEnumerable, System.Collections.IEnumerator { System.Collections.ArrayList customerIDs; System.Collections.IEnumerator ienum;
public void SortByName() { // Get all customerIDs from the database sorted by name
this.Reset(); } public void SortByID() { // Get all customerIDs from the database sorted by customerID
this.Reset(); } public Customers() { // Get all customerIDs from the database and is put in to an ArrayList, // customerIDs
// Gets ienumerator interface for customerIDs arraylist ienum = customerIDs.GetEnumerator(); } public System.Collections.IEnumerator GetEnumerator() { // Polymorph this object into an IEnumerator interface return (System.Collections.IEnumerator)this; } public void Reset() { ienum.Reset(); } public bool MoveNext() { return ienum.MoveNext(); } public object Current { get { return new Customer(Convert.ToInt32(ienum.Current)); } } }
Basically, the MoveNext and Reset methods are the MoveNext and Reset methods of the ArrayList that we've created which behind the scenes polymorphed into an IEnumerator interface and returned when GetEnumerator is called.
The Current property just returns a new Customer object, instantiated when it is called with the current object of the customerIDs ArrayList.
Interface - An Analysis Part I
Inheritance in OOP achieves two significant things. One is code reusability and second is the relationships between objects. By extending, not rewriting, the class written before, the amount of code written by developers is reduced considerably. Also, by establishing the relationship between objects, many things become possible in programming, for example, it is possible to polymorph one object into another.
Interface affects both the reusability and the relationship between objects.
What is an interface?
"An interface is a collection of method definitions (without implementations) and constant values".
Its purpose is to captures similarities between unrelated classes without forcing a class relationship, and to do this, an interface operates like a contract in the form of a collection of methods and constant declarations. When a class implements an interface, it promises to implement all of the methods declared in that interface.
A class can inherit multiple interfaces at once, and information on what interfaces are inherited by what class can be accessed by the framework and other objects. Hence, an object can identify an object that inherited a specified interface.
This information can be used by a particular framework or another object to determine whether the object is compatible with different objects or statements.
IEnumerable and IEnumerator are the best interface examples there are for you to understand the role of interface. Classes implementing these two interfaces are classified as an enumerable collection. This means that class behaves like any other collection object written by Microsoft, such as an ArrayList.
There are many features that define an enumerable collection, and by implementing these two interfaces, the framework guarantees that this object has these features. For example, an enumerable collection must have a method called MoveNext, which moves the cursor within the collection one step forward. By implementing the IEnumerator interface, the class is promising (and is required) to implement this method.
As mentioned above, when a class implements these interfaces and is defined as an enumerable collection, it behaves like one as well. This means that it is capable of being iterated using a foreach statement, just like we can do with an ArrayList object.
Why do we use interfaces?
We use interfaces because they allow reusability of code as well as help create useful relationships between objects. Because your object is now able to work with other objects, you no longer have to other dependent objects that are only compatible with your objects. For example, if you are to create a new connection object like a mySql Connection object, then by implementing the right connection interfaces it will be able to work with the existing data adapter object and a data set object. If interfaces are not implemented, data adapter and data set objects won't be able to see if your object is compatible and that it implements the required methods, hence it won't be compatible with the data adapter nor data set objects, forcing you to write custom objects that implement the same functionality as the data adapter and data set objects. Here, we are creating relationships between unknown objects.
Creating interfaces
An interface is very easy to create. It's often the planning of how you can use interfaces in your application that takes time, not the actual creation of it, as it implements no logic.
Here's an example of a simple interface:
interface ISomething
{
// Read only property
string ReadOnlyProperty{get;}
// Property int Property{get;set;}
// Method void Method();
}
As you can see, an interface is created using the interface keyword. One thing to note here is that Microsoft STRONGLY recommends that ALL interfaces begin with the character 'I'. So in the example above, I've named my interface ISomething.
The interface shown above implements 1 read only property, 1 normal (read and write) property, and finally, 1 method that returns void. If a class is to implement this interface, then it must implement those 2 properties and 1 method to successfully compile.
To implement these members into a class, you must have the same name and signature for the members in the class where it will be implemented. So, for
string ReadOnlyProperty
{
get;
}
your class's property should look something like
string ReadOnlyProperty { get { ..code to return string } }
for
int Property{get;set;}
it should look like this:
int Property { get { .. code to return int } set { ..code to set Property value… } }
and finally, for void Method();
it should look something like this: void Method() { ..code }
You can also have methods that take one or more parameters. This is done like this: void Method2(string, int); The method shown above can be implemented by a class like this: void Method2(string astring, in aint) { ..code }
Sunday, July 27, 2008
Clear the console window
using System.Runtime.InteropServices;
namespace DataStructures
{
public class ClearConsole
{
private const int STD_OUTPUT_HANDLE = -11;
private const byte EMPTY = 32;
[StructLayout(LayoutKind.Sequential)]
struct COORD
{
public short x;
public short y;
}
[StructLayout(LayoutKind.Sequential)]
struct SMALL_RECT
{
public short Left;
public short Top;
public short Right;
public short Bottom;
}
[StructLayout(LayoutKind.Sequential)]
struct CONSOLE_SCREEN_BUFFER_INFO
{
public COORD dwSize;
public COORD dwCursorPosition;
public int wAttributes;
public SMALL_RECT srWindow;
public COORD dwMaximumWindowSize;
}
[DllImport("kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll", EntryPoint = "FillConsoleOutputCharacter", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int FillConsoleOutputCharacter(int hConsoleOutput, byte cCharacter, int nLength, COORD dwWriteCoord, ref int lpNumberOfCharsWritten);
[DllImport("kernel32.dll", EntryPoint = "GetConsoleScreenBufferInfo", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
[DllImport("kernel32.dll", EntryPoint = "SetConsoleCursorPosition", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int SetConsoleCursorPosition(int hConsoleOutput, COORD dwCursorPosition);
private int hConsoleHandle;
public ClearConsole()
{
//
// TODO: Add constructor logic here.
//
hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
}
public void Clear()
{
int hWrittenChars = 0;
CONSOLE_SCREEN_BUFFER_INFO strConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO();
COORD Home;
Home.x = Home.y = 0;
GetConsoleScreenBufferInfo(hConsoleHandle, ref strConsoleInfo);
FillConsoleOutputCharacter(hConsoleHandle, EMPTY, strConsoleInfo.dwSize.x * strConsoleInfo.dwSize.y, Home, ref hWrittenChars);
SetConsoleCursorPosition(hConsoleHandle, Home);
}
}
}