Click here to Skip to main content
15,905,322 members
Home / Discussions / C#
   

C#

 
GeneralDetermining the currently running processes Pin
sam00715-Jun-04 12:54
sam00715-Jun-04 12:54 
GeneralRe: Determining the currently running processes Pin
Dave Kreskowiak16-Jun-04 3:42
mveDave Kreskowiak16-Jun-04 3:42 
GeneralXmlSerializer - Invalid Xml Header Pin
AndyBewley15-Jun-04 12:11
sussAndyBewley15-Jun-04 12:11 
GeneralRe: XmlSerializer - Invalid Xml Header Pin
Heath Stewart16-Jun-04 3:43
protectorHeath Stewart16-Jun-04 3:43 
GeneralRe: XmlSerializer - Invalid Xml Header Pin
bigTrak17-Jun-04 12:20
bigTrak17-Jun-04 12:20 
GeneralRe: XmlSerializer - Invalid Xml Header Pin
Heath Stewart17-Jun-04 17:09
protectorHeath Stewart17-Jun-04 17:09 
GeneralConstructors Pin
Ganjah78615-Jun-04 11:57
Ganjah78615-Jun-04 11:57 
GeneralRe: Constructors Pin
leppie15-Jun-04 12:07
leppie15-Jun-04 12:07 
GeneralRe: Constructors Pin
Colin Angus Mackay15-Jun-04 12:09
Colin Angus Mackay15-Jun-04 12:09 
GeneralRe: Constructors Pin
Heath Stewart15-Jun-04 12:10
protectorHeath Stewart15-Jun-04 12:10 
GeneralRe: Constructors Pin
Ganjah78615-Jun-04 12:43
Ganjah78615-Jun-04 12:43 
GeneralRe: Constructors Pin
Heath Stewart16-Jun-04 3:06
protectorHeath Stewart16-Jun-04 3:06 
GeneralRe: Constructors Pin
Alvaro Mendez15-Jun-04 12:55
Alvaro Mendez15-Jun-04 12:55 
GeneralRe: Constructors Pin
Heath Stewart16-Jun-04 3:04
protectorHeath Stewart16-Jun-04 3:04 
GeneralRe: Constructors Pin
Uwe Keim15-Jun-04 21:18
sitebuilderUwe Keim15-Jun-04 21:18 
GeneralRe: Constructors Pin
Heath Stewart16-Jun-04 3:05
protectorHeath Stewart16-Jun-04 3:05 
GeneralPossible Attribute bug Pin
leppie15-Jun-04 11:18
leppie15-Jun-04 11:18 
GeneralRe: Possible Attribute bug Pin
Heath Stewart15-Jun-04 11:27
protectorHeath Stewart15-Jun-04 11:27 
GeneralRe: Possible Attribute bug Pin
leppie15-Jun-04 12:03
leppie15-Jun-04 12:03 
GeneralRe: Possible Attribute bug Pin
Heath Stewart15-Jun-04 12:06
protectorHeath Stewart15-Jun-04 12:06 
GeneralLooping Through Windows Forms Instances Pin
XanderSon15-Jun-04 11:06
XanderSon15-Jun-04 11:06 
GeneralRe: Looping Through Windows Forms Instances Pin
Heath Stewart15-Jun-04 11:23
protectorHeath Stewart15-Jun-04 11:23 
You need to keep track of these Forms in a collection so that you can enumerate them. Using a foreach loop is easy - just be sure you don't change the underlying collection.

So, if you had a static collection property on a class (perhaps your Form, if you're dealing with multiple instances of a single Form class, for example), you can add your instances to it and enumerate it at any time:
public class MyForm : Form
{
  private static ArrayList instances = new ArrayList();
  public MyForm()
  {
    instances.Add(this);
  }
  protected override void Dispose(bool disposing)
  {
    instances.Remove(this);
    base.Dispose(disposing);
  }
  public static IList Instances
  {
    get { return instances; }
  }
}
At any time, just use the following code to enumerate your forms and, for example, output the caption:
foreach (MyForm form in MyForm.Instances)
  Console.WriteLine(form.Text);
If you want to do this for multiple types, then define a class with a public or internal static collection property, perhaps adding a few handy methods. Don't forget to override Dispose(bool) and remove your form instance, and to add it in your constructor for that matter. If you overload your constructor, make sure that only one instances gets added.

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: Looping Through Windows Forms Instances Pin
XanderSon15-Jun-04 14:00
XanderSon15-Jun-04 14:00 
Generalc#-Configuration file Pin
Member 115090115-Jun-04 10:46
Member 115090115-Jun-04 10:46 
GeneralRe: c#-Configuration file Pin
Heath Stewart15-Jun-04 11:18
protectorHeath Stewart15-Jun-04 11:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.