Select to view content in your preferred language

Press Enter key to trigger the locate address

488
1
09-20-2011 04:55 AM
jerrysalinas
Deactivated User
Hi,
I am trying to figure out how I can add code to allow the user to press the enter key after entering an address and begin the address search. I think I need to use the keydown event, but not sure. Thanx.

I saw this code:
    private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
                System.Diagnostics.Debug.WriteLine(e.Key);
        }

here is the LocatorButton_click
  private void LocatorButton_Click(object sender, RoutedEventArgs e)
        {
            this.IsBusy = true; // Make Busy Signal Visible

            if (locatorRadio_Address.IsChecked.Value && !string.IsNullOrEmpty(txtAddress.Text))
            {
                AddressHelper addressParser = new AddressHelper(txtAddress.Text);
                geocodeTool.GeocodeAddress(addressParser.Street1, addressParser.City, addressParser.State, addressParser.ZipCode, addressParser.Country);
            }
            else if (locatorRadio_Coords.IsChecked.Value && !string.IsNullOrEmpty(txtLongitude.Text) && !string.IsNullOrEmpty(txtLatitude.Text))
            {
                double lon = double.Parse(txtLongitude.Text);
                double lat = double.Parse(txtLatitude.Text);
                MapPoint point = new MapPoint(lon, lat, new SpatialReference(4326));
                geocodeTool.ReverseGeocode(point);
            }
        }
0 Kudos
1 Reply
JenniferNery
Esri Regular Contributor
Have you looked at this SDK sample? http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#AddressToLocation

You can modify the sample by refactoring code from FindAddressButton_Click (i.e. ExecuteFindAddress()) and subscribing to KeyUp/KeyDown event on all of your input TextBox (InputAddress, State, Zip).

private void FindAddressButton_Click(object sender, RoutedEventArgs e)
{
 ExecuteFindAddress();
}

private void ExecuteFindAddress()
{
 _locatorTask = new Locator("http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Streets_US_10/GeocodeServer");
 _locatorTask.AddressToLocationsCompleted += LocatorTask_AddressToLocationsCompleted;
 _locatorTask.Failed += LocatorTask_Failed;

 AddressToLocationsParameters addressParams = new AddressToLocationsParameters()
 {
  OutSpatialReference = MyMap.SpatialReference
 };

 Dictionary<string, string> address = addressParams.Address;

 if (!string.IsNullOrEmpty(InputAddress.Text))
  address.Add("Street", InputAddress.Text);
 if (!string.IsNullOrEmpty(City.Text))
  address.Add("City", City.Text);
 if (!string.IsNullOrEmpty(State.Text))
  address.Add("State", State.Text);
 if (!string.IsNullOrEmpty(Zip.Text))
  address.Add("ZIP", Zip.Text);

 _locatorTask.AddressToLocationsAsync(addressParams);
}

private void InputAddress_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
 if (e.Key == System.Windows.Input.Key.Enter)
 {
  ExecuteFindAddress();
 }
}
0 Kudos