Simple Form Example

Simple Form Example

62BJW
Advocate Advocate
2,663 Views
3 Replies
Message 1 of 4

Simple Form Example

62BJW
Advocate
Advocate

Can anyone point me to a very basic/simple example of how to use a windows form? I'm assuming that I call the form from my main execute and place the code that performs a task in the button_click event of the form? Sounds simple enough but I'm having problems. I've done this years ago in VB but I'm still a rookie when it comes to C#. Getting there slowly but surely.

 

Thanks

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

jeremytammik
Autodesk
Autodesk

Dear Bernie,

 

Thank you for your query.

 

Here is one that I am especially fond of, since it creates the .NET Windows form programmatically from scratch on the fly, making it easier (possible at all) to see and understand every single step that is going on:

 

https://thebuildingcoder.typepad.com/blog/2012/05/the-schedule-api-and-access-to-schedule-data.html

 

It does not handle any click events, though, because all the user can do with it is look at it and then close it.

 

Here is a completely different WPF sample that might help better understand how to handle click events:

 

https://thebuildingcoder.typepad.com/blog/2012/06/devcamp-day-two.html#22

 

I hope this helps.

 

Best regards,

 

Jeremy

 



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

Message 3 of 4

stever66
Advisor
Advisor

Here is a simple example using the Macro Editor with C#. 

 

It should be farily easy to convert to an Add-in by adding the typical code an Add-in starts with (the execute method and such). 

 

You do need to add references to Windows.System.Forms, and System.Drawing.

 

I'm sure its not the most elegant example of programming, but it will give you the basic idea.

 

If you need a particular variable to be available both within and outside your form, you have two options, and they are both included here. 

1 - You can declare a public static variable like "myreturn", "mystring", or even the "textbox1", or

2 - You can pass the variable to the form like the "acomment" and "anothercomment".

 

Hope this helps.

 

 

/*
 * Created by SharpDevelop.
 * 
 * 
 * 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.Windows.Forms;
// must go to Project menu, Add Reference, and add System.Windows.Forms;
// also add reference to System.Drawing


namespace CreateForm
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("2198EEA5-CF10-430C-BEC8-BB688B82BA16")]
    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
        
        //static variables can be declared here so they will be availble within and outside of the form:
        public static string myreturn = "";
        public static string mystring = "";
        public static System.Windows.Forms.TextBox textbox1 = new System.Windows.Forms.TextBox();


        public void CreateForm()
        {
            //define some variables to pass to the form:
            //these would normally be set with some more code
            String acomment = null;
            String anothercomment = null;
               
            //code to run before the dialog box goes here
            
            System.Windows.Forms.Form myForm = new form1(acomment, anothercomment );
            myForm.ShowDialog();

            //code to run after the dialog box goes here
        }
        
        public class form1 : System.Windows.Forms.Form
        {
            public form1(string acomment2, string anothercomment2)
            {
                this.Text = "My Form";
                this.Width = 375;
                this.Height = 275;

                textbox1.Multiline = true;
                textbox1.WordWrap = true;
                 //textbox1.ScrollBars = ScrollBars.Vertical;
                textbox1.Location = new System.Drawing.Point(25100);
                textbox1.Width = 300;
                textbox1.Height = 75;
                textbox1.Name = "mytextbox";
                textbox1.Anchor = (AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom);
                //setup the keynote comment heading label:
                Label label1 = new Label();
                label1.Text = "Enter Something: ";
                label1.Location = new System.Drawing.Point(2570);
                label1.Height = 15;
                label1.Width = 225;

                //setup a label:
                Label label2 = new Label();
                label2.Text = "Some text.";
                label2.Location = new System.Drawing.Point(2520);
                label2.Height = 25;
                label2.Width = 150;              

                //setup the "OK" button 
                Button button1 = new Button();
                button1.Text = "OK";
                button1.Location = new System.Drawing.Point(35200);
                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(135200);
                button2.Anchor = (AnchorStyles.Bottom);
                button2.Click += new System.EventHandler(button2_Click);           

                this.CancelButton = button2;
                // Set the start position of the form to the center of the screen.
                this.StartPosition = FormStartPosition.CenterScreen;

                // Add the controls to the form.
                this.Controls.Add(label1);
                this.Controls.Add(label2);
             

                this.Controls.Add(button1);
                this.Controls.Add(button2);
                

                this.Controls.Add(textbox1);
            }  //end public form1

            // Event Handler for the OK button 
            private void button1_Click(object sender, System.EventArgs e)
            {
                //MessageBox.Show("Button 1");
                
                //do something here
                TaskDialog.Show("Revit""You entered: " + "\n\r" +"\n\r" + textbox1.Text.ToString());
                this.Close();

            }

            //  EVent Handler for the Cancel Button 
            private void button2_Click(object sender, System.EventArgs e)
            {
                myreturn = "Cancel";
                //MessageBox.Show("Button 2");
                TaskDialog.Show("Revit""You Pressed Cancel");
                this.Close();
            }

           
               }  //end class form1

    }  //end public partial class This Application
}  // end namespace

 

 

 

  

 

 

Message 4 of 4

lionel.kai
Advisor
Advisor

@stever66 This is also a helpful line to add (by CancelButton): 

this.AcceptButton = button1;

 


Lionel J. Camara
BIM Manager at KAI Hawaii, Inc. - Structural and Forensic Engineers
Autodesk Certified Professional
0 Likes