Windows Form in C# Macros
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to get a Csharp Macro to work in Revit 2016. (I'm using SharpDevelop). I got it to work with
Python in SharpDevelop, but I'm having a hard time with C#. I'm a beginner at this, so I'm probably doing something simple wrong.
Anyhow, I just want to get a form working that has three buttons - "OK", "Apply", and "Cancel". This will actually compile, but it won't load into the Revit Macros, so I can't run it. I have a feeling I have something wrong in the red text, but I'm not sure. Here is the code: (sorry, but the formatting got all wacked when I pasted it in. Hope i have it fixed.)
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
namespace myEditorC { [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("0D7A17A2-B354-40C8-8218-D383A0E62D86")]
public partial class form1 : System.Windows.Forms.Form
{
public void Form1()
{
this.Text = "My Editor";
this.Width = 375;
this.Height = 275;
//setup the "OK" button
Button button1 = new Button ();
button1.Text = "OK";
button1.Location = new System.Drawing.Point(35, 200);
button1.Click += new System.EventHandler(button1_Click);
//setup the "Cancel" button
Button button2 = new Button();
button2.Text = "Cancel";
button2.Location = new System.Drawing.Point(135, 200);
button2.Click += new System.EventHandler(button2_Click);
//setup the "Apply" button
Button button3 = new Button();
button3.Text = "Apply";
button3.Location = new System.Drawing.Point(235, 200);
button3.Click += new System.EventHandler(button3_Click);
// Set the accept button of the form to button1.
this.AcceptButton = button1;
// Set the cancel button of the form to button2.
this.CancelButton = button2;
// Set the start position of the form to the center of the screen.
this.StartPosition = FormStartPosition.CenterScreen;
Controls.Add(button1);
Controls.Add(button2);
Controls.Add(button3);
} // end Form1
private void button1_Click(object sender, System.EventArgs e)
{
//myreturn = "OK";
}
private void button2_Click(object sender, System.EventArgs e)
{
//myreturn = "Cancel";
}
private void button3_Click(object sender, System.EventArgs e)
{
//myreturn = "Apply";
}
} // end partial classs
// start of SharpDevelop Generated macros code
public partial class ThisApplication
{
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
//end of SharpDevelop Generated Macros Code
public void myEditor()
{
UIDocument uidoc = ActiveUIDocument;
Document doc = ActiveUIDocument.Document;
Autodesk.Revit.DB.View pView = ActiveUIDocument.Document.ActiveView;Autodesk.Revit.DB.Transaction
t = new Autodesk.Revit.DB.Transaction(ActiveUIDocument.Document, "Form");
t.Start();
try
{
// Create a new instance of the form.
System.Windows.Forms.Form myForm = new form1();
myForm.ShowDialog();
}// end try
catch
{
TaskDialog.Show("Revit C# Error", "Error");
}// end catch
t.Commit();
}// end myEditor
} // end class thisapplication
}//end namespace