EzDevInfo.com

winforms interview questions

Top winforms frequently asked interview questions

When setting a form's opacity should I use a decimal or double?

I want to use a track-bar to change a form's opacity.

This is my code:

decimal trans = trackBar1.Value / 5000;
this.Opacity = trans;

When I try to build it, I get this error:

Cannot implicitly convert type 'decimal' to 'double'.

I tried making trans a double, but then the control doesn't work. This code has worked fine for me in VB.NET in the past.


Source: (StackOverflow)

Implementing INotifyPropertyChanged - does a better way exist?

Microsoft should have implemented something snappy for INotifyPropertyChanged, like in the automatic properties, just specify {get; set; notify;} I think it makes a lot of sense to do it. Or are there any complications to do it?

Can we ourselves implement something like 'notify' in our properties. Is there a graceful solution for implementing INotifyPropertyChanged in your class or the only way to do it is by raising the PropertyChanged event in each property.

If not can we write something to auto-generate the piece of code to raise PropertyChanged event?


Source: (StackOverflow)

Advertisements

How do I make a textbox that only accepts numbers?

I have a windows forms app with a textbox control that I want to only accept integer values. In the past I've done this kind of validation by overloading the KeyPress event and just removing characters which didn't fit the specification. I've looked at the MaskedTextBox control but I'd like a more general solution that could work with perhaps a regular expression, or depend on the values of other controls.

Ideally this would behave such that pressing a non numeric character would either produce no result or immediately provide the user with feedback about the invalid character.


Source: (StackOverflow)

Best way to implement keyboard shortcuts in a Windows Forms application?

I'm looking for a best way to implement common Windows keyboard shortcuts (for example Ctrl+F, Ctrl+N) in my Windows Forms application in C#.

The application has a main form which hosts many child forms (one at a time). When a user hits Ctrl+F, I'd like to show a custom search form. The search form would depend on the current open child form in the application.

I was thinking of using something like this in the *ChildForm_KeyDown* event:

   if (e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control)
        // Show search form

But this doesn't work. The event doesn't even fire when you press a key. What is the solution?


Source: (StackOverflow)

How do I group Windows Form radio buttons?

How can I group the radio buttons in Windows Form application (a lot like ASP.NET's radiobuttonlist!)?

So I can switch between each case chosen from the options.


Source: (StackOverflow)

How do I automatically scroll to the bottom of a multiline text box?

I have a textbox with the .Multiline property set to true. At regular intervals, I am adding new lines of text to it. I would like the textbox to automatically scroll to the bottom-most entry (the newest one) whenever a new line is added. How do I accomplish this?


Source: (StackOverflow)

How to detect the currently pressed key?

In Windows Forms, you can know, at any time, the current position of the cursor thanks to the Cursors class.

The same thing doesn't seem to be available for the keyboard. Is it possible to know if, for example, the Shift key is pressed?

Is it absolutely necessary to track down every keyboard notification (KeyDown and KeyUp events)?


Source: (StackOverflow)

How to update the GUI from another thread in C#?

What is the simplest way to update a Label from another thread?

I have a Form on thread1, from that I'm starting another thread (thread2). While thread2 is processing some files I would like to update a Label on the Form with the current status of thread2's work.

How can I do that?


Source: (StackOverflow)

When is Windows Forms the correct choice vs WPF? [duplicate]

Possible Duplicate:
WPF versus Windows Forms

I presume that WPF is intended to eventually replace Windows Forms altogether, but for now, they are both shipping.

My question is, when is one more appropriate than another? Should we be leaving Windows Forms to legacy and creating new Windows Clients in WPF only?

Disclaimer: I'm not generally a Windows client developer.


Source: (StackOverflow)

How can I make a .NET Windows Forms application that only runs in the System Tray?

What do I need to do to make a Windows Forms application run in the System Tray?

Not an application that can minimize to the tray, but one that exists only in the tray, with nothing more than an icon, tool tip, and "right click" menu.


Source: (StackOverflow)

Load image from resources area of project in C#

I have an image in my project stored at Resources/myimage.jpg. How can I dynamically load this image into Bitmap object?


Source: (StackOverflow)

How to hide TabPage from TabControl [duplicate]

This question already has an answer here:

How to hide TabPage from TabControl in WinForms 2.0?


Source: (StackOverflow)

How can I make the cursor turn to the wait cursor?

I have a C# application that has users login to it, and because the hashing algorithm is expensive, it takes a little while to do. How can I display the Wait/Busy Cursor (usually the hourglass) to the user to let them know the program is doing something?

The project is in C#.


Source: (StackOverflow)

UI Design Pattern for Windows Forms (like MVVM for WPF)

MVVM is most commonly used with WPF because it is perfectly suited for it. But what about Windows Forms? Is there an established and commonly used approach / design pattern like this for Windows Forms too? One that works explicitly well with Windows Forms? Is there a book or an article that describes this well? Maybe MVP or MVC based?


Source: (StackOverflow)