Results 1 to 26 of 26

Thread: instantiating unknown form name

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    instantiating unknown form name

    Hi,

    Is there any way of creating an instance of a form when you do not know the name of the base form (if that is the correct term) until it is selected at runtime? I.E. Can you let the user select the base form and then store it in a variable which you can then use to create the instance?

    We used to be able to do this easily in DOS programmes using a process called "Macro Substitution". I take it this is not available in VB.NET?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Guys,

    Any ideas please?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I'm not sure if this is what you mean

    Activator.CreateInstanceOf

    Seems to me you have to have some idea of form to call, either by a user-friendly name selected by the user, which correlates to a base form, and then you can use reflection with the statement above.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi nemaroller,

    I have not got my head round Reflections yet, but have looked it up in my books. I can't see how to use it to do what I want. I explain:

    My application gives the user a choice of 10 actions using Radio Buttons. Each action requires the instancing of a different form. Currently my code has a seperate event responding to the click event of each radiobutton. that code is

    VB Code:
    1. frmtemp=New frm?????  '????? being the form required
    2. frmtemp.show

    therefore I have 10 separate events.

    What I am trying to do is have one event handling all 10 radiobutton clicks with code something like

    [Highlight=VB]
    frmtemp=new xxxx
    frmtemp.show
    [/vbcode

    This time the xxxx would be the correct equivalent of

    sender.text

    if possible

    frmtemp has been declared in the Module as

    Public frmtemp as Form

    Maybe this is just not possible, but it was very easy in DOS. (As was printing; but look at that under .NET
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    How about a handler that uses Select Case which handles all radiobutton events , but you still have to declare the all the forms and after that , instantiate one of them depending on which radiobutton is checked . BTW , there's a way to do what you want in .NET but I can't remember where I've seen it !

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I wrote something like this, and actually i think Edneesis helped me on this about 6 months ago... or maybe not.

    Because of the distributed nature of the app i was working on, we had the names of our forms in a database. So this main page would make a call, and get the actual FORM name, its user-friendly title, and a few other things(it was a completely dynamically run/customizable app, hence the database calls).

    We would populate a repeater-ish control, so when they clicked on a value, say for instance, "Employees", I already had the name "employeeFormMain.frm" associated with it in the control.

    You should be able to accomplish the same thing with a radiobuttonlist... but anyway... once you have the name (String) of the form class...
    it went something like:
    VB Code:
    1. Dim tempForm As Windows.Form
    2. tempForm = Activator.CreateInstanceOf(theselectedformname,yourassembly...)
    3. tempForm.Show

    I could give you the exact code later on today, once I get to work and can access the powervault.

    Really, comparing this with Pirate's method is banter at best. Declaring a reference to hold a possible form value, doesn't use any substantial resource, until you instantiate it.

    The reflection method I'm describing just looks cooler, and it probably isn't as lean as Pirates (because mine has to make a reflection call), then again, its no resource hog either.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Thanks guys.

    Pirate,
    "How about a handler that uses Select Case which handles all radiobutton events , but you still have to declare the all the forms and after that , instantiate one of them depending on which radiobutton is checked "


    You don't have to declare all the forms.

    In the module

    Public frmTemp as Form



    In the Event

    frmTemp= New fclsFormClassName

    works OK.
    Last edited by taxes; Mar 30th, 2004 at 09:12 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I meant , declare only one variable of form class , then in your method instantiate it .Something like this :

    In a module or class
    VB Code:
    1. Dim frm As Form

    in the method :
    VB Code:
    1. frm = New Form5
    sry for that confusion

  9. #9
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    Ok.. I hope you folks are still around.

    I'm attempting to do something very similar to what's being discussed here. I've got a number of strongly typed Crystal reports that employ the CR 'Push' model. The setup database loads all of these User Friendly report names and the related object name into arrays.

    When the user selects a report from a list I need to be able to instantiate a new copy of the proper report and bind the current dataset to it.

    Currently I have an event handler that looks similar to the following:

    Code:
    Select Case RptID
      Case 0
         Dim MyRpt as New CrRpt200a
      Case.....
    End Select
    Where the object referenced matches the 'selection' the User made from a list. What I'd like to do, instead, is eliminate the Case statement and have a couple of lines that instantiate whatever is needed based on the arrays that were setup during the Load event.

    Something like:

    Code:
    Dim RptStr as String = RptNameStr(RptList.SelectedIndex)
    Dim MyRpt As New BlankRpt    'A dummy Blank Crystal Report
    MyRpt = Activator.CreateInstanceOf(RptStr)
    I know I'm missing something... but if this can work it would eliminate literally hundreds (maybe thousands) of lines of code. I'd greatly appreciate any light you folks can shed on this topic.

    Thank you.

    -Bill

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    You can use the CallByName function to call any Method or property using the name contained in a string.

    Unless someone else knows the precise answer, you will have to experiment with this function.


    (You could have done this very easily in dBaseIII plus using macro substitution, but you did not need to. )

    Alternatively, is there any way of creating VB.NET code during runtime?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  11. #11
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    Taxes -

    Thanks, sounds like you're as much an "xBASE" veteran as I am!

    Maybe I don't understand enough but is:

    Dim x As New SomeSortOfObject

    Something that the 'CallByName' function can be utilized for?

    It works like a champ to associate a Function/Subroutine with a User choice and I'm rapidly reducing code in several applications using it!

    Can you give me an example of using it to instantiate a new 'Form' from a variable?

    For instance if you were creating an instance of a form:

    Dim MyForm As New Form1

    and the variable 'SelForm' contained "Form1"

    How would you use the CallByName function to accomplish instantiation of 'MyForm'?

    I really miss "Macro Substitution" as it's still very much avile and well even in Visual FoxPro 8!

    Thanks again.

    -Bill

    Web Site

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Yes, I moved up to dBase III plus in 1992, having decided that dBase iv had far too many bugs and Visual Dbase was even worse. So I stayed with III plus until 2002 because the bigger, faster computers were more than III plus could handle and I was having to reboot 4 or 5 times a day. I still use III plus on a 300 meg laptop running Windows 98. You can create small data based programmes far more quickly than you can in VB.NET.

    I have used the CallByName function many times but I believe it can only be used to call Methods or Properties. Creating an instance of a form is neither of those.

    I suppose if you used a separate procedure to instantiate each of the forms in your application and call as necessary having previously reserved an appropriate variable, then using CallByName to call that procedure will cut down your code as compared with using Select Case.

    Did you notice nemaroller's post? He suggested Activator.CreateInstance and the MSDN Help reference seems to suggest that would work, but I can't get my mind round it at the moment.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  13. #13
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Hi Taxes, try to have a look at this link. I have not tried to apply its suggestions, but if you want to test it......
    I hope it was quite similar at what you need!

    http://www.vbforums.com/showthread.p...hreadid=291544

    In the next days I will have to turn back my head on 8051 assembler. because we need to implement a new machinary control. I don't remember almost anything

    Pray for me, my friend!

    Live long and prosper (Mr. Spock)

  14. #14
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    Taxes, Alextyx...

    Thanks for your insights and thoughts about this. That thread Alextyx posted looks like it has the foundation of a solution to my problem.

    I'm really trying to instantiate reports (Crystal) instead of forms but I think the concept is right on target.

    Again, thanks to you both and when I get this working I'll post a code summary to let you know how it works.

    -Bill

    CP.Solutions

  15. #15
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    This definitely works! The code segment below illustrates instantiating a Crystal Report using a variable to reference the object you want to use.

    Code:
    ' ----------------------------------------------------------
    ' In my actual code RptID is a reference to the appropriate 
    ' array item that  contains the proper Report (reference) names
    RptID = 3
    ' Get the application name and add the 'dot' - This can be done as a public during init
    Dim appName As String = Application.ProductName & "."
    ' Testing--- this string will be pulled from the array I mentioned
    Dim strRptName As String = "CrRpt" & CStr(RptID)
    ' This is the key line for reports... 
    ' and It should all be on one line in code
    Dim MyRpt As CrystalDecisions.CrystalReports.Engine.ReportClass = DirectCast(Type.GetType(appName & strRptName).InvokeMember(strRptName, Reflection.BindingFlags.CreateInstance, Nothing, Nothing, Nothing), CrystalDecisions.CrystalReports.Engine.ReportClass)
     
    ' Then, use the report object which contains "{UGReports.CrRpt3}" as it's class 
    ' as you normally would!! 
    MyRpt.SetDataSource(MyDataSet)
    CRViewer1.ReportSource = MyRpt
    CRViewer1.Show()
    ' ----------------------------------------------------------
    This works, and slick as can be.

    Thanks again to Alextyx for that thread link, and Taxes for his insight as well.

    -Bill

    CP.Solutions

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Guys,

    That's great.

    alextyx, how on earth did you dig that one out? The thread title does not indicate what it can do.

    Thanks billcoupe for rejuvenating this thread. That is just what we were both looking for.

    Great stuff and many thanks to dynamic_sysop.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  17. #17
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78
    Taxes -

    Between this and the CallByName function a lot of the Macro Sustitution issue is covered! While there are a lot of things about VFP I miss in .Net that was the one I missed most!

    This forum has a great bunch of folks... I wish I had a dollar for all the other places I've asked this same question and been told "it's not possible in VB.Net"... Seems the more I dig around, the more *is* possible in .Net!

    This place id definitely my new 'tech info' home!

    I'll keep you posted if I find additional ways to emulate the "&" feature.

  18. #18

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi billcoupe,

    I agree entirely. It is going to cut down a lot of coding. I've been a member of this forum for 9 months and have learned a lot. There are some misunderstandings but not many.

    Talk to you again sometime.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  19. #19
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Good morning Friends (09.34 AM local time ). I'm happy to be useful (it doesn't happen often ).
    The trick, dear Taxes, is that I don't believe too much in searching by a key, so when I look to a thread that I think could be useful later, I put it into favorites, that I have organised with folders, sub folders and so on. Obviously, only the most interesting are saved and when I have applied their techniques and included them in my apps, I can delete them. So that thread is one of my private collection!
    Good sunday, friends!
    Live long and prosper (Mr. Spock)

  20. #20
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by alextyx
    Good morning Friends (09.34 AM local time ). I'm happy to be useful (it doesn't happen often ).
    The trick, dear Taxes, is that I don't believe too much in searching by a key, so when I look to a thread that I think could be useful later, I put it into favorites, that I have organised with folders, sub folders and so on. Obviously, only the most interesting are saved and when I have applied their techniques and included them in my apps, I can delete them. So that thread is one of my private collection!
    Good sunday, friends!
    Will miracles never cease? Alextyx, mucho danke for that link. I'm in the middle of developing an app where I needed that kind of functionality (the menu is database driven) and the code in that link was spot on for getting me the results.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  21. #21
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Happy to be useful....it doesn't happen frequently!
    Live long and prosper (Mr. Spock)

  22. #22
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Happy to be useful....it doesn't happen frequently!
    Live long and prosper (Mr. Spock)

  23. #23
    New Member
    Join Date
    Nov 2006
    Posts
    2

    Re: instantiating unknown form name

    There is a more elegant way to do what u want guys!

    Use Assembly Class of System.Reflection Namespace to dig all the classes in the assembly

    Store all types in a hashtable using its fullname as its key

    Create a Function that will Create an Object depending on which ObjectType is selected using ConstructorInfo:

    For more details , check on the attachment.
    Attached Files Attached Files

  24. #24
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: instantiating unknown form name

    This thread is 2 years old , thats an interesting first post you have made.

    Welcome.

  25. #25
    Lively Member
    Join Date
    Sep 2004
    Location
    Burlington, North Carolina
    Posts
    78

    Re: instantiating unknown form name

    Wow.. it has been 2 years since I was first here... and it's been almost that long since I've worked in .Net!! I took a 6 week VFP contract in November of 2004 and I'm still on that gig.

    I've been doing some DNN development, and recently a Java web application... believe it or not, I'm looking forward to getting back to VB!

    Thanks for waking this thread up ramnujs!

  26. #26
    New Member
    Join Date
    Nov 2006
    Posts
    2

    Re: instantiating unknown form name

    sorry guys , it was then i realized that this topic is 2 years old after i have clicked submit button. I just joined the forum and i was very excited to make some reply...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width