How can I add a control to a Window Form at runtime

To add a control at runtime, you do three steps: 1. Create the control 2. Set control properties 3. Add the control to the Form’s Controls collection In general, if you need help on exactly what code you need to add, just look at the code generated by the designer when you add the control at design time. You can generally use the same code at runtime. Here are code snippets that create a textBox at runtime. [C#] //step 1 TextBox tb = new TextBox(); //step2 tb.Location = new Point( 10, 10); tb.Size = new Size(100, 20); tb.Text = ‘I was created at runtime’; //step3 this.Controls.Add(tb); [VB.NET] ’step 1 Dim tb as TextBox = New TextBox() ’step2 tb.Location = New Point( 10, 10) tb.Size = New Size(100, 20) tb.Text = ‘I was created at runtime’ ’step3 Me.Controls.Add(tb)

How do I make the TreeView scroll when I drag an item to the top or bottom?

When you drag an item within the TreeView, you can handle the DragOver event for a drag-drop. If you want to drag-drop into a spot that’s not currently visible, you can scroll the TreeView by handling the DragOver event: [C#] private void treeView1_DragOver(object sender, System.Windows.Forms.DragEventArgs e) { TreeView tv = sender as TreeView; Point pt = tv.PointToClient(new Point(e.X,e.Y)); int delta = tv.Height – pt.Y; if ((delta < tv.Height / 2) && (delta > 0)) { TreeNode tn = tv.GetNodeAt(pt.X, pt.Y); if (tn.NextVisibleNode != null) tn.NextVisibleNode.EnsureVisible(); } if ((delta > tv.Height / 2) && (delta < tv.Height)) { TreeNode tn = tv.GetNodeAt(pt.X, pt.Y); if (tn.PrevVisibleNode != null) tn.PrevVisibleNode.EnsureVisible(); } } [VB.NET] Private Sub treeView1_DragOver(sender As Object, e As System.Windows.Forms.DragEventArgs) If TypeOf sender is TreeView Then Dim tv As TreeView = CType(sender, TreeView) Dim pt As Point = tv.PointToClient(New Point(e.X, e.Y)) Dim delta As Integer = tv.Height – pt.Y If delta < tv.Height / 2 And delta > 0 Then Dim tn As TreeNode = tv.GetNodeAt(pt.X, pt.Y) If Not (tn.NextVisibleNode Is Nothing) Then tn.NextVisibleNode.EnsureVisible() End If End If If delta > tv.Height / 2 And delta < tv.Height Then Dim tn As TreeNode = tv.GetNodeAt(pt.X, pt.Y) If Not (tn.PrevVisibleNode Is Nothing) Then tn.PrevVisibleNode.EnsureVisible() End If End If End If End Sub ’treeView1_DragOver

How can I programmatically position the cursor on a given line and character of my richtextbox

There are a couple different methods that can be used here. The first changes focus, so may not be possible if you have controls that fire validation. The second uses interop, which requires full trust. Method 1: Eric Terrell suggested this solution in an email to [email protected]. The richtextbox control contains a Lines array property, one entry for every line. Each line entry has a Length property. With this information, you can position the selection cursor using code such as: private void GoToLineAndColumn(RichTextBox RTB, int Line, int Column) { int offset = 0; for (int i = 0; i < Line – 1 && i < RTB.Lines.Length; i++) { offset += RTB.Lines[i].Length + 1; } RTB.Focus(); RTB.Select(offset + Column, 0); } (Note: you may want to store this.ActiveControl to be retrieved after calling Select()). Method 2: const int SB_VERT = 1; const int EM_SETSCROLLPOS = 0x0400 + 222; [DllImport(‘user32’, CharSet=CharSet.Auto)] public static extern bool GetScrollRange(IntPtr hWnd, int nBar, out int lpMinPos, out int lpMaxPos); [DllImport(‘user32’, CharSet=CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, POINT lParam); [StructLayout(LayoutKind.Sequential)] public class POINT { public int x; public int y; public POINT() { } public POINT(int x, int y) { this.x = x; this.y = y; } } // Example — scroll the RTB so the bottom of the text is always visible. int min, max; GetScrollRange(richTextBox1.Handle, SB_VERT, out min, out max); SendMessage(richTextBox1.Handle, EM_SETSCROLLPOS, 0, new POINT(0, max – richTextBox1.Height));

Programming Windows with C#

Programming Windows(r) with C# (Core Reference) by Charles Petzold ISBN: 0735613702 This is an excellent book for both Windows Forms and GDI+. As the title suggests it is oriented towards C# programmers. VB programmers should have no trouble following along. Petzold writes lucid prose. Update: There is a VB.NET version of this book available now. Programming Microsoft Windows with Microsoft Visual Basic .NET (Core Reference). ISBN: 0735617996.

How can I let the user edit my Multi-Dimensional collection?

This is usually a problem if you have a custom collection type MyCollection which itself can take items of type MyCollection. The problem is because a single instance of a UITypeEditor is used by the framework irrespective of how many types and instances it serves to edit. Which means you cannot start editing your MyCollection from within a MyCollection editor, since the editor is already open. To work around this problem, you can provide a custom editor as follows: public class CustomCollectionEditor : CollectionEditor { // The base class has its own version of this property // cached CollectionForm private CollectionForm collectionForm; public CustomCollectionEditor(Type type) : base(type) { } public override object EditValue( ITypeDescriptorContext context, IServiceProvider provider, object value) { if(this.collectionForm != null && this.collectionForm.Visible) { // If the CollectionForm is already visible, then create a new instance // of the editor and delegate this call to it. BarItemsCollectionEditor editor = new BarItemsCollectionEditor(this.CollectionType); return editor.EditValue(context, provider, value); } else return base.EditValue(context, provider, value); } protected override CollectionForm CreateCollectionForm() { // Cache the CollectionForm being used. this.collectionForm = base.CreateCollectionForm(); return this.collectionForm; } }