Button labeling

Oct 20, 2016 at 4:37 PM
I am currently writing a script where I have a few inputs and a couple of basic calculations. I have successfully added a button that will execute the script, however, I am having trouble writing text on the button. It appears to be very straightforward from what I have found browsing various forums by using the following command:
calculateButton.Text = "Calculate";
where 'calculateButton' is the name of my button and I would like it to say "Calculate".
This is not working for me. The error I received says:
"'System.Winows.Controls.Button' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.Windows.Controls.Button' could be found (are you missing a using directive or an assembly reference?)"
I have added "using System.Winows.Controls.Button" in the appropriate location to my code but then I receive an error saying: "'System.Winows.Controls.Button' is a type not a namespace."

I have tried another method that requires the namespace 'System.Winows.Forms', however, I have not been able to get that namespace to work either.

Does anyone have a solution for my problem? I am new to Eclipse scripting and C# so there may be an easy fix that I am just unaware of. Thank you in advance for your time!
Oct 20, 2016 at 5:26 PM

There are two main ways of creating UI's using .NET, Windows Presentation Foundation (namespace System.Windows and others) and Windows Forms (namespace System.Windows.Forms). Typically, you don't want to mix the two. I'm assuming you're using WPF because the first error you got mentioned System.Windows.Controls.Button, which is part of WPF.

Since you're using WPF, you'll want to use XAML for designing the UI. I don't recommend setting the button's text as you've done in code. By the way, if you look at the Button class definition, it doesn't have a Text property, but it does have a Content property, so calculateButton.Content = "Calculate" should work. Still, you don't want to do it that way. The preferred way is to do it through XAML:
This will just create the button and set its text to "Calculate", but it won't do anything else. You'll have to wire-up the button's click behavior to a method in your code for it to do anything when the button is clicked. There are a couple of ways to do this. One is by using a Command, and another is by handling the Button's Click event. As a beginner, you'll probably want to handle the Button's Click event (example).

After writing all this, I realized that you may be working with a single-file plug-in. If so, I don't believe you can use XAML (since it would require additional files). You'll have to use code to create, configure, and place the controls inside your Window. If your script has a very simple user interface, this is OK, but as soon as your interface starts to get complex, I suggest moving to a binary plug-in.
Oct 20, 2016 at 5:54 PM
Thank you for the response!

I apologize for the lack of information. As a first time user I am unsure what information is useful for diagnostics. You are correct in that I am using WPF and I am writing a single-file plug-in so XAML does not work for me.

Your suggestion in using calculateButton.Content = "Calculate" worked perfectly. If you do not mind me asking, why do I not want to do it this way? For my intentions this seems to be just fine. Also, I have already written the code to perform all of the functionality for my button and it works, so that is not an issue at this point.
Oct 20, 2016 at 6:28 PM
I'm glad it worked!

For a single-file plug-in, what you're doing is fine since you can't use other files. If you move to a binary plug-in, however, I would recommend applying the MVVM pattern. WPF was designed with this pattern in mind, which separates the "view" (user interface components) from the "model" (data and behavior). This separation not only improves readability but also maintainability of your code.

I think it's much easier to look at a XAML file and understand what the UI looks like, compared to lines of code that set up those controls and their relationships. XAML has a tree-like structure, so it's easier to see the relationships between controls, such as whether a set of buttons is inside a stack panel. XAML also supports the MVVM pattern through data-binding, so that controls are automatically updated (and vice versa) when the data in your model changes.

The downside is that there's a steep learning curve when it comes to WPF and the MVVM pattern. It can be frustrating sometimes. But in the end I believe it's a more manageable way of creating rich Windows apps (and ESAPI scripts) than not using it. If your script does not have a complex user interface or complex interaction with your data model, then coding each control line-by-line is fine.
Oct 20, 2016 at 7:17 PM
I appreciate your great explanation. It sounds like a single-file plug-in is sufficient for my current needs, however, if I move on to more complicated scripts I will be sure to look into a binary plug-in and XAML.

Thank you again for your help!