MdiWindowListItem


Author
Message
Edhy Rijo
E
StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
H all,

I am using the LoadForm method posted here to load the forms, but I would like to automate this method by passing the form name stored in a Tag property of a menu item or a DevExpress RibbonControl Item.

The item will have the name of the form as string like this "frmProducers" but the LoadForm method is expecting a System.Type parameter instead of string, how can I either convert the Item.Tag string containing the form name as a System.Type expected by the LoadForm method posted here?

Private Sub LoadForm(ByVal FormType As System.Type)

'-- Establish Locals

Dim loForm As System.Windows.Forms.Form

'-- Create the form

loForm = CType(Activator.CreateInstance(FormType), System.Windows.Forms.Form)

'-- Associate the new form with the MDI form

loForm.MdiParent = Me

'-- Add a handler to the form close

AddHandler loForm.FormClosed, AddressOf HandleFormClosed

'-- This is a good place to update your open form collection

_Forms.Add(loForm.Handle.ToInt32(), loForm)

'-- Update the list or menu

'-- Show the Form

loForm.Show()

End Sub

The whole idea is to either type the form name in the Item.Tag property or read it from a database to load all the Items and update the Item.Tag which will make a generic single call to the LoadForm.

Thanks.

Edhy Rijo

StrataFrame Team
S
StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
You can use System.Type.GetType(typeNameAsString) to convert a string to a System.Type.  You'll need to store more than just "frmFormName" in the tag of the form, however, because the Type.GetType() static method requires the full name of the class.

Type.GetType("MyNamespace.frmMyFormName")

That will be what you'll have to give it.

Also, it might be the case that that method will not be able to resolve the name because your form type exists in another assembly.  If that's the case, then you'll need to give it the fully qualified assembly name.  It's basically the full name with a comma and the assembly name right after it like this:

"MyNamespace.frmMyFormName, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"

That's the difference between the Type.GetType() static method and the GetType() keyword (the keyword turns blue when you type it).  If you use the keyword, it will always resolve because the reference is resolved and at compile-time.  So, you might want to create a method that will resolve the names for you and uses the keyword to get the type.  Like this:

Public Shared Function ResolveFormType(ByVal formName As String) As Type
    Select Case formName
        Case "frmForm1"
            Return GetType(frmForm1)

        Case "frmForm2"
            Return GetType(frmForm2)

        Case "frmForm3"
            Return GetType(frmForm3)

    End Case
End Function

If you use this method, then the string can be any arbitrary value, as long as it's unique and you can assign it a type to return from the method.

Edhy Rijo
E
StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Ben Chase (03/31/2008)
You'll need to store more than just "frmFormName" in the tag of the form, however, because the Type.GetType() static method requires the full name of the class.

Type.GetType("MyNamespace.frmMyFormName")

Hi Ben,

Thanks for the explanation.  Using

Type.GetType("MyNamespace.frmMyFormName")
does give me the results I was looking for.

If I were to use the ReolveForm solution, I would then have to make an entry in the Case statement for every form I add to the project and all I am trying to do is to have all forms identified in a Item.Tag so I don't have to manually remember to keep adding it to the Case statement.  Thanks again!!!

Edhy Rijo

Chan
Chan
Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)
Group: Forum Members
Posts: 533, Visits: 2K
Hi,

I tried the code snippet and convert it to C# as below. I face problem that, the value of loForm.handle is changed in HandleFormClosed event handler. Any ideas? Thank you



private void LaunchForm(System.Type FormType)

{

//-- Establish Locals

Form loForm;

//-- Create the form

loForm = (Form)Activator.CreateInstance(FormType);

//-- Set the MDI parent

loForm.MdiParent = this;

int lnHandle = (int)loForm.Handle;

loForm.FormClosed += this.HandleFormClosed;

_Forms.Add(lnHandle, loForm);

imgWindows.Images.Add(loForm.Text, loForm.Icon);

lvWindows.Items.Add(lnHandle.ToString(), loForm.Text, imgWindows.Images.Count - 1);

//-- Show the form

loForm.Show();

}



private void HandleFormClosed(object sender, FormClosedEventArgs e)

{

System.Windows.Forms.Form loForm;

loForm = (Form)sender;

int lnHandle = (int)loForm.Handle;

if (_Forms.ContainsKey(lnHandle))

{

loForm = _Forms[lnHandle];

loForm.FormClosed -= HandleFormClosed;

imgWindows.Images.RemoveByKey(loForm.Text);

lvWindows.Items.RemoveByKey(lnHandle.ToString());

_Forms.Remove(lnHandle);

}

}

Chan
Chan
Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)
Group: Forum Members
Posts: 533, Visits: 2K
Hi,

May I know what control/container are you using to drop window and user panel on? I tried to use flowlayout but its docking cause themetoolstrip unable to dock to top with full screen width.



Please advice.



Thank you
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Handle the FormClosing event instead of the FormClosed event...that way you will still have a handle.
Mike Tomlin
Mike Tomlin
StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)
Group: StrataFrame Users
Posts: 23, Visits: 5.8K
I'm trying the VB version of this technique and I too am getting back a different handle to that which the child window was assigned on creation. I've tried changing to formclosing but the handle still changes. Any thoughts?

Thanks

Mike

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
The form handle won't change after it has been assigned.  If you are running in an MDI environment, then you can handle the OnParentChanged event and save off the handle at that point (I recommend against this).  You should ALWAYS be able to reference the Me.Handle of the form and it will return the handle to which it is presently assigned, which is the only one that you should ever deal with.  This is standard WinForms logic...once a handle is assigned it should never be changed unless the parent changes...that may be the ONLY time that it would change. 

Now it is another thing entirely if the handle has already been released.  In that case you just need to handle an event further up the chain to perform your closing logic.

Edhy Rijo
E
StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Just to confirm,

I'm also getting back a different handle to than which the child window was assigned on creation.

And also I've tried changing to formclosing but the handle still changes.

Edhy Rijo

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
OK...different than what?  You have mentioned that you are getting back a different handle...but what are you comparing the handle to? 
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search