Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ctrl-C copies entire DataGrid cell text instead of highlighted text

Status
Not open for further replies.

dalchri

Programmer
Apr 19, 2002
608
US
I've noticed a peculiar bug in the DataGridTextBoxColumn. If you use ctrl-c to copy text out of a cell, the entire cell rather than the highlighted text is copied.

Also, if you change the text of the cell and then copy it, you'll notice that the original text of the cell is what it copied, not the current text of the cell.

This problem does not occur with ctrl-x. Only the highlighted text is cut and pasted.

This problem does not occur if you right click on the highlighted text and select copy from the context menu.

It almost seems like the DataGrid control rather than the DataGridTextBox control is receiving the 0x301 windows message in WndProc.

Does anyone have an idea on how to solve this bug? I realize that I will most likely have to the inherit the DataGrid or DataGridTextBox classes to reroute the windows messages but I don't even really know what I am looking for or were to look for it.

Would the mistake manifest itself in ProcessMessage, PreProcessMessage, ProcessKeyPreview, WndProc... ?

How do I determine which control is receiving the 0x301 message and reacting to it by placing the text on the windows clipboard?

Thank you for any suggestions!
 
This solution works:

Code:
class DataGrid2 : System.Windows.Forms.DataGrid {
	protected override bool ProcessDialogKey(Keys key) {
		//The DataGrid has its own implementation of copy functionality for the ctrl-c shortcut
		//key sequence.  However, that implementation overrides the DataGridColumnStyle controls
		//because this method returns true.  The DataGrid should continue its implementation for
		//situations where there is no DataGridColumnStyle control in edit mode but return false
		//so that the DataGridSolumnStyle control can override the DataGrid with its implementation
		//of copy functionality.
		return 
			base.ProcessDialogKey(key) && 
			!((key & Keys.KeyCode) == Keys.C && 
				(key & Keys.Control) == Keys.Control && 
				(key & Keys.Alt) != Keys.Alt);
		//The Ctrl and C keys must be pressed but the Alt key cannot be pressed
		//This is the exact logic used by the DataGrid to trigger its copy implementation
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top