General Question: Userforms, Modules and passing varibales between them

General Question: Userforms, Modules and passing varibales between them

CadUser46
Collaborator Collaborator
1,247 Views
5 Replies
Message 1 of 6

General Question: Userforms, Modules and passing varibales between them

CadUser46
Collaborator
Collaborator

This is probably going to sound like a dumb question to the computer science experts but i have been wondering this for a while.  Apologies if its not specific enough to be on the Autodesk forum.

 

When you have a module that does some work and you need to launch a userform to make some selections, do you pass those varibles back into the module to continue doing work or do you execute the work under the 'commandbutton_click'?

 

If you pass them back to the module, how do you achieve this?  Global Variables or can you just address them directly in the module? Pro's/cons?

 

I have always done this under the commandbutton click event because i didnt know any better but i find it fragments the steps of the workflow and i think i could keep better track of what going in if i could retrieve the form variables from within the module.


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro
0 Likes
Accepted solutions (1)
1,248 Views
5 Replies
Replies (5)
Message 2 of 6

pball
Mentor
Mentor

I almost always put all of the code for a script under the Form. Since most of my form based scripts open the form, you make selections and enter information on the form, and finally hit a button that makes the script run. So I have a Module that simply opens the form and the rest of the code is run when you hit the save/continue/etc button.

 

The only exception to this is for scripts that have optional forms. What I mean by that is it may or may not present a form to the user for additional input. One example is a script I have that if the selected part has a drawing it will open that drawing and no form is needed. However if that part does not have a drawing a form will appear and ask the user what size and style of drawing template do they want to use. That means the form is loaded halfway through the script.

 

As to passing values from the form and the main module there are probably a few ways. One way is to load the form and then show it in the module code and when the user is done with the form and selects ok/continue/etc simply hide the form instead of closing it. Then you can access all the items on the form from the module. For instance you could access Form_Name.textbox1.value from the module if you had the form hidden. Just remember to unload the form when you are done with it.

 

I also know there is a way to pass parameters from a module to a form using a subroutine, but I've only done that once I think.

 

I hope this is a helpful reply.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 3 of 6

philippe.leefsma
Alumni
Alumni

It's not clear to me what you mean by "I have always done this under the commandbutton click event" , but yes passing variables from a module to a form seems to me like the best option. Keep in mind that global variables are pretty much considered as a poor design solution in nearly any programming language, especially .Net would allow you much cleaner approaches.

 

What I would recommend is to pass a single object from the module to the form that performs selection. This object will be a class that contains all the required parameters, for example Faces and some values. If at some point you need to pass some extra parameters to the form, let's say an Edge, you will just have to add a new member to that class and the impact of adding parameters on your overall design will be minimal.

 

Hope that helps,

Philippe.



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 4 of 6

CadUser46
Collaborator
Collaborator

What i meant was my knowledge level up to this point i have only ever dealt with the code behind forms from within the form/button declarations.  Additionaly i read recently that you cannot pass anything into a form ie i cant add anything to the () of userform_initialize like i would for a sub or function.

The project im working on seems to need an increasing number of public variables within the userform code even though i try to pass whatever i can between subs/functions.  I am trying as much as possible to be diligent about cleaning them up as i exit out of the procedures.

 

Can you suggest any good reading on the subject or perhaps and example of how you would pass this class object to a form?

 

How would i pass, lets say the MyXL=createobject(excel.application) object into the userform so i can retrieve values at multiple points within the userform code?  I have about 20 other things that i need to manipulate at varying stages within the userform exceution and i will freely admit that i dont understand how i would achieve this without some public dims.


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro
0 Likes
Message 5 of 6

philippe.leefsma
Alumni
Alumni
Accepted solution

You can easily pass any variable you want to the constructor of the form itself, when you create the form object, before displaying it. I'm not talking about the initialize method, which is called internally by the constructor.

 

You can also create your own methods in the form class, or read/write properties that allows you to exchange any info you may need between the form and the caller. Inside the form class, create members private or public, depends on your needs, that will store the values you pass in, so they can be used later on by the form methods.

 

Here is some pseudo code, I hope that makes sense:

 

MyForm f = new MyForm(myStuff);

f.Show();

or

MyForm f = new MyForm(); //default ctor

f.SetStuff(myStuff);
f.StuffSomeMore(someMoreStuff);

f.OtherStuff = stillMoreStuffVariable;

f.Show();

 

We don't really support this kind of general-purpose programming questions in our forums, as they are dedicated to Autodesk-API specific questions. So please refrain from posting this kind of topics in the future.

 

I don't have a specific resource to point out, but if you look for some getting started tutorial either on .Net or general design patterns on the web, you should easily find, well, plenty...

 

Regards,

Philippe.

 

 



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 6 of 6

CadUser46
Collaborator
Collaborator

Thanks. I think i know what you mean.

 

I'll accept it as solution so it drops off your list.


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro
0 Likes