How do I restrict my Container Control to parent only certain types of Controls, and vice-versa, during design-time?
To restrict your Container Control to parent only certain types of controls, override as follows in your designer: public class MyContainerControlDesigner : ParentControlDesigner { public override /*ParentControlDesigner*/ bool CanParent(Control control) { // My Children can only be of type TextBox. return (control is TextBox); } } To restrict your Control to get parented to by a certain type, do so in your Control’s designer: class MyControlDesigner : ControlDesigner { public override /*ControlDesigner*/ bool CanBeParentedTo(IDesigner parentDesigner) { // MyControl can be parent only by MyParent return (parentDesigner is MyParentDesigner); // or do this: // return (parentDesigner.Component is MyParent); } }
I need to encode the LParam argument of a mouse message. How do I do MakeLong , HiWord and LoWord type conversions
You can create static methods that encode and decode these values. Here are some. static int MakeLong(int LoWord, int HiWord) { return (HiWord << 16) | (LoWord & 0xffff); } static IntPtr MakeLParam(int LoWord, int HiWord) { return (IntPtr) ((HiWord << 16) | (LoWord & 0xffff)); } static int HiWord(int Number) { return (Number >> 16) & 0xffff; } static int LoWord(int Number) { return Number & 0xffff; }
How can I insert custom menu items (verbs) into my Component/Control designer’s Context Menu in design time?
You need to create custom ‘Verbs’ for your Component/Control designer. This will make your ‘verbs’ show up in the property browser and in the designer context menu. The designer throws an event when the user selects a verb and you can perform custom operation when you handle this event. You do this by overriding the Verbs property in your Designer class. Here is an example: public class MyControlExtDesigner : ControlDesigner { … public override DesignerVerbCollection Verbs { get { if (this.verbs == null) { this.verbs = new DesignerVerbCollection(); this.verbs.Add(new DesignerVerb(‘Add New Child’,new EventHandler(this.OnAdd))); } return this.verbs; } } private void OnAdd(object sender, EventArgs eevent) { // Code to add a new child inside your control. … } }
How do I set the width of a column in my DataGrid
To set a column width, your datagrid must be using a non-null DataGridTableStyle. Once this is in place, you can set the column width by first getting the tablestyle and then using that object to obtain a column style with which you can set the width. Here are some code snippets showing how you might do this. //…. make sure your DataGrid is using a tablestyle dataGrid1.DataSource = _dataSet.Tables[‘customers’]; DataGridTableStyle dgts = new DataGridTableStyle(); dgts.MappingName = ‘customers’; dataGrid1.TableStyles.Add(dgts); //…… //method to set a column with by colnumber public void SetColWidth(DataGridTableStyle tableStyle, int colNum, int width) { try { tableStyle.GridColumnStyles[colNum].Width = width; tableStyle.DataGrid.Refresh(); } catch{} //empty catch .. do nothing } //…. // here is how you might call this method private void button1_Click(object sender, System.EventArgs e) { DataGridTableStyle tableStyle = dataGrid1.TableStyles[‘customers’]; SetColWidth(tableStyle, 1, 200); }
How can I prevent the Enter key from moving to the next cell when the user is actively editing the cell and presses Enter
Override the method ProcessKeyPreview in your DataGrid. protected override bool ProcessKeyPreview(ref System.Windows.Forms.Message m) { Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode; if((m.Msg == WM_KEYDOWN || m.Msg == WM_KEYUP) && keyCode == Keys.Enter ) return false; return true; }