• 0

[VB.NET] Get the ID or Index no. of a MDI child


Question

Right - I have an MDI container form, to which I add children like this:

Dim newWin As New frmDisplay()
 ? ? ? ?newWin.MdiParent = Me
 ? ? ? ?newWin.Show()

Nice 'n simple. But I need to get some sort of ID for the child forms. Something like an integer (that would probably start at 0 or 1). I need to be able to get this number for the active form, and then use it to call inactive forms up.

Failing that, a way to give/get a unique identifier for the forms might work.

Get what I mean?

~snippet1

Link to comment
Share on other sites

15 answers to this question

Recommended Posts

  • 0

Derive a class from System.Windows.Forms.Form that implements an UID property and use that, plus a global window manager that resolve these UIDs into objects.

Link to comment
Share on other sites

  • 0

Umm - I kinda get what you mean, but I'm really actually quite dumb on how to do these things.

Could you hold my hand please? :unsure:

- In other words, I need a little help with that ;)

Link to comment
Share on other sites

  • 0

The derived form:

public class UniqueForm : System.Windows.Forms.Form
{
    int uid = (new Random()).Next();
    public int Uid
    {
        get { return uid; }
    }
}

Translate this to VB.NET at will. You could aswell use a cryptographic number generator, but that'd load the System.Security assembly into memory.

The global window manager would be something more complex. Maybe I'll amuse myself to write one later today, since I need one so or so for a future project. But it'll be in C#, I don't think that'll help.

Link to comment
Share on other sites

  • 0

Alright. I get how to make the UID property;

In frmDisplay:

Public uID as Integer

In frmMain:

newParabWin.uID = currentWins

-that basically sets the uID of the form to the current number of MDI children (after it has been added), which gives us the ID effect we're after.

But what I then need to do is;

Be able to use:

Me.ActiveMdiChild.uID

And to be able to call any of the children by uID.

I'm really stuck here.

Link to comment
Share on other sites

  • 0

I have a hard time explaining it to you. If you'd use C# I'd write the stuff now and be done.

You need a static class where you register the MDI windows with. You can automate the registering by overriding the constructor of the Form class. That class would allow you then resolving IDs into Form objects and also call MDI windows by ID. Basically a helper class. You could merge that helper in the derived Form class too, if you want.

I can speak better in code, here's how I'd do it, if you can read C# (untested):

public class ValueNotFoundException : System.Exception
{
	public ValueNotFoundException(string message) : base(message)
	{ }
}

public class MdiForm : System.Windows.Forms.Form
{
	private static Dictionary<int, MdiForm> windowList;

	public static MdiForm GetFormByID(int uid)
	{
  MdiForm m = windowList[uid];
  if (m == null)
  {
 	 throw new KeyNotFoundException("UID not found.");
  }
  else
  {
 	 return m;
  }
	}

	public static int GetIDByReference(MdiForm form)
	{
  foreach (int key in windowList.Keys)
  {
 	 if (windowList[key] == form) return key;
  }
  throw new ValueNotFoundException("This window isn't registered.");
	}

	private int uid = 0;
	public int Uid
	{
  get { return uid; }
	}

	public MdiForm() : base()
	{
  while (windowList.ContainsKey(uid))
  { uid = (new Random()).Next(); }

  windowList.Add(uid, this);
	}

	~MdiForm()
	{
  windowList.Remove(uid);
	}

	static MdiForm()
	{
  windowList = new Dictionary<int, MdiForm>();
  windowList.Add(0, null); // Reserved.
	}
}

You then create MdiForm's instead of regular Form's, but since the base of MdiForm is Form, you can use it as regular form, incl. MDI and what not, the constructor registers each created window, and you'll be able to get the object via ID by doing MdiForm.GetFromByID(5) etc.

And stuff.

Edited by Tom Servo
Link to comment
Share on other sites

  • 0

Ok - thanks anyway. I actually wrote my last post as you posted yours (and in the end I suppose what I was asking may as well be directed at anyone else out there who might know).

So - does anyone know? I know - I'm dumb, but I've not yet really read any proper material on any of this (just figured it all out, lol - and the odd help file).

/edit: Now (kinda) reading your code...

/edit2: Thank you very very much - it shall be a fantastic help once I translate it :)

- can anyone help a little with that?

Link to comment
Share on other sites

  • 0

I have an idea - would it be possible for someone who has C# to compile that file as a DLL, and for me to then reference that in my project?

Link to comment
Share on other sites

  • 0

One more question - I've got someone to compile it (to a DLL) for me - how should he go about it? (He has about as much of an idea as I).

Link to comment
Share on other sites

  • 0

Yes, it doesn't have generics. In the case of 1.1 you'd use a Hashtable instead of the Dictionary<>, and do type casting if you pull values out of that table (a la val = (MdiForm)windowList[13]; ).

But instead of trying to get canned code, you should take a look into collections and try to figure out a logic to do the same as I wrote above. This is rather an easy task compaerd to whatever you'll encounter during development of whatever you do later on. See it as exercise.

Link to comment
Share on other sites

  • 0
See it as exercise.

Unfortunately I don't have time for excecise. It's 1:10am Sunday and I have to have it done by Monday!

Link to comment
Share on other sites

  • 0

Thanks a billion. Don't worry - the GUI has absoultely nothing to do with the homework task (Maths - it's a parabola generator).

/me adds Tom Servo to 'People to Kill' list. Wait - that's 'People to Thank' ;)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.