When I right-click a tree node, it does not become selected. How can I make it be selected on a right-click
Handle the treeview’s mousedown event, and if it is the right-click, then explicitly set focus to th enode under the click. private void treeView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button == MouseButtons.Right) { treeView1.SelectedNode = treeView1.GetNodeAt (e.X ,e.Y ); } }
If I have a button with &Next as the text, the N-accelerator key does not appear until I press Alt. Why
This behavior has nothing to do with .Net and is expected Windows behavior beginning with Win2000. Windows will only show accelerator keys after the alt is pressed. There is a control panel setting that you can use to change this behavior. Look on the Effects tab under Control Panel|Display to see a checkbox labeled ‘Hide keyboard navigation indicators until I use the Alt key’.
How do I prevent resizing of my Control in the designer?
You normally need this support when your Control is parented by another custom Container Control that manages the location and size of your Control. You can prevent resizing by overriding this method in your custom ControlDesigner class: protected override bool EnableDragRect { get { return false; } } Or, for more control over the resizing process: public override /*ControlDesigner*/ SelectionRules SelectionRules { get { System.Windows.Forms.Design.SelectionRules selectionRules; System.Windows.Forms.Control control; selectionRules = base.SelectionRules; control = this.Control; if (control.Parent is MyControlParent) selectionRules = (SelectionRules)(selectionRules & ~(SelectionRules.AllSizeable)); return selectionRules; } }
How can I know when a Control/Component has been selected in the designer surface?
You can listen to the SelectionChanged event in your Control Designer. public class MyContainerDesigner : ParentControlDesigner { public override /*ParentControlDesigner*/ void Initialize(IComponent component) { base.Initialize(component); iSelectionService = (ISelectionService)this.GetService(typeof(ISelectionService)); if (iSelectionService != null) iSelectionService.SelectionChanged += new EventHandler(this.OnSelectionChanged); } private void OnSelectionChanged(object sender, EventArgs e) { // To find out the current selection (can be more than 1) do this: System.ComponentModel.Design.ISelectionService iSelectionService; System.Collections.ICollection selectedComponents; this.iamSelected = false; iSelectionService = (ISelectionService)this.GetService(typeof(ISelectionService)); if (iSelectionService != null) { selectedComponents = iSelectionService.GetSelectedComponents(); foreach(object selectedComponent in selectedComponents) { if(selectedComponent == this.Component) this.iamSelected = true; } } } }
Can I do some custom drawing on my control designer’s surface during design-time?
Yes, you can. You have to override OnPaintAdornments in your Control Designer. This will be called after the Control has painted. A good example is when you have a Control that has its border style set to None, and you want to draw a dummy border in the designer. protected override /*ParentControlDesigner*/ void OnPaintAdornments(PaintEventArgs pe) { System.Windows.Forms.Panel panel; panel = (System.Windows.Forms.Panel)this.Component; if (panel.BorderStyle == BorderStyle.None) this.DrawDesignTimeBorder(pe.Graphics, panel); base.OnPaintAdornments(pe); } public void DrawDesignTimeBorder(Graphics g, Control control) { System.Drawing.Rectangle clientRectangle; System.Drawing.Color bgColor, adjustedBgColor; System.Drawing.Pen pen; clientRectangle = control.ClientRectangle; bgColor = control.BackColor; if (((double) bgColor.GetBrightness()) >= 0.5) adjustedBgColor = ControlPaint.Dark(control.BackColor); else adjustedBgColor = ControlPaint.Light(control.BackColor); pen = new Pen(adjustedBgColor); pen.DashStyle = DashStyle.Dash; clientRectangle.Width = (clientRectangle.Width – 1); clientRectangle.Height = (clientRectangle.Height – 1); g.DrawRectangle(pen,clientRectangle); pen.Dispose(); }