Wednesday, February 25, 2009

Difference between Interface and Abstract Class - C#

1. A class can implement any number of interfaces, while can subclass at most one abstract class.

2. An abstract class can have non-abstract methods, while the methods of an interface are effectively abstract.

3. An abstract class can declare and use variables while an interface can not

4. An abstract class can have methods whose access is public, internal, protected, protected internal or private. Interface members implicitly have public access and no access modifiers(including public) are allowed on interface member declarations.

5. An abstract class can define constructors while interface cannot.

How to use Class Diagram in Visual Studio 2005

1. Solution Explorer, select project and right click to select "View Class Diagram".

2. right click you can create anything you want

3. For relations, need to use left side Toolbox -> Class designer

Tuesday, February 10, 2009

Byte, decimal, hex, string conversion in C#

1. string to byte array:

public static byte[] ToByteArray(string str)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}

2. byte to decimal
int ideciaml = (int)byteIn;

3. byte to char
char cChar = (char)byteIn;

4. byte to hex:
string sChar = string.Format("{0:x} ", byteIn);

5. All to string:

string sChar = (int)byteIn + "";
string sChar = (char)byteIn + "";
string sChar = string.Format("{0:x} ", byteIn);

6. Nonprintable to hex:

byte[] bReceived = ToByteArray(m_comm.ReadExisting());
foreach (byte bb in bReceived)
{
if((((int)bb)>32) && (((int)bb) <127))
txtData.Text += ((char)bb + "");
else
txtData.Text += ("0x" + string.Format("{0:x} ", bb));
}

Friday, February 6, 2009

Toolbar in Compact Framework

Toolbar in Compact Framework

To add a toolbar is easy! From the toolbox to add one, then add an imagelist. Assign this imagelist to the toolbar and individual toolbar button.

The problem is: how to hide/show the toolbar?

private void UpdateToolbarVisibility()
{
if (menuItemToolbar.Checked)
{
foreach (ToolBarButton tbb in toolBar.Buttons)
{
tbb.Visible = false;
}
menuItemToolbar.Checked = false;
}
else
{
foreach (ToolBarButton tbb in toolBar.Buttons)
{
tbb.Visible = true;
}
menuItemToolbar.Checked = true;
}
}

Still the position of toolbar cannot be adjusted? How? Still searching.

Key Hold "Hold CTRL then press ENTER"?

How to detect "Hold CTRL then press ENTER"?

1. KeyDown event:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("e.KeyData:" + e.KeyData.ToString() +
"-e.KeyCode:" + e.KeyCode.ToString() + "-e.Modifiers:" +
e.Modifiers.ToString() + "-e.KeyValue:" + e.KeyValue.ToString());
if (e.KeyData == (Keys.Control | Keys.Enter))
MessageBox.Show("Ctrl+Enter!");
}

2. KeyPress event:

Modifierkeys are translated to characters
inside the KeyPress event. When you hold ctrl while clicking Enter
(char)10 is sent instead of (char)13 and the Control click is suppressed,
so all you have to do to detect Ctrl+Enter is

if(e.KeyChar == (char)10)

The same goes for other combinations like

if(e.KeyChar == (char)97) // [A]

if(e.KeyChar == (char)1 ) // [CTRL]+[A]

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
string s = e.KeyChar.ToString();
MessageBox.Show("e.KeyChar:" + s);
}

Thursday, February 5, 2009

TCHAR and char Manipulation

1. Reset string: memset

char cReaderResponse[300];
memset(cReaderResponse, 0, sizeof(cReaderResponse));


TCHAR tcTmp[300];
memset(tcTmp, 0, sizeof(TCHAR)*300);

2. Copy

strncpy
_tcsncpy

3. Conversion:

MByteToWChar(cReaderResponse, tcTmp,
sizeof(tcTmp)/sizeof(tcTmp[0])); //convert char to TCHAR

-------------------
//-------------------------------------------------------------------------------------
//Description:
// This function maps a character string to a wide-character (Unicode) string
//
//Parameters:
// lpcszStr: [in] Pointer to the character string to be converted
// lpwszStr: [out] Pointer to a buffer that receives the translated string.
// dwSize: [in] Size of the buffer
//
//Return Values:
// TRUE: Succeed
// FALSE: Failed
//
//Example:
// MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW[0]));
//---------------------------------------------------------------------------------------
inline BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize)
{
// Get the required size of the buffer that receives the Unicode
// string.
DWORD dwMinSize;
dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, NULL, 0);

if(dwSize < dwMinSize)
{
return FALSE;
}


// Convert headers from ASCII to Unicode.
MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize);
return TRUE;
}
//-------------------------------------------------------------------------------------
//Description:
// This function maps a wide-character string to a new character string
//
//Parameters:
// lpcwszStr: [in] Pointer to the character string to be converted
// lpszStr: [out] Pointer to a buffer that receives the translated string.
// dwSize: [in] Size of the buffer
//
//Return Values:
// TRUE: Succeed
// FALSE: Failed
//
//Example:
// MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA[0]));
//---------------------------------------------------------------------------------------
inline BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
{
DWORD dwMinSize;
dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);
if(dwSize < dwMinSize)
{
return FALSE;
}
WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE);
return TRUE;
}
/*
使用方法也很简单,示例如下:
wchar_t wText[10] = {L"函数示例"};
char sText[20]= {0};
WCharToMByte(wText,sText,sizeof(sText)/sizeof(sText[0]));
MByteToWChar(sText,wText,sizeof(wText)/sizeof(wText[0]));
*/Good girl
----------------------------