|
|
|
The General Winforms Interview Questions consists the most frequently
asked questions in Winforms. This list of 100+ questions guage your familiarity
with the Winforms platform. The q&a have been collected over a period
of time from various blogs, forums and other similar Winforms sites
|
44. Design Time Custom Designers
|
| 44.1 I have
created a usercontrol and a designer, How can i debug the designer?
|
| 44.2 How can I
insert custom menu items (verbs) into my Component/Control designer's Context
Menu in design time?
|
| 44.3 How do I
control the state of my custom verb in my designer?
|
| 44.4 How do I
restrict my Container Control to parent only certain types of Controls, and
vice-versa, during design-time?
|
| 44.5 How do I
prevent the Designer-Grid from being drawn on my Container Control during
design-time?
|
| 44.6 How do I
get Mouse Messages on my Control during design-time?
|
| 44.7 Can I do
some custom drawing on my control designer's surface during design-time?
|
| 44.8 How can I
know when a Control/Component has been selected in the designer surface?
|
| 44.9 How do I
prevent resizing of my Control in the designer?
|
44.1 I have created a usercontrol and a designer, How can i debug the designer?
|
|
You need to use a second instance of VS.NET to debug the one that's running the
code.
Put your control on a from in VS.NET Start a 2nd Vs.Net Choose the Debug menu
>> Processes ... Double click "devenv.exe" and choose "Common Language
Runtime" as the types of debugging Open your code file, set your breakpoint,
and you're debugging.
Posted by Shawn Burke of MSFT in
microsoft.public.dotnet.framework.windowsforms.
|
44.2 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.
...
}
}
|
44.3 How do I control the state of my custom verb in my designer?
|
|
Here is a routine that will let you do this. The code below uses the routine
from our previous faq (how to translate a HSB color to RGB color).
|
[C#]
///
/// Adjusts the specified Fore Color's brightness based on the specified back
color and preferred contrast.
///
/// The fore Color to adjust.
/// The back Color for reference.
/// Preferred contrast level.
///
/// This method checks if the current contrast in brightness between the 2
colors is
/// less than the specified contrast level. If so, it brightens or darkens the
fore color appropriately.
///
public static void AdjustForeColorBrightnessForBackColor(ref Color foreColor,
Color backColor, float prefContrastLevel)
{
float fBrightness = foreColor.GetBrightness();
float bBrightness = backColor.GetBrightness();
float curContrast = fBrightness - bBrightness;
float delta = prefContrastLevel - (float)Math.Abs(curContrast);
if((float)Math.Abs(curContrast) < prefContrastLevel)
{
if(bBrightness < 0.5f)
{
fBrightness = bBrightness + prefContrastLevel;
if(fBrightness > 1.0f)
fBrightness = 1.0f;
}
else
{
fBrightness = bBrightness - prefContrastLevel;
if(fBrightness < 0.0f)
fBrightness = 0.0f;
}
float newr, newg, newb;
ConvertHSBToRGB(foreColor.GetHue(), foreColor.GetSaturation(), fBrightness, out
newr, out newg, out newb);
foreColor = Color.FromArgb(foreColor.A, (int)Math.Floor(newr * 255f),
(int)Math.Floor(newg * 255f),
(int)Math.Floor(newb * 255f));
}
}
[VB.Net]
'/ '/ Adjusts the specified Fore Color's brightness based on the specified back
color and preferred contrast.
'/
'/ The fore Color to adjust.
'/ The back Color for reference.
'/ Preferred contrast level.
'/
'/ This method checks if the current contrast in brightness between the 2
colors is
'/ less than the specified contrast level. If so, it brightens or darkens the
fore color appropriately.
'/
Public Shared Sub AdjustForeColorBrightnessForBackColor(ByRef foreColor As
Color, backColor As Color, prefContrastLevel As Single)
Dim fBrightness As Single = foreColor.GetBrightness()
Dim bBrightness As Single = backColor.GetBrightness()
Dim curContrast As Single = fBrightness - bBrightness
Dim delta As Single = prefContrastLevel -
System.Convert.ToSingle(Math.Abs(curContrast))
If System.Convert.ToSingle(Math.Abs(curContrast)) < prefContrastLevel Then
If bBrightness < 0.5F Then
fBrightness = bBrightness + prefContrastLevel
If fBrightness > 1F Then
fBrightness = 1F
End If
Else
fBrightness = bBrightness - prefContrastLevel
If fBrightness < 0F Then
fBrightness = 0F
End If
End If
Dim newr, newg, newb As Single
ConvertHSBToRGB(foreColor.GetHue(), foreColor.GetSaturation(), fBrightness,
newr, newg, newb)
foreColor = Color.FromArgb(foreColor.A, Fix(Math.Floor((newr * 255F))),
Fix(Math.Floor((newg * 255F))), Fix(Math.Floor((newb * 255F))))
End If
End Sub 'AdjustForeColorBrightnessForBackColor
|
44.4 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);
}
}
|
44.5 How do I prevent the Designer-Grid from being drawn on my Container
Control during design-time?
|
|
You can do so my overriding the DrawGrid property in your custom designer:
|
public class MyContainerDesigner : ParentControlDesigner
{
protected override /*ParentControlDesigner*/ bool DrawGrid
{
get
{
if (!this.disableDrawGrid)
return base.DrawGrid;
else
return false;
}
}
}
|
44.6 How do I get Mouse Messages on my Control during design-time?
|
|
The design-time will forward the MouseEnter and MouseLeave messages to your
Control by default. The MouseMove message are blocked by the designer. You can
get MouseDown and Up messages in your Control if you override GetHitTest method
in your designer and return true, as follows:
|
|
protected override /*ControlDesigner*/ bool GetHitTest(Point point)
{
if(this.NeedMouseDown(point))
return true;
else
return false;
}
|
44.7 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();
}
|
44.8 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;
}
}
}
}
|
44.9 How do I prevent resizing of my Control in the designer?
|
|
How do I prevent resizing of my Control in the designer?
|
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;
}
}
|
|