Yes, and now that I think about it, could it have anything to do with declaring the textbox as a "static public textbox" at the start of the code?
If you run the code, select a light fixture first. You can either add the shared parameter in the attached file, or change the code to use a parameter in your light fixtures.
Line 72 is the line I had to add to re-enable the textbox after running code on a light that doesn't have that particular parameter. (It's commented out below, so you should be able to recreate the issue.)
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
//add reference to System.Windows.Forms via Project-Add Reference....
using System.Windows.Forms;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Collections;
namespace Simplified_Edit_Lights
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("897A2706-4C47-45C0-B91E-6F7DB36BD6AE")]
public partial class ThisApplication
{
public static Document doc;
public static Parameter p;
public static System.Windows.Forms.TextBox TextBoxScheduleFix = new System.Windows.Forms.TextBox ();
public static String sScheduleFix;
public static Element elemType;
private void Module_Startup(object sender, EventArgs e)
{
}
private void Module_Shutdown(object sender, EventArgs e)
{
}
#region Revit Macros generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(Module_Startup);
this.Shutdown += new System.EventHandler(Module_Shutdown);
}
#endregion
public class formEditLights : System.Windows.Forms.Form
{
public formEditLights()
{
#region SetUpFormLabels&Textboxes
this.Text = "Edit Lights";
this.Width = 780;
this.Height = 1000;
//set up SCHEDULE FIXTURE Textbox
TextBoxScheduleFix.Location = new System.Drawing.Point(200, 20);
TextBoxScheduleFix.Width = 100;
TextBoxScheduleFix.Height = 20;
TextBoxScheduleFix.Anchor = (AnchorStyles.Left|AnchorStyles.Right|AnchorStyles.Top|AnchorStyles.Bottom);
try
{
p = elemType.get_Parameter(new Guid("e72f1b73-8aa3-4db7-b80f-98d2040c7bd2"));
sScheduleFix = p.AsInteger().ToString ();
TextBoxScheduleFix.Text = sScheduleFix;
//TextBoxScheduleFix.Enabled = true;
}
catch
{
TextBoxScheduleFix.Text = "";
TextBoxScheduleFix.Enabled = false;
}
#endregion
#region FormButtonDefs
//setup the "OK" button
Button button1 = new Button ();
button1.Text = "OK";
button1.Location = new System.Drawing.Point(215, 920);
button1.Anchor = (AnchorStyles.Bottom);
button1.Click += new System.EventHandler(button1_Click);
//setup the "Cancel" button
Button button2 = new Button();
button2.Text = "Cancel";
button2.Location = new System.Drawing.Point(340, 920);
button2.Anchor = (AnchorStyles.Bottom);
button2.Click += new System.EventHandler(button2_Click);
//setup the "Apply" button
Button button3 = new Button();
button3.Text = "Apply";
button3.Location = new System.Drawing.Point(465, 920);
button3.Anchor = (AnchorStyles.Bottom);
button3.Click += new System.EventHandler(button3_Click);
this.CancelButton = button2;
// Set the start position of the form to the center of the screen.
this.StartPosition = FormStartPosition.CenterScreen;
#endregion
// Add the controls to the form.
#region AddFormControls
GroupBox groupbox1 = new GroupBox ();
groupbox1.Text = "General: ";
groupbox1.Location = new System.Drawing.Point(15, 50);
groupbox1.Width = 720;
groupbox1.Height = 390;
groupbox1.Anchor = (AnchorStyles.Left|AnchorStyles.Right|AnchorStyles.Top);
groupbox1.Controls.Add(TextBoxScheduleFix);
this.Controls.Add (groupbox1);
this.Controls.Add(button1);
this.Controls.Add(button2);
this.Controls.Add(button3);
#endregion
} // end public form EditLights
// Event Handler for the OK button
private void button1_Click(object sender, System.EventArgs e)
{
UpdateFixtureParameters();
this.Close();
}
// EVent Handler for the Cancel Button
private void button2_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void button3_Click(object sender, System.EventArgs e)
{
UpdateFixtureParameters();
}
//routine to update any changed parameters
private void UpdateFixtureParameters()
//this is a routine "Edit Lights" uses to update parameters when either the "OK" or "Apply" buttons are pressed.
//Each textbox is compared to the parameter value, and if they are different, the textbox value is written to the parameter
{ using (Transaction trans = new Transaction (doc, "Edit Light Fixture Parameters"))
{
trans.Start();
//Set new vaule for Schedule Fixture
if (TextBoxScheduleFix.Enabled == true && sScheduleFix != TextBoxScheduleFix.Text)
{
//TaskDialog.Show("Revit","Update Schedule Fixt");
int i;
if(int.TryParse(TextBoxScheduleFix.Text, out i))
{
elemType.get_Parameter (new Guid ("e72f1b73-8aa3-4db7-b80f-98d2040c7bd2")).Set(int.Parse(TextBoxScheduleFix.Text));
sScheduleFix = TextBoxScheduleFix.Text;
}
else
{
string msg = @"Can't Set ""Schedule Fixture"" to a non-integer value.";
TaskDialog.Show("Revit Error.", msg);
}
}
trans.Commit();
}
}
}
public void Simplified_Edit_Lights()
{ //Pick a light and run macro
//This edits the Type Parameters for a particular light fixture type (similar to editing in a fixture schedule)
UIDocument uidoc = this.ActiveUIDocument;
doc = this.ActiveUIDocument.Document;
// Get the element selection of current document
Selection selection = uidoc.Selection;
//store element id's
ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();
if (selectedIds.Count == 1)
{
try
{
foreach (ElementId id in selectedIds)
{
Element LightFamilyType = doc.GetElement(id);
//TaskDialog.Show("Element ID", LightFamilyType.Name.ToString());
elemType = doc.GetElement(LightFamilyType.GetTypeId ());
Element et = doc.GetElement(LightFamilyType.GetTypeId());
break;
}
}
catch
{
TaskDialog.Show("Error!!", "Please Select one and only one Light Fixture before running this program.");
return;
}
}
else
{
TaskDialog.Show("Error!!", "Please Select one and only one Light Fixture before running this program.");
return;
}
System.Windows.Forms.Form myForm = new formEditLights ();
myForm.ShowDialog();
}
}
}