If you model and simulate behaviors using Enterprise Architect and AMUSE some Mock UIs based on Windows Forms can improve your model significantly.
How you can do that in a simple way? In that case you typically need to:
1. setup an Visual Studio Project and take care of some settings (e.g. usage of 3.5 NET); 2. develop a form; 3. compile the VS project into assembly; 4. import the form into your EA model, AMUSE reverse engineers it from the assembly and create a form class; 5. create an attribute in the class, that contains the state machine of type of reverse engineered form class; 6. connect form events with the state machine triggers; 7. initialize your form at state machine initializing phase
We recommend not import the form class directly but rather to create a wrapper class that contains only the events and methods you really need for modeling, otherwise the reverse engineered form class contains too much stuff inherited from the Form basis class. That means you have basically one more step to do.
The described procedure is not really complex and after performing it once you have a reusable template. In addition you develop and test the UI in very comfortable VS development environment.
But what if the usage of VS is not an option or you just want to perform the whole modeling using EA environment only?
– Not a problem at all . Following XMI contains a simple model including an integrated Windows Form: simple model including an integrated Windows Form
Please find below the corresponding video:
The idea is as simple as the implementation itself. First of all we have to know which .NET assemblies are referenced by AMUSE and therefore are available for behavior programming: System; System.Core; System.Data; System.XML; WindowsBase and most important for us System.Windows.Forms
Following steps are to do:
1. create an attribute e.g. testDialog of type System.Windows.Forms.Form with following initial value: new System.Windows.Forms.Form()
2. create a method e.g. ShaowDialog() responsible for the form initialization. Please find the example initial code below:
//the layout panel is required for positioning of controls we create below System.Windows.Forms.FlowLayoutPanel MyFlowLayoutPanel = new System.Windows.Forms.FlowLayoutPanel(); //ensure that we create controls and add them to the form only once //later on if the form is closed or hidden just show it again if(testDialog.Controls.Count < 1) { testDialog.Text = "Loginform"; System.Windows.Forms.Label NameLabel = new System.Windows.Forms.Label(); NameLabel.Text = "UserName"; MyFlowLayoutPanel.Controls.Add(NameLabel); System.Windows.Forms.TextBox NameBox = new System.Windows.Forms.TextBox(); NameBox.Name = "NameBox"; MyFlowLayoutPanel.Controls.Add(NameBox); System.Windows.Forms.Label PasswordLabel = new System.Windows.Forms.Label(); PasswordLabel.Text = "Pasword"; MyFlowLayoutPanel.Controls.Add(PasswordLabel); System.Windows.Forms.TextBox PasswordBox = new System.Windows.Forms.TextBox(); PasswordBox.Name = "PasswordBox"; PasswordBox.PasswordChar = '*'; MyFlowLayoutPanel.Controls.Add(PasswordBox); System.Windows.Forms.Button CloseButton = new System.Windows.Forms.Button(); CloseButton.Text = "Submit"; MyFlowLayoutPanel.Controls.Add(CloseButton); testDialog.Controls.Add(MyFlowLayoutPanel); //prevent hiding the dialog testDialog.TopMost = true; //connect button click event to the class operation OnSubmit CloseButton.Click += new System.EventHandler(this.OnSubmit); } testDialog.ShowDialog();
3. Create form event handler class operation e.g. OnSubmit(). Please find the example initial code below:
//TODO:: error handling //get the form from event args System.Windows.Forms.Form myForm = ((System.Windows.Forms.Control)sender).FindForm(); //assign the strings from the name and password boxes to corresponding class attributes this.Username = ((System.Windows.Forms.Control)myForm.Controls.Find("NameBox", true)[0]).Text; this.Password = ((System.Windows.Forms.Control)myForm.Controls.Find("PasswordBox", true)[0]).Text; testDialog.Close(); //fire trigger by calling identically named TriggerEvent method trigIdentificationDone();
4. Finally dispose dialog at state machine finalization e.g. implemented in a separate class operation DisposeDialog(). Please find the example initial code below:
testDialog.Close(); testDialog.Dispose();That’s it.
Enjoy!
Hello, dear members of Lieber Lieber,
I’m a Student performing a Master Thesis in Stuttgart University. I’m working with EA and AMUSE and I wish to get deeper into the possibilities of inherit Visual Studio forms. I’ve done succesfully this tutorial, which it’s very good exlplained (thank you very much). But I need to utilize Visual Studio to achieve some goals of my Thesis. Unfortunely I’m not used to reverse engineering in EA. So I’m writting to you to ask some more explanation of the whole procedure with EA, because I don’t understand totally the brief explanation at the beginning of this post. So I’ll be very glad if you could help me or at least recommend me some other tutorial to follow. Thank you very much.
Yours sincerously, Tomas
Hello Thomas,
sounds great, of course we would like to support you.
Probably it could be a good idea to try the beta of the next AMUSE version, which supports on-the-fly generation of Win Forms based on GUI sketches made in EA. Let us discuss what you exactly needs. Tomorrow I’m in Stuttgart and we could meet in the Uni after 4 p.m.