|
|
|
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
|
11.Windows Forms from MFC
|
| 11.1 Where can I find
a succinct discussion of Windows Forms?
|
| 11.2 When I add a
destructor to a class, why isn't it hit when my instantiated object goes out of
scope?
|
| 11.3 How do I convert
a string to a double or int? What plays the role of atof and atoi in C#?
|
| 11.4 What class or
method do I use to retrieve system metrics like the width of a scrollbar?
|
| 11.5 In C++, the code
"MyClass ht;" creates an object ht on the stack frame. But in C#, this same
code compiles, but gives a runtime error. Why?
|
| 11.6 I need to encode
the LParam argument of a mouse message. How do I do MakeLong , HiWord and
LoWord type conversions?
|
| 11.7 How do you clear
the contents of a list box?
|
11.1 Where can I find a succinct discussion of Windows Forms?
|
|
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.
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 resource. It also uses double buffering to
minimize flashing as you resize the control.
|
11.2 When I add a destructor to a class, why isn't it hit when my instantiated
object goes out of scope?
|
|
Your managed code doesn't have a destructor, just a finalizer. The difference is
a destructor (as in C++) fires immediately when an object goes out of scope,
but a finalizer is run when the CLR's garbage collector (GC) gets to your
object. It's not deterministic when this will occur, but it's very unlikely to
occur right after the object goes out of scope. It will happen at a later time,
however.
|
11.3 How do I convert a string to a double or int? What plays the role of atof
and atoi in C#?
|
|
You use static members of the Convert class found in the System namespace to
handle conversions in the .NET framework.
|
string s = "45";
int h = Convert.ToInt32(s);
double d = Convert.ToDouble(s);
|
|
11.4 What class or method do I use to retrieve system metrics like the width of
a scrollbar?
|
|
In the .NET framework, you use the SystemInformation class from the
System.Windows.Forms namespace. This class has comparable information to what
GetSystemMetrics() returned in VC6. There is also a Screen class that contains
additions display device properties.
|
11.5 In C++, the code "MyClass ht;" creates an object ht on the stack frame.
But in C#, this same code compiles, but gives a runtime error. Why?
|
|
MyClass ht; does not create any object. Instead, it creates a variable (a
reference) of type MyClass, and sets its value to null. No object is created.
To create an object, you need to explicitly call the class constructor with a
new.
|
|
MyClass ht = new MyClass();
|
|
You can also use ht to refer to an instance of MyClass that has been created
(with a new) at some other point in your code.
|
MyClass myClass = new MyClass();
......
MyClass ht;
......
ht = myClass; // both ht and myClass are references to the same object...
|
11.6 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;
}
|
11.7 How do you clear the contents of a list box?
|
|
You have to access the Items of the ListBox using the Items property and clear
(or otherwise operate on the items) these.
this.lb1.Items.Clear();
|
|
class NativeMethods
{
[DllImport("user32.dll", CharSet=CharSet.Auto)]
extern public static IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam,
IntPtr lParam) ;
}
// Send a WM_MOUSELEAVE manually to a window.
NativeMethods.SendMessage(control.Handle, NativeMethods.WM_MOUSELEAVE,
IntPtr.Zero, IntPtr.Zero);
|
|