Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Knowledge Base > Code Library > Creating a New Form of the Specified Type

Thread Tools Display Modes
Old 03-25-2004, 08:42 AM
Mikecrosoft's Avatar
Mikecrosoft Mikecrosoft is offline
Mexican Coder
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
Default Creating a New Form of the Specified Type

This code its used to create new forms (or another types) just passing the Type of the form, this was done using a method called InvokeMember of the Type Class, this method calls the constructor of the specified Type and returns a new instance of it. So I used this code for my MDI application, this function searches for a opened window of one Type, if there is opened window, just returns that window, but if the window type is not found, return a new one. I hope help.

Public Function SearchWindow(ByVal MyFormType As Type, ByVal ParentWindow As Form) As Form

		'Check if the MyFormType is a Window
		If Not MyFormType.BaseType.Equals(GetType(Form)) Then
			MsgBox("MyFormType is not a Window")
			Exit Function
		End If
		Dim Frm As Form
		'Searches in the MDIParent object if one instance of the specified form
		'is opened, if yes return it.
		For Each Frm In ParentWindow.MdiChildren
			If Frm.GetType.Equals(MyFormType) Then
				Return Frm
			End If
		'If any form was found, so created a newone form on the specified type ;)
		Dim NewFrm As Object = MyFormType.InvokeMember(Nothing, Reflection.BindingFlags.DeclaredOnly Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.CreateInstance, Nothing, Nothing, Nothing)
		'Return the new form
		Return NewFrm
End Function

* If I stop to ask I will stop to learn
* Just I know that I don't know nothing
Reply With Quote

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a Shadowed edge for a round rectangled form Nucleon API 3 12-15-2003 09:04 AM
disable print preview from xlDialogPrint w_seyller Excel 9 12-11-2003 12:20 PM
MDI Form and dimensions kkonkle Interface and Graphics 6 03-26-2003 10:50 AM
Creating & Using Form Methods outside of the form ... tcalp General 2 03-17-2003 10:27 AM
ActiveX Control - Creating Method and Calling From Form oceanic99 General 1 02-13-2003 07:46 PM


Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..