:: Home

  login:         
  passwords:  

Winforms Interview Questions

Windows Forms Deployment
Windows Forms Controls
Windows Forms Data Binding
Windows Forms Datagrid
Windows Forms Docking
Windows Forms Keyboard Handling
Windows Forms Layout
Windows Forms Licensing
Windows Forms Menus
Windows Forms Mouse Handling
Windows Forms from MFC
Windows Forms from VB6
Windows Forms Patterns
Windows Forms Printing
Windows Forms Resources
Windows Form Scrolling
Windows Forms Tips
Windows Forms Common Dialogs
Windows Forms Listbox
Windows Forms ComboBox
Windows Forms Rich TextBox
Windows Forms ListView
Windows Forms TreeView
Windows Forms Button
Windows Forms TabControl
Windows Forms TextBox
Windows Forms MDI
Windows Forms Cursors
Windows Forms WebBrowser
Windows Forms PictureBox
Windows Forms Form
Windows Forms MDI
Windows Forms In IE
GDI+Bitmaps&Images
GDI+Font
GDI+Color
GDI+Brushes
GDI Drawing Tips
GDI+ from GDI
GDI Paths Regions
GDI+Pens
Interioerability WIn32
Tools Metadata Viewers
Design Time Serialization
Design Time Custom Designers
Design Time Tips
Design Time Type Editors
Vs.Net Tips
Vs.Net Debugging
Vs.Net Macros
Framework Tips General
Framework Tips Events
Framework Tips General IO
Framework Tips Strings
Framework Tips Threading
Tool Resource Editor
Design Time UI
Framework Tips CGI
Framework Tips XML

WPF Interview Qs

SilverLight Interview Qs

SAP Interview Questions

Oracle Interview Questions

PHP Interview Questions

Ajax Interview Questions

IIS 7.0

OOP Interview Questions

Ruby Interview Questions

Sql Server Interview Questions

SharePoint 2007 Questions

Microsoft Crm Questions

Windows Forms ListBOx

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

19.Windows Forms listBox

    19.1 How do I implement Drag and Drop support between ListBoxes?
    19.2 How can I drag file names from Windows Explorer and drop them into a listbox?
    19.3 How do I implement an ownerdrawn listbox?
    19.4 How can I set the width of a listbox to fit the text?

19.1 How do I implement Drag and Drop support between ListBoxes?

The code below minimally handles D&D for a single selection list box by handling four events. The D&D is initiated in MouseDown if the user mouses down on the current selection. The DragEnter event is used to set the dragging effect to copy if you are not over the drag source. The DragDrop event is used to do the drop. And finally, the SelectedIndexChanged event is used to track the current selection for use in the MouseDown event. You can download a working project (C#, VB). This project handles additional events to provide visual queues during the drop.

public class ListBoxDragNDrop : ListBox
{
private int lastMouseUpItemIndex = -1;
private bool isDropSource = false;
public ListBoxDragNDrop()

{
this.AllowDrop = true; //allow D&D
this.SelectionMode = SelectionMode.One; //single selection
DragDrop += new System.Windows.Forms.DragEventHandler(OnDragDrop);
DragEnter += new System.Windows.Forms.DragEventHandler(OnDragEnter);
MouseDown += new System.Windows.Forms.MouseEventHandler(OnMouseDown);
SelectedIndexChanged += new System.EventHandler(OnSelectedIndexChanged);
}
private void OnDragDrop(object sender, DragEventArgs e)
{
if(e.Effect == DragDropEffects.Copy)
{
Point point = this.PointToClient(new Point(e.X, e.Y));
int index = this.IndexFromPoint(point);
if( index > -1 && index < this.Items.Count)
Items.Insert(index, e.Data.GetData(DataFormats.Text));
else
Items.Add(e.Data.GetData(DataFormats.Text));
}
}
private void OnDragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text) && !isDropSource )
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(MouseButtons == MouseButtons.Left && SelectedIndex == lastMouseUpItemIndex)
{
isDropSource = true;
DoDragDrop(SelectedItem, DragDropEffects.Copy);
isDropSource = false;
lastMouseUpItemIndex = this.SelectedIndex;
}
}
private void OnSelectedIndexChanged(object sender, System.EventArgs e)
{
lastMouseUpItemIndex = this.SelectedIndex;
}
}
One more note. If your listboxes contain full file pathnames, you can support dropping these paths onto Windows Explorer by supporting the FileDrop dataformat. In the OnMouseDown override, if you replace the DoDragDrop line with the following code block, you will be able to drop files.
//put the file path is a string array
string[] files = new String[1];
files[0] = SelectedItem.ToString();
//create a dataobject holding this array as a filedrop
DataObject data = new DataObject(DataFormats.FileDrop, files);
//also add the selection as textdata
data.SetData(DataFormats.StringFormat, SelectedItem.ToString());
//do the dragdrop
DoDragDrop(data, DragDropEffects.Copy);


19.2 How can I drag file names from Windows Explorer and drop them into a listbox?

Change the control's Anchor property so that it is anchored on all 4 sides.

Please note that you can only have 1 control per form anchored in this manner (all 4 sides). And other controls on the form should be anchored by their sides that are not adjacent to special control anchored on all 4 sides.


19.3 How do I implement an ownerdrawn listbox?

Check out this sample project at gotnetdot.com. It derives from Form and implements the owner drawing by handling the DrawItem event and MeasureItem event.

// add a reference to System.Design.DLL
using System.Windows.Forms.Design;
.............
public class DirBrowser : FolderNameEditor
{
FolderBrowser fb = new FolderBrowser();
public string Description
{
set { _description = value; }
get { return _description; }
}
public string ReturnPath
{
get { return _returnPath; }
}
public DirBrowser() { }
public DialogResult ShowDialog()
{
fb.Description = _description;
fb.StartLocation = FolderBrowserFolder.MyComputer;
DialogResult r = fb.ShowDialog();
if (r == DialogResult.OK)
_returnPath = fb.DirectoryPath;
else
_returnPath = String.Empty;
return r;
}
private string _description = "Choose Directory";
private string _returnPath = String.Empty;
}

19.4 How can I set the width of a listbox to fit the text?

You can subclass ComboBox. In your derived class, make sure you set

this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.DropDownStyle = ComboBoxStyle.DropDownList;

Copyright 2007, Megasolutions Ltd
     
Copyright 2007, Megasolutions Ltd