O'Reilly Forums: "unable To Add Event Handler" - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

"unable To Add Event Handler" head first c# lab1

#1 User is offline   Suey 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 11-July 14

Posted 11 July 2014 - 11:18 AM

I am trying to work my way through Lab 1. I am using win7 64 bit and visual studio 2013. I am using the alternative directions using the .wpf. When I get to the point of double clicking the start button so it would create a default method, it gave me "unable to add event handler". I have tried manually adding what the book had listed but was unsure where it needed to go and it broke it anyway. I would appreciate any ideas! :rolleyes:
0

#2 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 801
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 12 July 2014 - 09:39 AM

Are you working on lab 1 and having trouble with the button to start the race? Or "Save the Humans" in the WPF PDF and having trouble with the button to start the game? Either way, can you post your code?
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#3 User is offline   Suey 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 11-July 14

Posted 12 July 2014 - 12:16 PM

I am using the wpf pdf and having trouble with the start button to start the game. Looking at what other people have done though, I am sure I have made some kind of massive error. I gonna scrap what I have and then start all over. Forgive me, I am brand new at this. Hear it is anyway though. Thanks. :)


<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="Save_the_Humans.MainWindow"
Title="Save_the_humans" Height="700" Width="1000">
<Window.Resources>
<ControlTemplate x:Key="EnemyTemplate" TargetType="{x:Type ContentControl}">
<Grid>
<Ellipse Fill="#FFEA2B18" Height="100" Stroke="Black" Width="100"/>
</Grid>
</ControlTemplate>
</Window.Resources>

<Grid Background="Black">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140"/>
<ColumnDefinition/>
<ColumnDefinition Width="160"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="150"/>
</Grid.RowDefinitions>



<ProgressBar Grid.Column="1" Grid.Row="1" Height="20"/>
<StackPanel Grid.Column="2" Orientation="Vertical" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">

<TextBlock TextWrapping="Wrap" Text="Avoid These" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}"/>
<ContentControl Content="ContentControl" RenderTransformOrigin="0.021,-0.105" Template="{DynamicResource EnemyTemplate}"/>
</StackPanel>
<Canvas x:Name="playArea" Grid.ColumnSpan="3">
<Canvas.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF9E70CD" Offset="0"/>
<GradientStop Color="#FF48C96B" Offset="1"/>
</LinearGradientBrush>
</Canvas.Background>
<StackPanel x:Name="human" Canvas.Left="10" Orientation="Vertical">
<Ellipse Fill="#FFF7F1F1" Height="10" Stroke="Black" Width="10"/>
<Rectangle Fill="#FFF5F1F0" Height="25" Stroke="Black" Width="10"/>
</StackPanel>
<TextBlock x:Name="gameOverText" Canvas.Left="243" TextWrapping="Wrap" Text="Game Over" Canvas.Top="241" FontFamily="Arial" FontWeight="Bold" FontStyle="Italic" FontSize="100"/>
<Rectangle Height="50" Canvas.Left="690" Stroke="Black" Canvas.Top="42" Width="50" RenderTransformOrigin="-0.079,0.25">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="45"/>
<TranslateTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFA6A493" Offset="0"/>
<GradientStop Color="#FFEACF23" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Button x:Name="startButton" Content="Start!" HorizontalAlignment="Center" VerticalAlignment="Center" Width="76" Click="starButton_click" Canvas.Left="32" Canvas.Top="584"/>
</Canvas>

</Grid>
</Window>
0

#4 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 801
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 13 July 2014 - 08:59 AM

I don't think you need to start over! :) I creatde a new project called "Save the Humans" (it needs to have the same name) and pasted your XAML code into it, and when I double-clicked on the Start button it created the method just fine. I was able to run the program and see your main page.

I do see a problem with the XAML you posted:
<Button x:Name="startButton" Content="Start!" HorizontalAlignment="Center" VerticalAlignment="Center" Width="76" Click="starButton_click" Canvas.Left="32" Canvas.Top="584"/>

Notice how it says Click="starButton_click"? I'm not sure how it got that way, but the method name is supposed to be "startButton_Click" -- it's missing the "t" and the "C" is lowercase.

But once I corrected that line, I was able to paste in the code from http://hfcsharp.codeplex.com/ (in the Save_the_Humans_pages_20-32 project, file MainWindow.xaml.cs) and it worked just fine!

Here's a direct link to the code I pasted in: http://hfcsharp.code...nWindow.xaml.cs

I'm still not quite sure why you were getting that error message, but I think if you just create a new project called "Save the Humans" (you may need to move or delete your old project because it has the same name), paste your existing XAML code into MainWindow.xaml, and change starButton_click to startButton_Click, it should work just fine.

And you should definitely feel confident that you did everything right so far, because your XAML code worked great for me (with that one tiny change)!
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#5 User is offline   Suey 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 11-July 14

Posted 14 July 2014 - 09:36 AM

Thank you so much!!!!! :D
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users