0

First of all, I have to mention that i'm a newbie at c#.
I'm developing an windows 8 metro app with xaml and c#.
I created an array of checkboxes dynamically and assigned a pointerpressed event to each of them.
the weird problem is that the pointerpressed event is not firing up the registerd function.
If i create the same checkboxes using design tool(I'm using visual Studio 2012), strangely the function does work this time.
As i don't know the number of checkboxes to be created till runtime, using design tool for handling checkboxes isn't an option.

The code i'm running is as below:


CheckBox[] chk=new CheckBox[count];
int x=20; //Width of each checkbox(to be incremented for every iteration)
for( int i = 0; i < count ; i++ )
{
    chk[i] = new CheckBox();
    chk[i].HorizontalAlignment = HorizontalAlignment.Left;
    chk[i].Margin = new Thickness(1100, x, 0, 0);
    chk[i].VerticalAlignment = VerticalAlignment.Top;
    chk[i].Height = 30;
    chk[i].Width = 35;
    chk[i].PointerPressed += new PointerEventHandler(chkhandler);
    gridit.Children.Add(chk[i]);
    x = x + 35;
}
private static void chkhandler(object obj, PointerRoutedEventArgs arg)
{
    textblock1.Text="Testing";  //Sample Textblock

}




The checkboxes get checked during runtime but the associated function doesn't work.
Thanks in advance

nj-ath
  • 3,028
  • 2
  • 25
  • 41
  • 1
    Did you investigate the various events for a Checkbox to determine if you are binding to the correct item. Your example does not indicate what you were expecting but since your are using Checkbox then I'd key off the chk[i].Click or chk[i].Checked. Especially since looking in Visual Studio 2012 and 4.5 framework there is no PointerPressed event for CheckBox. – SASS_Shooter May 15 '13 at 20:26
  • @SASS_Shooter `PointerPressed` might be referring to this: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.pointerpressed – Chris Sinclair May 15 '13 at 20:27
  • Thanks, click works. By the way is there any difference between click event and pointer pressed – nj-ath May 15 '13 at 20:36

1 Answers1

2

Try;

  • making the event handler public & non-static,
  • make sure nobody is eating up the events.

what about this:

chk[i].AddHandler(PointerPressedEvent, 
    new PointerEventHandler(SomeButton_PointerPressed), true);
Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78