Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

Adding controls to a .NET WPF application

Adding controls to a .NET WPF application

The Visual Studio IDE contains powerful tools to enable you to create .NET applications quickly and effectively, and in this article we will use the tools to create a simple WPF application. But at runtime you may need to modify the contents of the user interface so we will also demonstrate how you can add buttons and respond to button click events on the added buttons.

Let's get started. First, start the New -> Project... Dialog. Select COBOL -> Managed -> WPF Application and press OK. At this point a new WPF project is created and the graphical editor is started for the application. You should see the editor for Window1.xaml.

Open the Toolbox window (if it is not visible go to the View menu and select Toolbox), select StackPanel and drag it onto Window1. Resize it to the size of the window. The StackPanel object will control the layout of the window.

Next, select Button and drag it onto Window1. You should be able to see a Properties window for the button. If it is not visible you can enable it from the View menu. Change the Content property to 'Add another button'. This changes the button text. You may need to resize the button to be able to see all of the text.

Now we are going to write the code that dynamically updates the window. Double-click the mouse on the button. You will see that you are taken into a COBOL program called Window1.xaml.cbl. This is the code-behind file for the Window1.xaml and contains the event handlers for the controls being used. You will see that an event handler method called button1_Click has been added. Add a local-storage section for the method and include a definition for a new button as follows :

       local-storage section.
       01 newButton type Button.

Next, add code to the procedure division of button1_Click to create the new button, set its text and add it to the window :

           set newButton to new Button()
           add 1 to counter
           set newButton::Content to addedButtonText
           invoke stackPanel1::Children::Add(newButton)

This code uses addedButtonText and counter to give each of the added buttons a unique name. We define them in the working-storage section of the class as follows :

       01 addedButtonText.
           03 filler pic x(11) value "AddedButton".
           03 counter pic 99 value 0.

If you now build and run the application, when you click the 'Add another button' a second button will be displayed, illustrating that you have successfully modified the window at run-time. However, that is not especially useful by itself so next we will add an event handler for the added button. Add the following code to the class :

       method-id newButton_Click final private.
       01 theButton type Button.
       01 msgText type String.
       procedure division using by value sender as object 

e as type System.Windows.RoutedEventArgs.
           if sender is instance of type Button
               set theButton to sender as type Button
               set msgText to theButton::Content as string
           else
               set msgText to "Unknown"
           end-if
           invoke type MessageBox::Show("Added button clicked"msgText)
       end method.

This provides a button-click handler which displays a message box. To associate the handler with the button we need to add the following line to button1_Click immediately after the line where newButton::Content is set

           invoke newButton::add_Click(new RoutedEventHandler(self::newButton_Click))

The complete class should now look like this :

       class-id WPFApplication1.Window1 is partial
                 inherits type System.Windows.Window.

       working-storage section.
       01 addedButtonText.
           03 filler pic x(11) value "AddedButton".
           03 counter pic 99 value 0.
           
       method-id NEW.
       procedure division.
           invoke self::InitializeComponent()
           goback.
       end method.

       method-id button1_Click final private.
       local-storage section.
       01 newButton type Button.
       procedure division using by value sender as object e as type System.Windows.RoutedEventArgs.
           set newButton to new Button()
           add 1 to counter
           set newButton::Content to addedButtonText
           invoke newButton::add_Click(new RoutedEventHandler(self::newButton_Click))
           invoke stackPanel1::Children::Add(newButton)
       end method.
       
       method-id newButton_Click final private.
       01 theButton type Button.
       01 msgText type String.
       procedure division using by value sender as object e as type System.Windows.RoutedEventArgs.
           if sender is instance of type Button
               set theButton to sender as type Button
               set msgText to theButton::Content as string
           else
               set msgText to "Unknown"
           end-if
           invoke type MessageBox::Show("Added button clicked"msgText)
       end method.
       
       end class.

 If you now rebuild and run the application, whenever you click on one of the added buttons a message box will be displayed with the title bar showing the button clicked.

This is a relatively simple sample using buttons but the same principle can be applied to allow any control to be added, removed or modified during the execution of the application.

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Comments

Great example, thanks!

Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2012-06-21 16:58
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.