IT Community - Software Programming, Web Development and Technical Support

Windows Forms Application Interview Tips and Tricks

This is a discussion on Windows Forms Application Interview Tips and Tricks within the Interview Questions & Answers and Tips forums, part of the DiscussWeb IT Curriculum category; Hi Techies, Let's start posting general Windows Forms Application interview questions and answers in this thread. Thanks...


Go Back   IT Community - Software Programming, Web Development and Technical Support > DiscussWeb IT Curriculum > Interview Questions & Answers and Tips

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-21-2008, 11:48 PM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Windows Forms Application Interview Tips and Tricks

Hi Techies,

Let's start posting general Windows Forms Application interview questions and answers in this thread.

Thanks
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-21-2008, 11:49 PM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

How can I run an EXE from within my application?
Use the Process class found in the System.Diagnostics namespace.

[C#]

Process proc = new Process();

proc.StartInfo.FileName = @"Notepad.exe";

proc.StartInfo.Arguments = "";

proc.Start();

[VB.NET]

Dim proc As New Process()

proc.StartInfo.FileName = "Notepad.exe"

proc.StartInfo.Arguments = ""

proc.Start()
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-22-2008, 12:37 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

What are the common issues in redirecting assemblies using the publisher policy files?
1) Make sure to follow proper naming conventions for the policy dll. For example, if the original assembly name is TestAssembly.dll then the corresponding policy assembly should be called "policy.1.0.TestAssembly.dll" to make this redirection work for all "1.0.*" version bindings of the original assembly.
2) While specifying the name for the assembly in the policy file, do not include the ".dll" extension.
This is wrong:
>assemblyIdentity name="TestAssembly.dll" publicKeyToken="f638d0a8d5996dd4" culture="neutral" /<

Instead use:
>assemblyIdentity name="TestAssembly" publicKeyToken="f638d0a8d5996dd4" culture="neutral" /<

3) Make sure to sign the policy assembly with the same strong name as the original.

4) Make sure to distribute the policy file along with the policy assembly. Installing the policy assembly in the GAC alone will not suffice. Note that any change made to the policy file after creating the policy assembly will not take effect.

5) Always use /link (to the policy file) in the "al" command while creating the policy assembly. Do not use /embed. It doesn't seem to be supported.

Some good links:
Welcome to the MSDN Library
http://www.newtelligence.com/downloa...basta2001.aspx
Only For Gurus
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-22-2008, 12:38 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

How can I find all programs with a GUI (not just arbitrary windows) that are running on my local machine?
You could use EnumWindows with p/Invoke, but using the static Process.GetProcesses() found in the System.Diagnostics namespace will avoid the interop overhead.

[C#]
Using System.Diagnostics;
...
foreach ( Process p in Process.GetProcesses(System.Environment.MachineNam e) )

{

if( p.MainWindowHandle != IntPtr.Zero)

{

//this is a GUI app

Console.WriteLine( p ); // string s = p.ToString();

}

}

[VB.NET]

Imports System.Diagnostics

...

Dim p As Process

For Each p In Process.GetProcesses(System.Environment.MachineNam e)

If p.MainWindowHandle <> IntPtr.Zero Then

'this is a GUI app

Console.WriteLine(p) ' string s = p.ToString();

End If

Next p

There is one potential problem on Windows 98. If a process was started with ProcessStartInfo.UseShellExecute set to true, this MainWindowHandle is not available.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-22-2008, 12:39 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

How can I get a list of all processes running on my system?
Use the static Process.GetProcesses() found in the System.Diagnostics namespace.

[C#]
Using System.Diagnostics;
...
foreach ( Process p in Process.GetProcesses() )

Console.WriteLine( p ); // string s = p.ToString();

[VB.NET]
Imports System.Diagnostics
...
Dim p As Process
For Each p In Process.GetProcesses()
Console.WriteLine(p) ' string s = p.ToString()
Next p
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-22-2008, 12:40 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

How can I make sure there is only one instance of my application running?
Saar Carmi shows how to do this in his sample found on C# Corner. In it, he uses the Process class in System.Diagnostics to implement this functionality using code such as
[C#]
public static Process RunningInstance()
{

Process current = Process.GetCurrentProcess();

Process[] processes = Process.GetProcessesByName (current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{

//Ignore the current process

if (process.Id != current.Id)

{

//Make sure that the process is running from the exe file.

if (Assembly.GetExecutingAssembly().Location.Replace( "/", "\\") == current.MainModule.FileName)

{

//Return the other process instance.

return process;

}

}

}

//No other instance was found, return null.

return null;

}

[VB.NET]

Public Shared Function RunningInstance() As Process

Dim current As Process = Process.GetCurrentProcess()

Dim processes As Process() = Process.GetProcessesByName(current.ProcessName)

'Loop through the running processes in with the same name

Dim process As Process

For Each process In processes

'Ignore the current process

If process.Id <> current.Id Then

'Make sure that the process is running from the exe file.

If [Assembly].GetExecutingAssembly().Location.Replace("/", "\") = current.MainModule.FileName Then

'Return the other process instance.

Return process

End If

End If

Next process

'No other instance was found, return null.

Return Nothing

End Function 'RunningInstance
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-22-2008, 12:40 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

How do I determine which operating system is running?

The Environment class in the System namespace has this information.
[C#]
string versionText = Environment.OSVersion.Version.ToString();
[VB.NET]
Dim versionText As String = Environment.OSVersion.Version.ToString()

The Version property has member properties such as Major and Minor that give additional information.

Note that XP is windows version 5.1
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-22-2008, 12:42 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

How can I get all IP addresses for my local machine?
[C#]
string s ="";
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;

for (int i = 0; i < addressList.Length; i ++)

{

s += addressList[i].ToString() + "\n";

}
textBox1.Text = s;
[VB.NET]

Dim s As String = ""

Dim addressList As System.Net.IPAddress() = Dns.GetHostByName(Dns.GetHostName()).AddressList
Dim i As Integer
For i = 0 To addressList.Length - 1

s += addressList(i).ToString() + ControlChars.Lf
Next i

textBox1.Text = s
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 03-22-2008, 12:44 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

My user does not have .NET installed. Will he be able to run my Windows Forms application?

No, the .NET runtime platform has to be on any machine that will run your Windows Forms application. Microsoft has made the .NET runtime platform installation (dotnetredist.exe) available as a free download from Microsoft .NET Framework Redistributable .
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 03-22-2008, 12:45 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

How do I get the path to my running EXE?

The Application class has a static member ExecutablePath that has this information.
[C#]
textBox1.Text = Application.ExecutablePath;
[VB.NET]
TextBox1.Text = Application.ExecutablePath
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 03-22-2008, 12:45 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

How can I programmatically obtain the name of the assembly that the code is executing in ?

The following code snippet demonstrates how you can obtain the name of the assembly that the code is executing in:

[C#]
MessageBox.Show(System.Reflection.Assembly.GetEntr yAssembly().GetName().Name);
[VB.NET]
MessageBox.Show(System.Reflection.Assembly.GetEnTr yAssembly().GetName().Name)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 03-22-2008, 12:46 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

How can I see what is installed in the Global Assembly on a machine?

Use Windows Explorer to view the C:\WINNT\assembly folder. If the .NET Framework is installed, the Windows Explorer will show a custom view of this folder. Use the detailed view to see the details of each assembly.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 03-22-2008, 12:47 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

How do I set the company name that is returned by System.Windows.Forms.Application.CompanyName?

This is an assembly attribute. The VS development environment sets it in the AssemblyInfo.cs (vb) file. If you open that file, you will see a block of assembly attributes that you can set, including company and version numbers.

[assembly: AssemblyCompany("Syncfusion, Inc.")]
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 03-22-2008, 12:48 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

How do you prevent serialization of certain child controls in your Composite Control?

One solution is to use a panel that has a picturebox placed on it with DockStyle.Fill. This will make the picturebox assume the size of the panel. In addition, set the DockPadding.All property to the width of the desired border. Then in the Panel's OnPaint method, call the baseclass and then paint the desired borders.

Here are both VB and C# projects that illustrate how you might go about this. The derived PicturePanel class has properties that allow you to set the bordersize and color as well as the image that is to be displayed. This sample retrieves the image from an embedded
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 03-22-2008, 12:49 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

How do I get hold of the currently focused Control?

The .Net framework libraries does not provide you an API to query for the focused Control. You have to invoke a windows API to do so:

[C#]

public class MyForm : Form

{

[DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Winapi)]

internal static extern IntPtr GetFocus();



private Control GetFocusedControl()

{

Control focusedControl = null;

// To get hold of the focused control:

IntPtr focusedHandle = GetFocus();

if(focusedHandle != IntPtr.Zero)

// Note that if the focused Control is not a .Net control, then this will return null.

focusedControl = Control.FromHandle(focusedHandle);

return focusedControl;

}

}

[VB.Net]

Public Class Form1

' Declare the GetFocused method here:
_

Public Shared Function GetFocus() As IntPtr

End Function

Private Function GetFocusedControl() As Control

Dim focusedControl As Control = Nothing

' To get hold of the focused control:

Dim focusedHandle As IntPtr = GetFocus()

If IntPtr.Zero.Equals(focusedHandle) Then

' Note that if the focused Control is not a .Net control, then this will return null.

focusedControl = Control.FromHandle(focusedHandle)

End If

Return focusedControl

End Function
End Class
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16 (permalink)  
Old 03-22-2008, 12:49 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

Why do calling Focus() on a control not set focus on it?

Note that when you call this method the control should be visible, otherwise the focus will not be set. Hence calling this in say Form_Load on a control in the form will be ineffective. You should instead consider give that control an appropriate TabIndex, so that it will be the first focused control.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17 (permalink)  
Old 03-22-2008, 12:50 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

How do I listen to windows messages in my Control?

In a derived class you should override WndProc as follows (listening to the WM_KEYUP message, for example):
[C#]
public class MyCombo : ComboBox
{
private const int WM_KEYUP = 0x101;
protected override void WndProc(ref System.Windows.Forms.Message m)

{

if(m.Msg == WM_KEYUP)

{

return; //ignore the keyup

}

base.WndProc(ref m);

}

}

[VB.NET]

Public Class MyTextBox

Inherits TextBox

Private WM_KEYUP As Integer = &H101

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

If m.Msg = WM_KEYUP Then

Return 'ignore the keyup

End If

MyBase.WndProc(m)

End Sub 'WndProc

End Class 'MyTextBox
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #18 (permalink)  
Old 03-22-2008, 12:51 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

How do I programatically change the color of a control?

Use the properties BackColor and ForeColor.
button1.BackColor = Color.White;
button1.ForeColor = Color.Blue;
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #19 (permalink)  
Old 03-22-2008, 12:52 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

How can I change the Border color of my control?

Override the OnPaint. Here is some code for a derived Button.
[C#]
public class MyButton : Button

{

protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint(e);

int borderWidth = 1;

Color borderColor = Color.Blue;

ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, borderColor,

borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth,

ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid,

borderColor, borderWidth, ButtonBorderStyle.Solid);

}

}

[VB.NET]

Public Class MyButton

Inherits Button

Protected Overrides Sub OnPaint(e As PaintEventArgs)

MyBase.OnPaint(e)

Dim borderWidth As Integer = 1

Dim borderColor As Color = Color.Blue

ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid)

End Sub 'OnPaint

End Class 'MyButton
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #20 (permalink)  
Old 03-22-2008, 12:52 AM
santhakumar santhakumar is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 928
santhakumar is on a distinguished road
Default Re: Windows Forms Application Interview Tips and Tricks

Why should I provide a Non-Client border to my Control derived class?

Providing a border in the non-client region of your control rather than in the ClientRectangle has very many advantages:

When you include a scrollbar in your control, the scrollboar will appear inside the border, rather than to the outside if you drew the border in the client area.
When you allow custom painting of the control, your user will not draw over the NC border.
Your own client painting code will be simplified in that you will not have to bother about taking the border into account while painting the client area. The next faq will tell you how to include a non-client border.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Tips & Tricks vadivelanvaidyanathan Operating Systems 428 01-09-2009 01:13 AM
Windows Vista Tips & Tricks prasannavigneshr Operating Systems 337 12-30-2008 08:32 PM
ASP.NET 2.0 Interview Tips and Tricks santhakumar