Windows Form in C# Macros

Windows Form in C# Macros

stever66
Advisor Advisor
3,000 Views
3 Replies
Message 1 of 4

Windows Form in C# Macros

stever66
Advisor
Advisor

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

 

0 Likes
3,001 Views
3 Replies
Replies (3)
Message 2 of 4

Mustafa.Salaheldin
Collaborator
Collaborator

I've modified the code

 

/*
 * Created by SharpDevelop.
 * User: Mustafa
 * Date: 5/31/2016
 * Time: 7:15 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;

namespace myEditor
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("FA1AC762-0901-4B2B-B90B-803AC26177D6")]
	public partial class ThisDocument
	{
		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 void myEditor()
		{
            Document doc = this.Document;

            Autodesk.Revit.DB.View pView = doc.ActiveView;
            
            Autodesk.Revit.DB.Transaction t = new Autodesk.Revit.DB.Transaction(this.Document, "Form");

            t.Start();

             try
                 {
             		// Create a new instance of the form.

                        Form1 myForm = new Form1();

                        myForm.ShowDialog();

                 }// end try

             catch

                 {                 
                        TaskDialog.Show("Revit C# Error", "Error");

                  }// end catch

             t.Commit();

        }// end myEditor
	}
	
	public partial class Form1 : System.Windows.Forms.Form
	{
		public Form1()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void Form1Load(object sender, EventArgs e)
		{
			
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			
		}
		
		void Button2Click(object sender, EventArgs e)
		{
			
		}
		
		void Button3Click(object sender, EventArgs e)
		{
			
		}
	}
	
		partial class Form1
	{
		/// <summary>
		/// Designer variable used to keep track of non-visual components.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
		
		/// <summary>
		/// Disposes resources used by the form.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}
		
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(62, 19);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(75, 23);
			this.button1.TabIndex = 0;
			this.button1.Text = "ok";
			this.button1.UseCompatibleTextRendering = true;
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.Button1Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(58, 83);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(75, 23);
			this.button2.TabIndex = 1;
			this.button2.Text = "cancel";
			this.button2.UseCompatibleTextRendering = true;
			this.button2.UseVisualStyleBackColor = true;
			this.button2.Click += new System.EventHandler(this.Button2Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(71, 169);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(75, 23);
			this.button3.TabIndex = 2;
			this.button3.Text = "apply";
			this.button3.UseCompatibleTextRendering = true;
			this.button3.UseVisualStyleBackColor = true;
			this.button3.Click += new System.EventHandler(this.Button3Click);
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(284, 262);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1Load);
			this.ResumeLayout(false);
		}
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button1;
	}

}

If it works like a charm the mark this reply as an answer.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

Message 3 of 4

Anonymous
Not applicable

hello ! 

I have a problem with a code in C#, and i have noticed that I have the same with this one ...

 

For this section:

 

  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

 

 

they told me " 'ThisApplication' ne contient pas de définition pour 'Startup' et aucune méthode d'extension 'Startup' acceptant un premier argument de type 'ThisApplication' n'a été trouvée (une directive using ou une référence d'assembly est-elle manquante ?)", so that maybe there is a reference or a using missing ...

can you help me ? 😄

 

 

 ( sorry for my english, I hope i'm clear )

0 Likes
Message 4 of 4

Mustafa.Salaheldin
Collaborator
Collaborator

First: You have to create a separate topic for your issue

Second: What are you tring to do (Revit Macro or Addin)?

Third: I don't speak French.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes