Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I dynamically load a Windows form into a panel control at runti 5

Status
Not open for further replies.

SBendBuckeye

Programmer
Joined
May 22, 2002
Messages
2,166
Location
US
Hello All,

We have a huge ugly form with multiple tabs on it. It is very unwieldy. I am in the process of splitting the data off into separate forms, 1 per tab. So I have TabPage1, frmTabPage1, etc.

I have already loaded several static datatables via multithreading to speed up load time. I would like to do something similar with the forms, eg open them using multiple threads (using delegates of course) and then only show them when the user clicks a given tab the first time.

The logic behind this is that most users only use a subset of the tabs on the form. Any ideas, suggestions, etc would be much appreciated. Thanks in advance!

Have a great day!

j2consulting@yahoo.com
 
Courtesy of our good friend chrissie,

Dim f2 As New form2
f2.TopLevel = False
panel1.Controls.Add(f2)
f2.Show


Hope this helps.
 
Still I will take none of the credit. Since I found it on vbcity.

But I would still be carefull using tabs.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
You guys rock! I posted this last night just before I left and had 3 responses from heavy hitters by morning. I am already using the above technique.

I wasn't very clear so let me clarify. Associated with each tab is a corresponding form. I know I could use the above in the tab pages click event but I was hoping to avoid using a case statement such as this:
Code:
Dim frm As Form

Select Case CType(Sender, TabPage).Tag.ToString
    Case "0"
        frm = New form0
    Case "1"
        frm = New form1
End Select

frm.TopLevel = False
panel1.Controls.Add(frm)
frm.Show
Thanks again to chrissie1, you can use Reflection to return a control object from its name. I was hoping to be able to do something similar with the form like this:
Code:
Dim frm As Form

'Assuming TabPag0, TabPage1, etc
With CType(Sender, TabPage).Name
    frm = GetFormFromName("form" & .Substring(.Length - 1, 1))
End With

frm.TopLevel = False
panel1.Controls.Add(frm)
frm.Show 

Private Function GetFormFromName(formName As String) As Form
    Dim frm As Form
    'Code to return form instance from name
    Return frm
End Function
As an aside to chrissie1's code above posted by earthandfire, the following will expand the form to fill the entire panal as well as move it up so the title bar is hidden and thus unavailable:
Code:
Private Sub Panel1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.Click
   Dim pan As Panel = CType(sender, Panel)
   Dim frm As Form
   frm = New YourTabFormName
   frm.TopLevel = False

   'Assumes borders are uniform around all edges so check width
   Dim delta As Integer = frm.ClientSize.Width - frm.Width
   'Calculate title bar size 
   delta = frm.ClientSize.Height - (frm.Height + delta)

   'Adjust panel height and location to hide form title bar
   pan.Location = New Drawing.Point(pan.Location.X, pan.Location.Y + delta)
   pan.Height -= delta
   pan.Controls.Add(frm)

   frm.WindowState = FormWindowState.Maximized
   frm.Dock = DockStyle.Fill
   frm.Show
End Sub


Have a great day!

j2consulting@yahoo.com
 
I'm not fat.

And what is the problem exactly?

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
I'd like to be able to instantiate a form based on its name. So instead of

Dim frm As New MyFormName

I would like to be able to do something like

Dim frm As New Form = GetFormByName("MyFormName")

I'm looking for the form equivalent to your control FAQ which allows you retrieve a control object based on its name. Sorry I haven't communicated well. Thanks for the help!

Have a great day!

j2consulting@yahoo.com
 
The only ways I know of doing that are creating a function that takes a string and returns an object of the type you want, or by having an object and using getType.

ie:
Code:
public sub GetTypeFromString(ObjectType as string) as type
  select case ObjectType.toupper
    case "STRING"
      return gettype(String)
    case "INTEGER"
      return gettype(Integer)
'...
  end select
end function

or if you already have an object:
Code:
  dim x as new MyObject.GetType()

-Rick


VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Maybe a slight variation of this may be of use:

Code:
  Public Shared Function LoadPlugIn(ByVal AsmName As String, ByVal FormName As String) As Form

    Dim asm As [Assembly] = [Assembly].LoadFile(AsmName)
    Return CType(asm.CreateInstance(FormName), Form)

  End Function

where AsmName is the name (and path) of the Assembly holding the Form.

Call it with:

Dim f As Form = LoadPlugIn(AssemblyPathAndName, FormName)
f.TopLevel etc. etc.



You will also need:

Imports System.Reflection


Hope this helps.
 
do you think I could iterate through all the forms in my project?

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Thanks earthandfire! That pointed me in the right direction. The following code works inside the currently executing code:
Code:
Imports System.Reflection
Imports System.Windows.Forms

Class Test
    Public Shared Sub Main(ByVal formName As String)
        Dim asmName As String = [Assembly].GetEntryAssembly().GetName.Name
        Dim formType As Type = Type.GetType(asmName & "." & formName)
        Dim frm As Form = CType(Activator.CreateInstance(formType), Form)
    End Sub
End Class
Invoke it like so:
Code:
 Test.Main("Form1")

Have a great day!

j2consulting@yahoo.com
 
Just guessing and not tested but possibly with something like:

Dim asm As [Assembly] = [Assembly].GetExecutingAssembly()

and then something like:

For Each t as Type in asm.GetTypes()
If TypeOf t Is Form Then Messagebox.Show(CType(t,Form).Name)
Next


At least something along those lines.


You probably know reflection better than me, chrissie, but


Hope this helps
 
I'm in a good mood and I'll give stars to anyone.

so voila one for sbendbuckeye and oe for rick in the other thread about threading

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
and I really need to buy another keybaord, one with a cable thistime, these wireless things only work for other people.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
FYI, I wrote this up as Get Form by Name faq796-6037.

Have a great day!

j2consulting@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top