How do I implement Drag and Drop support between two TreeViews

In a posting in the Microsoft.Windows.Forms newsgroup, Brian Roder (Microsoft) gives VB.Net code snippets to handle the DragEnter, ItemDrag and DragDrop events that provide a solution to this problem. You can get C# code in this sample, TreeViewDnD. Here is some sample handlers. private void treeView2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { TreeNode newNode; if( e.Data.GetDataPresent(”System.Windows.Forms.TreeNode”, false)) { Point pt; TreeNode destinationNode; pt = treeView2.PointToClient(new Point(e.X, e.Y)); destinationNode = treeView2.GetNodeAt(pt); newNode = (TreeNode) e.Data.GetData(”System.Windows.Forms.TreeNode”); if(!destinationNode.Equals(newNode)) { //destinationNode.Nodes.Add(newNode.Clone()); destinationNode.Nodes.Add((TreeNode) newNode.Clone()); destinationNode.Expand(); //Remove original node newNode.Remove(); } } } private void treeView2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { e.Effect = DragDropEffects.Move; } private void treeView2_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e) { DoDragDrop(e.Item, DragDropEffects.Move); }

How do I format numbers, dates and currencies in a TextBox

Each type has a ToString method that can be used to accomplished formatting. Also, you can use the String.Format method to format things as well. To format dates, use the ToString member of DateTime. You may want to use the InvariantInfo setting (see below) to get culture-independent strings.

How do color the tabs on my TabControl

Ken Tucker offers this solution. Set the TabControl’s DrawMode to OwnerDraw, and then handle the DrawItem event to draw things yourself. Here are both VB and C# sample projects that display a gradient tab for the active tabpage.

How can I put Controls, a ProgressBar for example, into a StatusBar?

You cannot place controls into a StatusBar control in the designer. However, you can add any no. of Controls to the StatusBar programatically through it’s Controls property. After adding the Controls, set their Visible, Location and Bounds property appropriately. You could then create a status bar that looks like this, for example:

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. You can also download a sample that implements an owner drawn listbox by deriving from ListBox and overriding the virtual methods OnDrawItem and OnMeasureItem. Here is a OnDrawItem override that draws colored rectangles in the listbox. protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e) { //undo the selection rect on the old selection if( oldSelectedRect != e.Bounds && oldSelectedIndex > -1 && oldSelectedIndex < Items.Count) { e.Graphics.DrawRectangle(new Pen((Color) Items[oldSelectedIndex], 2), oldSelectedRect); } //draw the item .. here we just fill a rect if( e.Index > -1 && e.Index < Items.Count) e.Graphics.FillRectangle(new SolidBrush( (Color)Items[e.Index] ), e.Bounds); //draw selection rect if needed if(SelectedIndex == e.Index) { e.Graphics.DrawRectangle(new Pen(Color.Black,2), e.Bounds); oldSelectedRect = e.Bounds; oldSelectedIndex = e.Index; } }