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.
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.
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.
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:
frmtemp=New frm????? '????? being the form required
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
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.
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 !
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:
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.
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.
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.
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.
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.
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!
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
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.
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.
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.
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.
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!
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.
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!
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...