Nothing is everything.

Wednesday, December 30, 2009

RichTextBox drag and drop

http://msdn.microsoft.com/en-us/library/aa984395(VS.71).aspx

Event Recursion Problem

We often meet some issues that an event will be fired recursively and its handlers will also be called recursively. Below is a sample which shows a AfterCheck event handler of the TreeView in similar circumstances:

void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Node.Nodes.Count > 0)
{
foreach (TreeNode subNode in e.Node.Nodes)
{
//This property setting will firing the AfterCheck event again.
subNode.Checked = true;
}
}
}


To control the event firing and handler calling, we need to create our own TreeView control and override the OnAfterCheck method to control the event firing. There are four requirements below:



1. The event should be fired recursively and its handlers can also be called recursively.

        In this case, we need to do nothing.



2. The event should be fired only once and its handlers can only be called once as well.

       



public class MyTreeView : TreeView
{
//Indicate if the event should be fired.
private bool IsFire = true;
protected override void OnAfterCheck(TreeViewEventArgs e)
{
if (IsFire)
{
IsFire = false;

//Your Code in the old AfterCheck event handler.

IsFire = true;
base.OnAfterCheck(e);
}
}
}


3. The event should be fired recursively but the handler can only be called once.


public class MyTreeView : TreeView
{
//Indicate if the event should be fired.
private bool IsFire = true;
protected override void OnAfterCheck(TreeViewEventArgs e)
{
if (IsFire)
{
IsFire = false;

//Your Code in the old AfterCheck event handler.

IsFire = true;
base.OnAfterCheck(e);
}
else
{
base.OnAfterCheck(e);
}
}
}


4. The handler should be called recursively but the event can only be fired once.


public class MyTreeView : TreeView
{
//Indicate if the event should be fired.
private bool IsFire = true;
protected override void OnAfterCheck(TreeViewEventArgs e)
{
if (IsFire)
{
IsFire = false;

//Your Code in the old AfterCheck event handler.

IsFire = true;
base.OnAfterCheck(e);
}
else
{
//Your Code in the old AfterCheck event handler.
}
}
}


Please feel free to tell me if I made some mistakes. Thanks.

Remove all event handlers

http://bytes.com/topic/c-sharp/answers/274921-removing-all-event-handlers

Using wpf control in windows forms

http://msdn.microsoft.com/en-us/library/ms742215.aspx

Thursday, December 17, 2009

Bind a BindingSource to a Type

When we set the DataSource property of the BindingSource class to a Type instance, it will automatically create a empty IBindingList and bind it to the BindingSource. This will result in an allowance to call the AddNew method of the BindingSource to add a new object of the corresponding type. If the type implements IList, the object type will be T, not IList.

By the way, in this case, the type of the inner data source is System.RuntimeType. This is an introduction:
http://doogalbellend.blogspot.com/2006/06/what-is-systemruntimetype.html

This is the details: http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.datasource.aspx

Visual Studio Shortkeys

http://msdn.microsoft.com/en-us/library/da5kh0wa(VS.80).aspx

WebBrowser Control: Prevent new window opened in IE

The link below shows the details:
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/2ff26743-b0b7-4377-a01b-3f128d32d9dd?prof=required

Wednesday, December 16, 2009

How to modify the app.config programmatically.

The code snippet below shows how add a connection string during runtime:
// Open App.Config of executable
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.
config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings("myConnectionString1", @"Data Source=MyPC\SQLEXPRESS;Initial Catalog=MyDataBase2;User ID=MyUserName"));
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");

The link below contains the details:
http://geekswithblogs.net/akraus1/articles/64871.aspx

How to modify the app.config during runtime.

This is the code:

// Open App.Config of executable
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Add an Application Setting.
config.AppSettings.Settings.Add("Modification Date",
DateTime.Now.ToLongTimeString() + " ");

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);

// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");



The link below shows the details:

How to set StartUri in WPF

We can easily set the start window of a WPF application by setting the StartupUri property of the Application node in the App.xaml to the window xaml file name, such as "Window1.xaml". But if the Window1.xmal is placed in another folder, what can we do?

We only need to add the directory path before the window xaml file name, such as "Palyer/MyPlayerWindow.xaml" or "Palyer\MyPlayerWindow.xaml".

How to convert a char to a digit efficiently

We often need to allow the user to input a digit(0~9). We can call getch() method to get the char, because it is fast. But when we convert it to a digit, it becomes a little complicated, since we need to build a string and call atoi.

There is a simple and efficient method to achieve this:
int digit = getchar() & 0xF.

These are the binary formats of the digits:
0: 11 0000
1: 11 0001
...

0xF: 00 1111

So this calculation will remove the high bits and only leave the lower four bits, these bits formed a new integer, which is just the digit we need.

Tuesday, December 15, 2009

How to call C++ function in C#

#include "stdafx.h"
#include "QueryAPI.h"

extern "C"
{
__declspec(dllexport) unsigned long QueryUser(const wchar_t* AccountName,
wchar_t* FullUserName,
wchar_t* EmailAddress,
double* Salary,
wchar_t* CardId,
int* Old,
bool* Single)
{
cout<<*AccountName; char* name = "微软" ; ::memcpy((void*)FullUserName,name,100); char* Email = "guochao6398@126.com";
::memcpy((void*)EmailAddress,Email,100);
char* CardID = "12345678";
::memcpy((void*)CardId,CardID,20);
*Salary = 200.34;
//*CardId = "123456789";
*Old = 2;
*Single = false;
return 0;
}
}

Followers