Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Silverlight
I am trying to add mutible eventhandlers for a array of TextBlocks, but it seems like it is always the last element of sHeadline[x] that gets the eventhandler. For example if I have 10 elements of sHeadline[x] it is always sHeadline[9] that gets the eventhandler.
How can I fix this?
Thanks!
 
void service_GetSearchForVesselCompleted(object sender, PositionServiceReference.GetSearchForVesselCompletedEventArgs e)
{
  // Number of results
  var numberOfResults = e.Result.Count;
 
  // Variables
  String vesselName;
  SolidColorBrush textColor = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 30, 30, 30));
 
  // Create headlines
  TextBlock[] sHeadline = new TextBlock[numberOfResults];
 
  // Go trough results list
  for (int x = 0; x < numberOfResults; x++)
  {
    // Get vales
    vesselName = e.Result[x].Vessel_Name;
 
    sHeadline[x] = new TextBlock();
    sHeadline[x].TextDecorations = TextDecorations.Underline;
    sHeadline[x].FontSize = 12;
    sHeadline[x].Text = vesselName;
    sHeadline[x].Margin = new Thickness(9, 0, 0, 0);
    sHeadline[x].Height = 15;
    stackPanelSearchResults.Children.Add(sHeadline[x]);
 
    // Add listners
    sHeadline[x].MouseLeftButtonDown += new MouseButtonEventHandler((object senderH, MouseButtonEventArgs eH) => sHeadline_MouseLeftButtonDown(senderH, eH, vesselName, x));
  }
}
 
void sHeadline_MouseLeftButtonDown(object sender, MouseButtonEventArgs e, string inpVesselName, int x)
{
  labelStatus.Content = "Click on " + inpVesselName + " " + x;
}
Posted 21-Feb-13 5:50am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This sort of answers the same question:
 
http://stackoverflow.com/questions/4155691/c-sharp-lambda-local-variable-value-not-taken-when-you-think[^]
 
but basically you are not storing the value of vesselname and x when you create the lambda but the catual variable themselves so they get stuck on their last value for all of those handlers.
  Permalink  
Comments
Solo1233211 at 21-Feb-13 12:30pm
   
Yes, I understand, but how can i achieve this?
Chris Reynolds (UK) at 21-Feb-13 12:36pm
   
Dashing this off before I go home without testing, but you should be able to get the answer from the link. Basically, create variables in the loop
for (int x = 0; x < numberOfResults; x++)
{
// Get vales
string LoopvesselName = e.Result[x].Vessel_Name;
int loopx = x;
sHeadline[x].MouseLeftButtonDown += new MouseButtonEventHandler((object senderH, MouseButtonEventArgs eH) => sHeadline_MouseLeftButtonDown(senderH, eH, LoopvesselName, loopx));
}
Solo1233211 at 21-Feb-13 12:52pm
   
Thank you! This is a very elegant solution!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 398
1 DamithSL 235
2 OriginalGriff 230
3 Zoltán Zörgő 149
4 Dave Kreskowiak 120
0 OriginalGriff 7,575
1 DamithSL 5,529
2 Sergey Alexandrovich Kryukov 5,279
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100