Winform interview Questions Page 5
Page 1Page 2Page 3Page 4Page 5

Why does adding images to an ImageList in the designer cause them to lose their alpha channel?

Looks like the ImageList editor loses the transparency when it does some internal copy/clone of the images. However, it seems that it does work when you add the images in code to the ImageList.

One workaround (not so tidy) is to add the images to the ImageList in the design time (so that your design-time will be closer to the runtime) and then clear that ImageList and refill it with the images again, in code.

Take a look at this faq on how to add images to your project and retrive them programatically during runtime. Adding image files to a project as an embedded resource and retrieving them programatically.

Why do the XP Icons when drawn using Graphics.DrawImage not draw transparently? 

Note that it's only the ImageList class that can draw an Icon with alpha channel properly. So, instead of using the Graphics.DrawImage method, first associate this icon to a ImageList and then use the ImageList.Draw method to draw the icon. Also, note that this is possible only in XP with XP Themes enabled for that application.

Why do the XP Icons that have alpha channel not draw properly when associated with controls like ListView?

Make sure that you include the manifest file that will enable XP themes support for you application. Then the icons with alpha channel will draw semi-transparently.

 How do I prevent resizing of my Controls by the user, via Docking or anchoring or manual resizing during design-time?

The best place to ensure a particular height/width for you control is in the SetBoundsCore override of your Control, as follows:

protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
int prefHeight = this.GetPreferredHeight();
// Ensure that the height is atleast as big as prefHeight

if(height < prefHeight)
height = prefHeight;
base.SetBoundsCore(x, y, width, height, specified);
Protected Overrides Sub SetBoundsCore(ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal specified As BoundsSpecified)

Dim prefHeight As Integer = Me.GetPreferredHeight()
' Ensure that the height is atleast as big as prefHeight
If height < prefHeight Then
height = prefHeight
End If

MyBase.SetBoundsCore(x, y, width, height, specified)
End Sub

What control do I use to insert Separator lines between Controls in my Dialog?
Use the Label Control with the BorderStyle set to Fixed3D and height set to 2.

2.22 How can I make my controls transparent so the form's background image can show through them?

By default, a control's background color will be the same as the container's background. In the picture below, the form's background was set to Color.Red. In design mode, you can see the four controls have the red background, but since the form's Image was set to a bitmap, you cannot see the form's red background. To make the controls' background transparent, you can set the alpha blend value of their background to zero. In code, you could use:
public Form1()
checkBox1.BackColor = Color.FromArgb(0, checkBox1.BackColor);

button1.BackColor = Color.FromArgb(0, button1.BackColor);
linkLabel1.BackColor = Color.FromArgb(0, linkLabel1.BackColor);
label1.BackColor = Color.FromArgb(0, label1.BackColor);
// Or use the System.Drawing.Color.Transparent color.

In design mode, you can set the alpha blend value to zero by typing the four component in the property grid. So, for each control's BackColor property, you would type 0,255,0,0 to make it a tranparent red.

Here is a VB project using this idea.

2.23 How do I create an editable listbox with an in-place TextBox and Button?

The attached EditableList UserControl implements an editable listbox with an in-place TextBox and Button allowing users to directly edit the contents of the list box.

When the user clicks on a selected item, a textbox and a button is shown over the selected item and the user can directly edit the selectected item text. The button can be programmed to show for example a OpenFileDialog to allow user to select a file (useful while implementing a Files list).

2.24 How do I determine the width/height of the Non-Client area (like the border in a textbox) of a Control?

One generic way for all Controls is to get the difference between the ClientRectangle's width and the Control.Bounds' width. That should give the border width (and in general the NC area width); similarly for height.

2.25 How can I programmatically manipulate Anchor styles?

You can do this using the bitwise operators &, | and ^ ( And, Or and Xor (or &, Or, ^) in VB.Net). Here is code that will toggle label1 being anchored on the left.


private void button1_Click(object sender, System.EventArgs e)


if ((label1.Anchor & AnchorStyles.Left) == 0)

{ //add it

label1.Anchor = label1.Anchor | AnchorStyles.Left;


else if ((label1.Anchor & AnchorStyles.Left) != 0)

{ //remove

label1.Anchor = label1.Anchor ^ AnchorStyles.Left;




Private Sub button1_Click(sender As Object, e As System.EventArgs)

If(label1.Anchor And AnchorStyles.Left) = 0 Then

'add it

label1.Anchor = label1.Anchor Or AnchorStyles.Left

ElseIf(label1.Anchor And AnchorStyles.Left) <> 0 Then


label1.Anchor = label1.Anchor Xor AnchorStyles.Left

End If

End Sub 'button1_Click

2.26 What is the best method to override in custom Controls to perform custom initialization during runtime?

When custom initialization is to be done during runtime on certain Controls, the best way is to implement the ISupportInitialize interface in that Control. Then the BeginInit method will be called as soon as the Control gets created and EndInit will be called after the design-time initialization of that control.

2.27 I set a Control's Visible property to true and in the immediate next statement, it returns false. Why doesn't setting the Visible property 'take'?

A control's Visible property will also depend on it's parent control's (if any) visible property. If the parent control is not visible, the control will also return false.

2.28 I'm trying to make the background of my linklabel transparent so a picturebox will show through it. However, if I set the link label's BackColor property to Transparent the label still has a white background. Why?

Controls with a "Transparent" color actually render their parent's background, so you're seeing the White background of the Form, not the PictureBox. Three easy ways to deal with this:

* Use a Panel with it's "BackgroundImage" property set instead of a PictureBox, and parent the LinkLabels to the panel (PictureBoxes generally don't have children)
* Set the BackgroundImage of the Form to the image (basically the same as 1 above, but avoids the extra control)
* In code, set the Parent of the LinkLabel to be the PictureBox. You'll need to update the LinkLabel's position to match the new origin of the parent if the PictureBox isn't at (0,0)

(Shawn Burke on microsoft.public.dotnet.framework.windowsforms newsgroup)

2.29 How do I dynamically load a control from a DLL?

You use System Reflection to dynamically load a control. If the DLL is named "SpecControls.DLL" and the class you want is "SpecControls.ColorControl", then use this code.


// load the assembly

System.Reflection.Assembly assembly = Assembly.LoadFrom("SpecControls.DLL");

// get the type

Type t = assembly.GetType("MyControls.MyControl");

// create an instance and add it.


Control c = (Control)Activator.CreateInstance(t);



' load the assembly

Dim assembly1 As System.Reflection.Assembly = Assembly.LoadFrom("SpecControls.DLL")

' get the type

Dim t As Type = assembly1.GetType("MyControls.MyControl")

' create an instance and add it.


Dim c As Control = CType(Activator.CreateInstance(t), Control)


2.30 What is the (DynamicProperties) item listed on a control's property page in VS.NET?

Clicking Advanced... under this DynamicProperties option in the control properties displays certain control properties that can be set through an XML app.config file that is added to your project. This file stores the dynamic property values and is automatically read during the intialization process on the form at design time. You can manually edit this file to change the properties and next when you run the application, controls will pick up the new values. This file is used strictly for design time initializations.

2.31 How can I make a Panel or Label semi-transparent on a Windows Form?

You can make a panel or label transparent by specifying the alpha value for the Background color.

panel.BackColor = Color.FromArgb(65, 204, 212, 230);

In the designer you have to enter these values manually in the edit box. Don't select the color using the ColorPicker.

2.32 How can I add a control to a Window Form at runtime?

To add a control at runtime, you do three steps:

* 1. Create the control
* 2. Set control properties
* 3. Add the control to the Form's Controls collection

In general, if you need help on exactly what code you need to add, just look at the code generated by the designer when you add the control at design time. You can generally use the same code at runtime.

Here are code snippets that create a textBox at runtime.


//step 1

TextBox tb = new TextBox();


tb.Location = new Point( 10, 10);

tb.Size = new Size(100, 20);

tb.Text = "I was created at runtime";




'step 1

Dim tb as TextBox = New TextBox()


tb.Location = New Point( 10, 10)

tb.Size = New Size(100, 20)

tb.Text = "I was created at runtime"



2.33 How do I make the arrow keys be accepted by a control (such as a button) and not handled automatically by the framework's focus management?

By default, the arrow keys are not handled by a control's key processing code, but instead are filtered out for focus management. Hence, the control's KeyDown, KeyUp and KeyPressed events are not hit when you press an arrow. If you want your control to handle these keyboard events, you tell the framework by overriding your control's IsInputKey method.

protected override bool IsInputKey(Keys key)




case Keys.Up:

case Keys.Down:

case Keys.Right:

case Keys.Left:

return true;


return base.IsInputKey(key);


2.34 In the property browser for a custom control, how do I disable a property initially, but enable it later based on some other property changing?

Implement ICustomTypeDescriptor, and provide your own PropertyDescriptor for that property that changes it's return value for IsReadOnly.

2.35 How can I have the control designer create the custom editor by calling the constructor with the additional parameters rather than the default constructor?

You can do this by creating the editor yourself rather than allowing TypeDescriptor to do it:

1) Shadow the property you care about in your designer...

protected override void PreFilterProperties(IDictionaryProperties props)


PropertyDescriptor basePD = props["MyProperty"];

props["MyProperty"] = new EditorPropertyDescriptor(basePD);


2) Create a property descriptor that "wraps" the original descriptor

private class EditorPropertyDescriptor : PropertyDescriptor


private PropertyDescriptor basePD;

public EditorPropertyDescriptor(PropertyDescriptor base)


this.basePD = base;


// now, for each property and method, just delegate to the base...

public override TypeConverter Converter


get { return basePD.Converter; }


public override bool CanResetValue(object comp)


return basePD.CanResetValue(comp);

// and finally, implement GetEditor to create your special one...

3) create your editor by hand when it's asked for

public override object GetEditor(Type editorBaseType)


if (editorBaseType == typeof(UITypeEditor))

return new MyEditor(Param1, param2, param3);


return basePD.GetEditor(editorBaseType);



(from on microsoft.public.dotnet.framework.windowsforms)

2.36 How do I listen to the screen resolution change in my control?

You should override WndProc in your control/form and listen to the WM_DISPLAYCHANGE message. Refer to this faq a WndProc override sample:

How do I listen to windows messages in my Control?

2.37 How do I determine which button in a Toolbar is clicked?

When you click on a button on a Toolbar the click is sent to the Toolbar, so you need to check the ToolBarButtonClickEventArgs's button property to determine the button on the Toolbar that was clicked.

private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)

//check if toolBarButton1 was clicked
if (e.Button == toolBarButton1)

MessageBox.Show("Button 1 clicked");


Private Sub toolBar1_ButtonClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs)

'check if toolBarButton1 was clicked

If e.Button = toolBarButton1 Then
MessageBox.Show("Button 1 clicked")

End If
End Sub