Category list in Windows Form Checked List Box

Category list in Windows Form Checked List Box

SwainStrain
Enthusiast Enthusiast
1,117 Views
2 Replies
Message 1 of 3

Category list in Windows Form Checked List Box

SwainStrain
Enthusiast
Enthusiast

Hi everybody,

 

i'm searching for a solution to my problem. I'm trying to add all the Revit Categories to a Checked List Box in a Windows Form in order to select them and obtain the selected categories from the form.

Unfortunately, i'm stuck at the creation of the list in the box.

 

Here is the FORM code:

 

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace LP_Parameters.Commands
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        //Revit references
        public UIApplication rvtUiApp;
        public UIDocument rvtUiDoc;
        public Autodesk.Revit.ApplicationServices.Application rvtApp;

        public Form1()
        {
            InitializeComponent();
        }       
        
        public Form1(ExternalCommandData commandData)
        {
            //Revit references
            rvtUiApp = commandData.Application;
            rvtUiDoc = rvtUiApp.ActiveUIDocument;
            rvtApp = rvtUiApp.Application;
            InitializeComponent();
        }

        private void checkedListBox1_Load(object sender, EventArgs e)
        {

            Document rvtDoc = rvtUiDoc.Document;
            Categories cat = rvtDoc.Settings.Categories;
            SortedList<string, Category> myCategories = new SortedList<string, Category>();
            myCategories.Clear();

            foreach (Category c in cat)
            {
                if (c.AllowsBoundParameters)
                    myCategories.Add(c.Name, c);
            }

            checkedListBox1.DataSource = myCategories;
            checkedListBox1.DisplayMember = "Name";
        }

        private void OKbtn_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            Close();
        }

        private void Cancelbtn_Click(object sender, EventArgs e)
        {
            Close();
        }                
    }
}

 

The error when i run it in Revit is the following:

 

2020-11-21 12_09_17-New Message - Autodesk Community.png

 

2020-11-21 12_11_03-New Message - Autodesk Community.png

 

It seems that the error is at line 39, where i define the rvtDoc variable:

 

2020-11-21 12_12_04-LP Parameters - Microsoft Visual Studio (Administrator).png

 

Any idea about what am i doing wrong?

 

Thank you very much to everybody, you are always really helpful!

0 Likes
Accepted solutions (2)
1,118 Views
2 Replies
Replies (2)
Message 2 of 3

jeremytammik
Autodesk
Autodesk
Accepted solution

You cannot access the Revit API at all except from within a valid Revit API context:

 

https://thebuildingcoder.typepad.com/blog/2020/11/document-session-id-api-context-and-external-event... 

 

Such a context is provided within the event handler of a Revit API callback method, and nowhere else.

 

The most common example of such an event handler is the external command Execute method.

 

Please work through the Revit API getting started material first of all, to understand the basic architecture of a Revit add-in:

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#2

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 3

SwainStrain
Enthusiast
Enthusiast
Accepted solution

Thank you very much Jeremy,

 

I followed your instructions at the following link:

 

https://thebuildingcoder.typepad.com/blog/2014/03/using-generic-collections-with-filters-and-forms.h... 

 

It was really useful, i corrected a few errors in my script and learned a lot! 

 

The problem was related to the list of categories, i modified the script as follows and it worked perfectly fine:

 

    public partial class Form1 : Form
    {
        Document _doc;               

        public Form1(Document doc)
        {
            InitializeComponent();
            _doc = doc;

            //Categories

            Categories cat = _doc.Settings.Categories;
            List<String> myCategories = new List<String>();
            foreach (Category c in cat)
            {
                if (c.AllowsBoundParameters)
                    myCategories.Add(c.Name);
            }

            myCategories.Sort();
            checkedListBox1.DataSource = myCategories;
            checkedListBox1.DisplayMember = "Name";

 

Thank you very much again!

0 Likes