Passing variables to a form

Passing variables to a form

Anonymous
Not applicable
774 Views
5 Replies
Message 1 of 6

Passing variables to a form

Anonymous
Not applicable

New to the Revit API so please excuse what is probably a very simple task.

 

How do I pass a variable from the current Revit session to a form. For example, if I wanted to pass the current Revit Version to a label that would show up in a form.

 

I can extract the version into a variable that I can display in a taskdialog:

 

  string verNum = app.VersionNumber;
TaskDialog.Show("Version Number", verNum);

 

but I can't seem to pass the to a form

0 Likes
Accepted solutions (1)
775 Views
5 Replies
Replies (5)
Message 2 of 6

rosalesduquej
Alumni
Alumni

Hi Tbh, 

 

Can this be solve by just making the variable you are assigning the Revit Version a public variable, so you can access it from the class you are creating to do your form?

 

public string verNum;

verNum = app.VersionNumber;

 

A little info about public variables below.

 

Public variables can be used in any procedures in the project. If a public variable is declared in a standard module or a class module, it can also be used in any projects that reference the project where the public variable is declared.

 

https://msdn.microsoft.com/en-us/library/office/gg264241.aspx?f=255&MSPPError=-2147217396 

 

Is this what you meant ? Cheers and hope everything goes well. 

 

Thanks,



Jaime Rosales D.
Sr. Developer Consultant
Twitter | AEC ADN DevBlog
Message 3 of 6

Revitalizer
Advisor
Advisor

Hi,

 

you could pass the variable as a parameter in your Form's constructor.

Also, you could add some SetWhatEver(yourParameter) methods to your Form class to modify it after creation.

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





Message 4 of 6

Aaron.Lu
Autodesk
Autodesk
There are a lot of ways to do that:
1. pass the parameters with the Form constructor, e.g.
public MyWinForm(string verion):Form(){}
2. pass via form properties, e.g.
MyWinForm form = new MyWinForm();
form.RvtVersion = "xxx";
3. use static members outside of the form, e.g.
public MyCommand: IExternalCommand
{
public static string Version;
public Result Exeucte(...)
{
Version = ...;
}
}


Aaron Lu
Developer Technical Services
Autodesk Developer Network
Message 5 of 6

Anonymous
Not applicable
Accepted solution

your comman class, creat a public static variable

 

E.G 

namespace MyCommand{

 

           public class Command : IExternalCommand
           {
                     public static string LabelName;

                      ....................

                     ...................

                     public Result Execute(
                     ExternalCommandData commandData,
                     ref string message,
                     ElementSet elements)
                     {

                            ..............

                            .............

                     }

            }

}

 

 

then in your form.cs when you want to call the string use

 

label1.text = MyCommand.Command.LabelName;

 

hope this helps

           

Message 6 of 6

Anonymous
Not applicable

That turned out to be the issue. I wasn't making my variable public. I ended up creating a subclass that generated a public variable and passed it to the label. Made sense once I figured out the problem.

 

Got my first 'useable' addin complete. Nice to get back into the programming side after so many years away.

 

Appreciate everyones feedback.

0 Likes