How to serialize properties of items that do not implement IComponent in a collection?

You have to set DesignerSerializationVisibility attribute on the property whose type is a strongly-typed collection to Content. You have to ensure the collection is created at startup or on demand. In order to support serialization of items that do not implement IComponent into your code you have to write a TypeConverter for that class that can convert to InstanceDescriptor. See the documentation on InstanceDescriptor for an example.

How can I make sure I don’t open a second instance modeless dialog that is already opened from my main form

One way to do this is to maintain a list of opened modeless dialogs and check this list before you open a new one to see if one is already present. If you open all these modeless dialog’s from the same ‘main’ form, then you can use the Owned Forms property of that main form to maintain this list of opened dialogs. Below are some code snippets that suggest how you must go about this. Note that your dialog forms need to be able to turn off the ownership. This is done below by adding an Owner field to the dialog form. //sample code that either opens a new dialog or displays an already opened dialog private void button1_Click(object sender, System.EventArgs e) { foreach (Form f in this.OwnedForms) { if (f is Form2) { f.Show(); f.Focus(); return; } } //need a new one Form2 f2 = new Form2(); this.AddOwnedForm(f2); f2.Owner = this; f2.Show(); } //code for form2 public class Form2 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; public Form Owner; //………….. private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e) { Owner.RemoveOwnedForm(this); } }

How do I browse for, and read a text file into an TextBox

You use the Frameworks OpenFileDialog to implement this functionality. using System.Text; using System.IO; …. private void button1_Click(object sender, System.EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Title = ‘Open text file’ ; dlg.InitialDirectory = @’c:\’ ; dlg.Filter = ‘txt files (*.txt)|*.txt|All files (*.*)|*.*’ ; if(dlg.ShowDialog() == DialogResult.OK) { StreamReader sr = File.OpenText(dlg.FileName); string s = sr.ReadLine(); StringBuilder sb = new StringBuilder(); while (s != null) { sb.Append(s); s = sr.ReadLine(); } sr.Close(); textBox1.Text = sb.ToString(); } }

How do I create a new image off a base image with certain portions of the base image modified, in code

This code depends on the actual bitmap in use. This logic sets a random rectangular portion in the image to a new color. public class ImageUtil { private Image baseImage; private void InitBaseImage(Image baseImage) { this.baseImage = baseImage.Clone() as Image; } private Image ApplyNewColorOnImage(Color newColor) { // Create a new bitmap off the base image. Image newImage = this.baseImage.Clone() as Image; Bitmap newBitmap = new Bitmap(newImage); // Set the Color cue pixels to the appropriate color. // This logic of course, depends on the actual bitmap. for (int i = 12; i <= 14; i++) for (int j = 2; j <= 14; j++) newBitmap.SetPixel(j, i, newColor); return newImage; } }

Why would I be getting a NullReferenceException in Windows Forms with no application code in the call stack

The CLR is catching an Access Violation that’s being thrown from unmanaged code, and that is propagating up as a NullReferenceException. I’ve seen this happen with certain common control library windows types if an application such as spy++ is running, and I see this is the TreeView control that is having troubles with a mouse down. Have you done any modification to the control through P/Invoke methods? (from [email protected] on microsoft.public.dotnet.framework.windowsforms)