Question

Dynamically create an object where the type is not known till runtime. VB.net 2005 Generics?

Asked by: craigdev

I have the following three methods in my main MDI Parent form to handle form transitions:

Public Sub SwitchToFormAndCloseMe(ByVal sender As Form, ByVal toForm As Form)
        If sender IsNot Nothing Then
            sender.Close()
        End If
        HandleShowChild(toForm)
End Sub

Private Sub HandleShowChild(ByRef toForm As Form)
        If IsChildInMemory(toForm.GetType()) Then
            toForm.Show()
        Else
            'Need to create a new form of this type and show it
        End If
End Sub

Private Function IsChildInMemory(ByVal childType As Type) As Boolean
        Dim frmChild As Form
        For Each frmChild In Me.MdiChildren
            If frmChild.GetType() is childType Then
                Me.ActivateMdiChild(frmChild)                
                Return True
            End If
        Next
        Return False
End Function


Basically I need to implement the commented bit "Need to create a new form of this type and show it"

My usage of this is like:

Main.SwitchToFormAndCloseMe(Me, My.Forms.frmXXX)

I am open to better suggestion or best practices for form transitions, I don't want the main form to have to hold an instance of the the child forms. So want to dynamically create a form intance of the provided type and show it. Also should my methods be ByRef or ByVal for this? Can I acheive this with Generics? I have never used Generics except to hold a stronly typed List.

Thanks

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-12-16 at 19:27:33ID22095733
Topic

.NET

Participating Experts
3
Points
500
Comments
40

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. VB.Net, ByVal and ByRef
    Hi Experts, In VB.Net. When we should use ByVal and when use ByRef to pass an array to a Sub/Function? I have tested ByVal and ByRef and got the same results..... Thank you in advance.
  2. Byref and Byval
    I would like someone to give me a simple and clear illustration of using Byval and Byref in VB example. thanks

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: gena17Posted on 2006-12-17 at 00:51:30ID: 18154144

craigdev,

You can use
  System.Activator.CreateInstance(type) method.
It creates an instance of the specified type and returns it to you.

 

by: craigdevPosted on 2006-12-17 at 01:00:45ID: 18154154

Can I do this if the type only has constructors as:

Sub New()
End Sub

Or can I call a constructor that takes params if necessary, I might night need this though.

 

by: craigdevPosted on 2006-12-17 at 01:08:56ID: 18154161

This is my method as follows:

Private Sub HandleShowChild(ByVal type As Type)
        ResetStatus()

        If IsChildInMemory(type) Then
            the found form is activated in IsChildInMemory
        Else 'Create a new instance and show it
            Dim newForm = CType(System.Activator.CreateInstance(type), type)
            newForm.Show()
        End If
    End Sub

I would like to constraing the Type permissable to this function to only objects which inherit from Form. Also I don't like:

Dim newForm = CType(System.Activator.CreateInstance(type), type)
newForm.Show()

As newForm is not typed haven't tested if this will work yet as object. Ideally want something like:

Dim newForm as typeof(type) = CType(System.Activator.CreateInstance(type), type)
newForm.Show()

 

by: gena17Posted on 2006-12-17 at 01:47:28ID: 18154187

> Or can I call a constructor that takes params if necessary
Yes, you can:
  CreateInstance ( type As Type, ParamArray args As Object() )

> I would like to constraing the Type permissable to this function to only objects which inherit from Form.
You can use:
  if type.BaseType == typeof(System.Windows.Form) then ...
Sorry for C# sysntax

>Ideally want something like
I don't it is possible w/o some switch and one-by-one checking.

 

by: craigdevPosted on 2006-12-17 at 01:50:14ID: 18154191

OK I have got this far:

Public Sub SwitchToFormAndDisposeMe(ByVal sender As Form, ByVal toFormType As Type)
        If sender IsNot Nothing Then
            sender.Dispose()
        End If
        HandleShowChild(toFormType)
End Sub

Private Function IsChildInMemory(ByVal childType As Type) As Boolean
        Dim frmChild As Form
        For Each frmChild In Me.MdiChildren
            If frmChild.GetType() Is childType Then
                Me.ActivateMdiChild(frmChild)
                Return True
            End If
        Next
        Return False
    End Function

Private Sub HandleShowChild(ByVal type As Type)
        ResetStatus()

        If IsChildInMemory(type) Then
            'Ignore the provided form, just activate the found form
        Else 'Create a new instance of this type and show it
            Dim newForm = System.Activator.CreateInstance(type)
            PrepareAndShowForm(newForm)
        End If
    End Sub

I can show the new Form yah! However I am sure how best to call

SwitchToFormAndDisposeMe(..,..)

On another form I am calling as follows:

SwitchToFormAndDisposeMe(Me, frmXXX.GetType()) where frmXXX refers to the class name not an instance

but this actually calls the Sub New of form frmXX I want a solution that does not instantiate the object I just need to pass an objects type information for compare to the Switch function, the main controlling MDI will handle instantiation of new Forms. I am so close I can feel it!

Thanks








 

by: craigdevPosted on 2006-12-17 at 01:52:26ID: 18154194

I don't want to use My.Forms.frmXXX as this creates a new instance of that Form as I use it. I want to get a type object as pass it to the Switch method.

 

by: gena17Posted on 2006-12-17 at 02:03:28ID: 18154201

craigdev,
Sorry, I can't understand what you want.
Can you please elaborate?

> SwitchToFormAndDisposeMe(Me, frmXXX.GetType()) where frmXXX refers to the class name not an instance
How can you call GetType() on class name?

 

by: craigdevPosted on 2006-12-17 at 02:09:52ID: 18154210

Ok I am a litte confused now myself.

I am on FormA, I want to switch to FormB. I don't want to instantiate an instance of FormB inside class A, I want this all to handled in my Main class. I know the type I want to switch to, but I don't want to make an instance here.


Public Class FormA

Private Sub Button1_Click( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles Button1.Click
        Main.SwitchToFormAndCloseMe(Me, FormB.GetType()) 'Or just pass the class, not sure how to wire this, maybe the Switch should not take a type param
End Sub

End Class


How am I best to get the type of a class that I want, pass it to switch, let it handle it appropriately?

Thanks

 

by: craigdevPosted on 2006-12-17 at 02:13:12ID: 18154214

I am fairly new to VB.net been coding in C# mainly so maybe when I am suing FormB.GetType where FormB is the class name and I have not created an instance, in fact an instance is actually created when I use FormB?

 

by: gena17Posted on 2006-12-17 at 02:29:28ID: 18154240

I am coding in C# too :)
I can recommend the following:
If you call FormA.ShowDialog() from Main class, so Main class knows when FormA is closed and can handle it.
If you call FormA.Show() from Main class, you can add OnClose event handler to FormA in your Main class and handle this event as well.
If from some reason you don't want to create an instance of FormB in FormA (although FormA decides which Form should be opened after itself), you can add some property FormToOpenAfterMe to FormA. Main class will use this property an open whatever you want.
I hope I understood you :)

 

by: gena17Posted on 2006-12-17 at 02:31:37ID: 18154245

By the way, if FormB is a class name, how can you call FormB.GetType()
Is it allowed in VB? It is probably like typeof(FormB)?
Any case, an instance should not be created here (IMHO).

 

by: Marv-inPosted on 2006-12-17 at 02:41:49ID: 18154276

if you know the object you are passing is a form - forma and formb are inherrited from system form.

in SwitchToFormAndCloseMe(Me,FormB) where SwitchToFormAndCloseMe(a as form,b as form)

http://msdn2.microsoft.com/en-us/library/zyy863x8.aspx <- here is a link talking about ctype,directcast, and trycast

public sub SwitchToFormAndCloseMe(a as form, b as form)

'//simple - dont care because its a form
a.close
b.show

if b.gettype= gettype(FormB) then
  with directcast(FormB)
    '//do what you need
  end with
endif

end sub

youll have to excuse me if that was a bit jumbled - just woke up

 

by: craigdevPosted on 2006-12-17 at 02:41:57ID: 18154277

I want to able write code in FormA button click like

Main.SwitchToFormAndDisposeMe(Me, typeof(FormB))

and let main dispose A and create a new instance of FormB and show it,

I am not sure of the VB.net syntax for this, at the moment I am getting an erro telling me to "Insert the missing Is"

Also not sure what param my Main.SwitchToFormAndDisposeMe(.. should take whether it should be a "Type" or an object and inside the method call that object to get type, actually I don't want an object just type information.

So close but just not quite getting it.

Thanks heaps, you have been excellent help.

 

by: craigdevPosted on 2006-12-17 at 02:55:02ID: 18154293

I think when I am typing FormB it is being treated as an instance which I don't want, how do I refer to the Class and pass that to the method or just that Class's type. I never want to create an instance because quite a bit of work is done in the constructor.

 

by: Marv-inPosted on 2006-12-17 at 03:05:19ID: 18154311

im not 100% sure on this but i think anytime you refer to a class in vb the new sub fires.

you could move your processing to the load event - that will not fire till show is called.

 

by: craigdevPosted on 2006-12-17 at 03:09:01ID: 18154315

If anyone can confirm this that would be awesome, it's just that this is an existing applicaiton I have to work on with quite a few forms, it would take some time to refactor the solution and shift all processing to Load. Plus I think it's a cleaner solution if I don't have to create an instance, I shouldn't need to.

Thanks

 

by: craigdevPosted on 2006-12-17 at 03:14:44ID: 18154322

This is code now (I just don't know how to call it):

Private Sub HandleShowChild(ByVal providedType As Type)
        ResetStatus()

        If IsChildInMemory(providedType) Then
            'Ignore the provided form, just activate the found form, done in IsChildInMemory
        Else 'Create a new instance of this type and show it

            If providedType.BaseType() is GetType(Windows.Forms.Form)
                Dim newForm = System.Activator.CreateInstance(providedType)
                PrepareAndShowForm(newForm)
            Else
                Throw New ArgumentException("Type must inherit form System.Windows.Form, provided type inherits from: " & providedType.BaseType.ToString())
            End If            
        End If
    End Sub

 

by: craigdevPosted on 2006-12-17 at 03:23:56ID: 18154331

Also:

If providedType.BaseType() is GetType(Windows.Forms.Form)


fails for forms where I am using Visual Inheritance on Forms, so the baseType of the passed form is actually MyBaseForm, so I need to check

If providedType.BaseType() is GetType(Windows.Forms.Form) or providedType.BaseType().BaseType is GetType(Windows.Forms.Form)

Obviously this would only ever work for 2 levels of Inherited forms, what is a good solution for this, if you recursively kept call baseType you would get all the way to System.Object, how do you know when to stop or is there some other attribute on Type I should be testing against?

Thanks

 

by: gena17Posted on 2006-12-17 at 05:02:55ID: 18154445

You can check without recursion:
if ( (typeof(Windows.Forms.Form)).IsInstanceOfType(t) )

 

by: SanclerPosted on 2006-12-17 at 05:28:37ID: 18154479

>>
im not 100% sure on this but i think anytime you refer to a class in vb the new sub fires.
<<

In VB (up to 6) and in VB.NET 2005, there is a "default instance" of any Class based on Form: this was not the case in VB.NET 2002 or 2003.

What this means (I haven't followed the full thread by the way, so it may not be appropriate to your current needs) is that you can, anywhere in the Namespace within which a Form's class file exists, simply  refer to that TYPE and it will (if one does not already exist) create an instance.  If you only want one instance of that TYPE then you have no need expressly to instantiate it.  If you want more that one instance, you will need to go down the "normal" route - like

    Dim myForm1 As New FormType

- for other instances, and keep track of those instances.  But for the "default instance" just referring to the TYPE is enough.

Roger

 

by: craigdevPosted on 2006-12-17 at 05:35:21ID: 18154483

The thing is I don't want an instance, I do not want the Form's Sub New to be called. I want to call code like the following with the type of the class, and let this method create an intance of that class. I do not want the Constructor of that class to called before I pass the params to this method.

Private Sub HandleShowChild(ByVal providedType As Type)
        ResetStatus()

        If IsChildInMemory(providedType) Then
            'Ignore the provided form, just activate the found form, done in IsChildInMemory
        Else 'Create a new instance of this type and show it

            If providedType.BaseType() is GetType(Windows.Forms.Form)
                Dim newForm = System.Activator.CreateInstance(providedType)
                PrepareAndShowForm(newForm)
            Else
                Throw New ArgumentException("Type must inherit form System.Windows.Form, provided type inherits from: " & providedType.BaseType.ToString())
            End If            
        End If
End Sub

 

by: SanclerPosted on 2006-12-17 at 05:48:54ID: 18154510

Have a look at the last post in this thread.  

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_22080422.html

I still haven't mastered everything in this thread but, from what I have read, my feeling is that the approach outlined there might meet your needs

Roger

 

by: craigdevPosted on 2006-12-17 at 05:58:36ID: 18154530

Just read it, doesn't quite fit my requirements. Calling:

ShowForm(thatForm)

Will create an instance of thatForm, I do not want this. I want to pass the Type of thatForm to the Main module and let it create the Type if it does not already exist in my MDIChild collection else show the form. I do not want it to create an instance in the callingForm as the Constructor to thatForm does quite a bit of work, there is alot of code to refactor if I had to shift processing from New() -> Load


Kind Regards

 

by: craigdevPosted on 2006-12-17 at 06:02:46ID: 18154539

gena17

IsInstanceOfType I am having some trouble with.


If (GetType(System.Windows.Forms.Form)).IsInstanceOfType(providedType) Then
                Dim newForm = System.Activator.CreateInstance(providedType)
                PrepareAndShowForm(newForm)
Else
                Throw New ArgumentException("Type must inherit form System.Windows.Form, provided type inherits from: " & providedType.BaseType.ToString())
End If

I am getting the argException thrown as providedType.Base Type is System.Windows.Form not System.Windows.Forms.Form

If I try and check for:

(GetType(System.Windows.Forms.Form)).IsInstanceOfType(providedType) or (GetType(System.Windows.Form)).IsInstanceOfType(providedType)

VS 2005 tells me "System.Windows.Form" is not defined. I have check my base forms they all inherit from System.Windows.Forms.Form

this is a strange one!

 

by: SanclerPosted on 2006-12-17 at 06:29:58ID: 18154595

>>
Calling:

ShowForm(thatForm)

Will create an instance of thatForm, I do not want this.
<<

I beg to differ.  If you are calling your sub HandleShowChild it means you DO want an instance of your form type.  

The question that sub is answering is whether it already exists or not.  If it does exist (in its default instance) then the call with reference to its Type will not create a new instance.  If it does not exist then you will need to create it in order to show it.  I am not clear why creating it in calling the sub rather than in the sub itself makes any operational difference.  

I think the point is that - although the call to the HandleShowChild sub may be made within "the calling form", it does not (EMPHASIS added) "create an instance IN the callingForm".  Because you are calling the default instance by TYPE it is (on my understanding) created "at large".  If you then add that default instance to (e.g.) an MDI parent's children or expressly make its parent or its owner the calling form then it will exhibit the behaviour appropriate to those relationships.  But that does not mean that, as the default instance, just because it was first referred to in one particular form it is created IN that form.

Let me stress that I am not saying that the alternative approach being discussed in this thread is wrong.  It's just that, unless I have misunderstood the requirements, it seems more complicated than the existance of default instances requires.

Roger

 

by: craigdevPosted on 2006-12-17 at 07:00:01ID: 18154666

Ok so I need to just be sure on this. Also I am not quite sure what you mean by "default instance" If I have a form called FormA and FormB, and their will be many switchings between these MDIChild forms I only every want to call the constructor of each form once for the life of the application. If I change to use your ShowForm method to take a form and if my usage is as follows:

Public Class FormA
    Inherits System.Windows.Forms.Form

Private Sub btnShowFormB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowFormB.Click
       Main.ShowForm(Me, FormB) 'not explicitly creating an instance
End Sub

End Class


Public Class FormB
    Inherits System.Windows.Forms.Form

Private Sub btnShowFormA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowFormA.Click
       Main.ShowForm(Me, FormA) 'not explicitly creating an instance
End Sub

End Class


Then are you saying that a defualt instance of each form gets constructed only once, or this will happend everytime the the button is clicked, I don't want Sub New to called over and over. Form Load can be called multiple times, this is not a problem

Thanks

 

by: craigdevPosted on 2006-12-17 at 07:00:50ID: 18154670

Is it possible to achieve it the way I was suggestion using Generics in some some elegant implementation.

 

by: SanclerPosted on 2006-12-17 at 07:31:56ID: 18154722

Yes, that's what I'm saying.  It will, of course, depend on what you do with FormA while you're not using it.  If you .Close it, then the "default instance" will no longer exist, so when you call ShowForm(FormA) again, its .New will be fired again.  So, to avoid re-firing, an unused form will need to be hidden rather than disposed of.  And, as I say, it will only work if you only want ONE instance of FormA, ONE instance of FormB, etc.  And it won't work with VB.NET 2002 or 2003.

To test it, stick (temporary) debugging code in your child forms' .New subs on these lines

        Debug.WriteLine("FormA.New")

As to a more elegant, Generics approach, I'm not sure.  As this one works for me, I haven't tried it ;-)

Roger

 

by: craigdevPosted on 2006-12-17 at 07:59:58ID: 18154780

You are right Roger I just tested it. Cool it works as you described I will implement in this fashion from here in. The constructors only get called once. Wierd concept of MS but helpful.

Also some of the MDIChildren call ShowDialog for some small forms to get users data in a textbox etc, very simple forms. Yet this dialog which comes up is not linked at all to the MDIChild or Parent so if I click minimise on the parent this Dialog (MDIChild) still remains center screeen and active. Of you hit "Show Desktop" on the Windows QuickLaunch the MDIParent is minimised but the dialog still floats above all other applications, even on alt-tabing to other running applications.

Any idea how do I fix this?

What is my best practice for showing Modal Dialogs off an MDIChild?

Thanks

 

by: SanclerPosted on 2006-12-17 at 09:46:06ID: 18155034

Glad the ShowForm thing works OK.

>>
What is my best practice for showing Modal Dialogs off an MDIChild?
<<

I recognise the description re that from your current question in VB.NET (which is really my "home" TA).  I didn't chip in there because I don't think there's an easy answer.  I think you'll have to "self-code" the modality.  This is because the two built-in elements - MDI parent/children on the one hand and modal forms (i.e. used with showdialog) on the other - seem to me to be incompatible.  The MDI concept has everything working WITHIN the MDI frame (in program terms, not just visually).  Modal forms function OUTSIDE that frame.  The whole point of ShowDialog is that the user cannot do anything else until s/he has properly responded to the form concerned.

The EFFECT of modality is obviously achievable - e.g. consider the MS Word Find/Replace "dialog" in its MDI environment - but I haven't found a straightforward way of doing it just with the "normal" VB.NET mechanisms.

Here's a PAQ which might help

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21960589.html

It's the Accepted Answer post near the end that might be relevant: there was a lot of "round the houses" stuff before we got to an answer in that case.

Roger

 

by: craigdevPosted on 2006-12-17 at 12:44:29ID: 18155495

I am not sure where to start with this. I have normal form transitions going really well now.

I am at the stage at several leafs within the from menu/wizard hierarchy where the existing code has about 10 buttons on a form, each one opens a model ShowDialog() allows the user to edit some grids etc click Ok, Cancel. The container form then checks the DialogResult of that model form, it OK update(dataset) If cancel RejectChanges is being called.

So now I am presented with scenario I would have to change all the forms to call show, put the dataset update code inside the OK click handle, move the RejectChanges into the OnClose of the form, test for DialogResult Cancel if true Reject Changes?

Any other suggestion or do I just have to refactor all these forms to conform to this scenario, I can achieve modal likeness, by calling Show on the current Dialogs and also setting the FormStyleBorder as None and Dock to Fill. So you only can ever work on one form at a time. This is the way the application intends the user to drive it anyway.

Regards

 

by: craigdevPosted on 2006-12-17 at 12:52:08ID: 18155528

Also a lot of the in code execution logic relies on the the code blocking at the point ShowDialog() is called, wait for a result then carry on. Any way to achieve this with some .Net statement?

 

by: SanclerPosted on 2006-12-17 at 16:00:50ID: 18156005

I think you can keep your data-processing on the calling form.  Get the calling form to listen for a (self-coded) event that is raised by the called form (the "dialog") with a Save or Cancel argument depending which button is pressed.  And the same approach - waiting for the firing of an event from the "dialog" - could at the same time implement the "wait for a result then carry on" requirement that you mention.  In the latter context, I would envisage the calling form's calling code ending with the showing of the "dialog", and then separate code to save or reject the changes when the called form fires its event.

This is brainstorming - which means I've done no testing ;-).  And it's not ideal, even in brainstorming terms.  But whatever you do is going to have to be a compromise, and this approach might hit a better balance between saving as much as you can of what you've got at the moment and having extensively to refactor for the new environment.

I've written in general terms, because details would depend on minutiae of your set-up which I don't know.  If the general idea appeals but you need to discuss details, it might be helpful if you posted sample code for the "dialog calling" and "data processing" operations of a calling form and the subs from a "dialog" form.

Roger

 

by: craigdevPosted on 2006-12-18 at 23:54:29ID: 18164191

Sounds good, how would I force the calling form to "wait" for this event?

Also the client is very picky about the look and feel of the application (they are a media house). I need to paint the MDI Parent container with a gradient effect. I have tried handling the Paint event of the MDI Parent and doing it here, but it is not working. Ideally I would paint this effect in the child container, set all MDI Child form's background to transparent and the app would look good, all the labels would have to support transparency as well.

What is my best and most efficient (the current forms a quite flickery (I think due to the bind of the syncfusion grids)), to implement this requirement? or should I open another question?

Regards

 

by: craigdevPosted on 2006-12-19 at 01:12:02ID: 18164368

Ok I have sorted the MDI Parent paint using the solution found at the bottom here:

http://www.bobpowell.net/mdiback.htm it works really well.

However I am trying to do this for child forms:

Private Sub PrepareAndShowForm(ByRef childForm As Form)
        PrepareForm(childForm)
        childForm.BackColor = color.Transparent
        childForm.Visible = True
        childForm.Show()
End Sub

As I set my MDI child windows to have FormStyleBorder as None and Dock to Fill, they function really like a panel, I'd like each MDI child to support transparency and pick up the gradient background form the MDI Parent, also to support transparent lables on the child. Is this acheivable? or do I need to change all my existing form to inherit from panel instead of Form, what would be the consequences of this?

Thanks




 

by: SanclerPosted on 2006-12-19 at 02:27:20ID: 18164614

>>
how would I force the calling form to "wait" for this event?
<<

Put a sub to handle it in the calling form.  And then either declare the called form WithEvents (but that means you would have to declare it at form level) or add a handler when you do declare it

        AddHandler CalledForm.Event, AddressOf CallingFormEventHandler

On the painting side, it sounds as though you're now happy with what you've got but, if not, another question would be better.  Display issues are not my forte.

If, for display purposes, you decide to replace your forms by panels you wouldn't be able to use the MDI parent's child forms collection to organise / control them.  And you'd have to self-code Events (e.g. like FormClosing) that exist in Forms but not in Panels.  But, as I remarked before, you're in a "having to compromise" situation, and you're really the only one that can decide where the best compromise is.

Roger

 

by: craigdevPosted on 2006-12-19 at 02:45:26ID: 18164688

I still not qutie getting you. In calling form

dialogForm.Show(Me)
'Need to Wait here till I get an event from dialogForm only once I have that result step down to the Process() method

'Do some other processing
Process()

This behaviour works if I dialogForm.ShowDialog(Me), I need to mimic the blocking of ShowDialog for Show()

Thanks

 

by: Marv-inPosted on 2006-12-19 at 02:53:28ID: 18164721

in dialogForm the property Me.Owner will hold the reference to the caller - because you use show(Me)

so that will allow you contorl over that form - and i think there is an ownedforms that works the other way

 

by: SanclerPosted on 2006-12-19 at 02:53:37ID: 18164722

>>
In the latter context, I would envisage the calling form's calling code ending with the showing of the "dialog", and then separate code to save or reject the changes when the called form fires its event.
<<

So ...

       '[....]
       AddHandler dialogForm.Event, AddressOf CallingFormEventHandler
       dialogForm.Show(Me)
End Sub

Private Sub CallingFormEventHandler
      'Do some other processing
      Process()
End Sub

Roger

 

by: craigdevPosted on 2006-12-19 at 04:05:31ID: 18164984

I see now, thanks Sancler will put in practice soon as test this.

Kind regards.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...