Click or drag to resize
Code Example - AreaUnits

Allows the user to convert from Square Meters to various AreaUnits values (i.e. Acres, Square Miles, etc.) and the opposite.

Code Example
Area Units

This section contains selected code files from a Visual Studio project that emphasize specific ArcGIS Runtime SDK (Windows Desktop) features. For example: some code examples may accomplish the bulk of the work as a configuration property in the .xaml file and hence only the .xaml file will be shown in detail below. In other code examples, the .xaml is used to define the configuration of graphical elements for the application but the application logic is performed in the code behind, hence you may see both the .xaml and .cs/.vb files shown in detail below.

XAML
<Window x:Class="AreaUnits.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
    Title="MainWindow" Height="600" Width="800">

    <Grid>

        <!-- TextBlock to provide the instructions on how to use the sample code. It will be populated with instructions in the code-behind when the application loads. -->
        <TextBlock Height="94" HorizontalAlignment="Left" Name="TextBlock_Instructions" VerticalAlignment="Top" Width="770" TextWrapping="Wrap"  Margin="10,2,0,0" />

        <!-- XXX AreaUnits to SquareMeters. -->
        <Grid HorizontalAlignment="Left" Height="230" Margin="10,101,0,0" VerticalAlignment="Top" Width="770" Background="AliceBlue">
            <Button Content="Convert To" HorizontalAlignment="Left" Margin="218,67,0,0" VerticalAlignment="Top" Width="165" x:Name="Button1" Click="Button1_Click" Height="45"/>
            <TextBox HorizontalAlignment="Left" Height="23" Margin="84,92,0,0" TextWrapping="Wrap" Text="100" VerticalAlignment="Top" Width="89" x:Name="TextBox_Input_1"/>
            <TextBox HorizontalAlignment="Left" Height="23" Margin="426,93,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="163" x:Name="TextBox_Output_1" />
            <ComboBox HorizontalAlignment="Left" Margin="426,66,0,0" VerticalAlignment="Top" Width="163" x:Name="ComboBox1"/>
            <Label Content="Square Meters:" HorizontalAlignment="Left" Margin="84,63,0,0" VerticalAlignment="Top" />
        </Grid>

        <!-- Square Meters to XXX AreaUnits.-->
        <Grid HorizontalAlignment="Left" Height="230" Margin="12,331,0,0" VerticalAlignment="Top" Width="770" Background="#FFEDFFED">
            <ComboBox HorizontalAlignment="Left" Margin="95,53,0,0" VerticalAlignment="Top" Width="158" x:Name="Combobox2"/>
            <TextBox HorizontalAlignment="Left" Height="23" Margin="95,81,0,0" TextWrapping="Wrap" Text="100" VerticalAlignment="Top" Width="158" x:Name="TextBox_Input_2"/>
            <Button Content="Convert To" HorizontalAlignment="Left" Margin="289,53,0,0" VerticalAlignment="Top" Width="172" x:Name="Button2" Click="Button2_Click" Height="51"/>
            <Label Content="Square Meters:" HorizontalAlignment="Left" Margin="516,49,0,0" VerticalAlignment="Top" RenderTransformOrigin="1.421,1.423"/>
            <TextBox HorizontalAlignment="Left" Height="23" Margin="512,81,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="98" x:Name="TextBox_Output_2"/>
        </Grid>

    </Grid>

</Window>

SPECIAL NOTE: The XAML displayed above comes from a C# project. If you are a VB.NET developer, you will need to modify the text for the x:Class namespace from "AreaUnits.MainWindow" to be just "MainWindow".

namespace AreaUnits
{
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Add instructions on how to use the sample. 
            TextBlock_Instructions.Text = "The top set of controls (in the light blue Grid) allow you to convert a number (double) from Square Meters to various other " + 
                "area formats (like: Acres, Square Miles, etc.) - simply enter a number in the TextBox on the left, choose a desired output area format from the DropDown, " + 
                "and click the button to see the output result. " + System.Environment.NewLine + 
                "Conversely, the bottom set of controls (in the light green Grid) allow you yo convert a number (double) from various area formats (like: Acres, Square Miles, " + 
                "etc.) to SquareMeters - simply enter a number in the TextBox on the left, choose the desired area format from the DropDown, and click the button to see " + 
                "the output in Square Meters.";

            // SPECIAL NOTE:
            // Because we do not have a Map Control added to this application, we need to manually initialize the ArcGIS Runtime Environment so that we can use the 
            // various Esri.ArcGISRuntime Classes. If a Map Control were part of your application, calling the ArcGISRuntime.Initialize method would not be necessary.
            Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.Initialize();

            // Add all of the various AreaUnits that a user can convert to in the ComboBox.
            ComboBox1.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.Acres.Name);
            ComboBox1.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.Ares.Name);
            ComboBox1.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.Hectares.Name);
            ComboBox1.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareCentimeters.Name);
            ComboBox1.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareDecimeters.Name);
            ComboBox1.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareFeet.Name);
            ComboBox1.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareInches.Name);
            ComboBox1.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareKilometers.Name);
            ComboBox1.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMeters.Name);
            ComboBox1.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMiles.Name);
            ComboBox1.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMillimeters.Name);
            ComboBox1.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareYards.Name);
            ComboBox1.SelectedIndex = 0; // Make the first ComboBox choice active.

            // Add all of the various AreaUnits that a user can convert from in the ComboBox.
            Combobox2.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.Acres.Name);
            Combobox2.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.Ares.Name);
            Combobox2.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.Hectares.Name);
            Combobox2.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareCentimeters.Name);
            Combobox2.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareDecimeters.Name);
            Combobox2.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareFeet.Name);
            Combobox2.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareInches.Name);
            Combobox2.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareKilometers.Name);
            Combobox2.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMeters.Name);
            Combobox2.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMiles.Name);
            Combobox2.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMillimeters.Name);
            Combobox2.Items.Add(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareYards.Name);
            Combobox2.SelectedIndex = 0; // Make the first ComboBox choice active
        }

        private void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Get the AreaUnits value string name.
            string theSelectionText = ComboBox1.SelectedValue.ToString();

            // Get the input Square Meters value.
            double theInputValue = System.Convert.ToDouble(TextBox_Input_1.Text);

            // Create a placeholder output AreaUnits output value from the .ConvertFromSquareMeters Method. 
            double theOutputValue = 0;

            // For each AreaUnits type perform the .ConvertFromSquareMeters calculation.
            if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.Acres.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.Acres.ConvertFromSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.Ares.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.Ares.ConvertFromSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.Hectares.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.Hectares.ConvertFromSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareCentimeters.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareCentimeters.ConvertFromSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareDecimeters.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareDecimeters.ConvertFromSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareFeet.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareFeet.ConvertFromSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareInches.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareInches.ConvertFromSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareKilometers.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareKilometers.ConvertFromSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMeters.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMeters.ConvertFromSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMiles.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMiles.ConvertFromSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMillimeters.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMillimeters.ConvertFromSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareYards.Name)
            {
                // NOTE: Using this long version you can also obtain information on the AreaUnit Class.
                Esri.ArcGISRuntime.Geometry.AreaUnit myUnit_SquareYards = (Esri.ArcGISRuntime.Geometry.AreaUnit)Esri.ArcGISRuntime.Geometry.AreaUnit.Create(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareYards.Id);
                theOutputValue = myUnit_SquareYards.ConvertFromSquareMeters(theInputValue);

                // Additional AreaUnit Properties that you could make use of in another application
                int myAreaUnit_SquareYards_ID = myUnit_SquareYards.Id;
                string myAreaUnit_SquareYards_Name = myUnit_SquareYards.Name;
                Esri.ArcGISRuntime.Geometry.UnitType myAreaUnit_SquareYards_UnitType = myUnit_SquareYards.UnitType;
            }

            // Display the other XXXX AreaUnits values.
            TextBox_Output_1.Text = theOutputValue.ToString();
        }

        private void Button2_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Get the AreaUnits value string name.
            string theSelectionText = Combobox2.SelectedValue.ToString();

            // Get the input AreaUnits value.
            double theInputValue = System.Convert.ToDouble(TextBox_Input_2.Text);

            // Create a placeholder output Square Meters output value from the .ConvertToSquareMeters Method. 
            double theOutputValue = 0;

            // For each AreaUnits type perform the .ConvertToSquareMeters calculation.
            if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.Acres.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.Acres.ConvertToSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.Ares.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.Ares.ConvertToSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.Hectares.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.Hectares.ConvertToSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareCentimeters.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareCentimeters.ConvertToSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareDecimeters.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareDecimeters.ConvertToSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareFeet.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareFeet.ConvertToSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareInches.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareInches.ConvertToSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareKilometers.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareKilometers.ConvertToSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMeters.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMeters.ConvertToSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMiles.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMiles.ConvertToSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMillimeters.Name)
            {
                theOutputValue = Esri.ArcGISRuntime.Geometry.AreaUnits.SquareMillimeters.ConvertToSquareMeters(theInputValue);
            }
            else if (theSelectionText == Esri.ArcGISRuntime.Geometry.AreaUnits.SquareYards.Name)
            {
                // NOTE: Using this long version you can also obtain information on the AreaUnit Class.
                Esri.ArcGISRuntime.Geometry.AreaUnit myUnit_SquareYards = (Esri.ArcGISRuntime.Geometry.AreaUnit)Esri.ArcGISRuntime.Geometry.AreaUnit.Create(Esri.ArcGISRuntime.Geometry.AreaUnits.SquareYards.Id);
                theOutputValue = myUnit_SquareYards.ConvertToSquareMeters(theInputValue);

                // Additional AreaUnit Properties that you could make use of in another application
                int myAreaUnit_SquareYards_ID = myUnit_SquareYards.Id;
                string myAreaUnit_SquareYards_Name = myUnit_SquareYards.Name;
                Esri.ArcGISRuntime.Geometry.UnitType myAreaUnit_SquareYards_UnitType = myUnit_SquareYards.UnitType;
            }

            // Display the other Square Meters values.
            TextBox_Output_2.Text = theOutputValue.ToString();
        }
    }
}