[Visual C#] Click button functions in WPF.

Joined
May 28, 2007
Messages
1,023
Reaction score
165
I've been following a rather nice ebook, and I'm a bit stuck on something.
I'm currently making the generic WPF application with a label, a text box, and a button.
When you click the button, it'll say Hello (Name you entered in the text box).

My Window1.Xaml.CS:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFHello
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void ok_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(“Hello “ + userName.Text);
        }
    }
}
I double clicked my OK button in my WPF app to add in the ok_click method, and the ebook told me to add the:
Code:
MessageBox.Show(“Hello “ + userName.Text);

But in the ebook, the snippet that contains that has void ok_Click, not private void.

My Window1.xaml:
Code:
<Window x:Class="WPFHello.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Hello" Height="140" Width="300">
    <Grid>
        <Label Height="28" Margin="12,12,120,0" Name="label1" VerticalAlignment="Top" FontSize="12">Please enter your name.</Label>
        <TextBox Height="23" Margin="21,36,137,0" Name="userName" VerticalAlignment="Top" TextChanged="userName_TextChanged" />
        <Button Height="23" HorizontalAlignment="Right" Margin="0,36,25,0" Name="ok" VerticalAlignment="Top" Width="75" Click="ok_Click">OK</Button>
    </Grid>
</Window>
 
Thanks, ebook told me to directly copy and paste, but I guess not ;]

Now It's:
Code:
private void ok_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Hello " + userName.Text);
        }

And I'm getting the error:
Error 1 'WPFHello.Window1' does not contain a definition for 'userName_TextChanged' and no extension method 'userName_TextChanged' accepting a first argument of type 'WPFHello.Window1' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\Cyrus\My Documents\Visual Studio 2008\Projects\WPFHello\WPFHello\Window1.xaml 7 99 WPFHello

And still no popup on click.
 
Thanks, ebook told me to directly copy and paste, but I guess not ;]

Now It's:
Code:
private void ok_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Hello " + userName.Text);
        }

And I'm getting the error:


And still no popup on click.

Im not 100% sure about this, but I think its here:
Code:
<TextBox Height="23" Margin="21,36,137,0" Name="userName" VerticalAlignment="Top" TextChanged="userName_TextChanged" />
The TextChanged, either define a "userName_TextChanged" function or delete that part.
 
Im not 100% sure about this, but I think its here:
Code:
<TextBox Height="23" Margin="21,36,137,0" Name="userName" VerticalAlignment="Top" TextChanged="userName_TextChanged" />
The TextChanged, either define a "userName_TextChanged" function or delete that part.
I'll play with it.
Thanks BBim.

Edit: Thanks mate, I just deleted that part (TextChanged="userName_TextChanged") out completely, and I'm error free and functioning great.
Time to move on.
Thanks again.
 
Last edited:
Microsoft Visual CSharp 2008 Step by Step - John Sharp.
Rather simple book, mostly copied from the Visual Dev Center.
Fantastic book; without that book, and C# Programming by David Conger, I wouldn't really have gotten anywhere in programming in the first place. I recommend John Sharp's book to anyone who has no previous experience in programming. (And, if you're using WPF, if I'm not mistaken, I'm pretty sure his book covers WPF applications...)
 
Fantastic book; without that book, and C# Programming by David Conger, I wouldn't really have gotten anywhere in programming in the first place. I recommend John Sharp's book to anyone who has no previous experience in programming. (And, if you're using WPF, if I'm not mistaken, I'm pretty sure his book covers WPF applications...)
It does. :]
Thank you, my good sir.
I'll look up that book in a bit after I progress a bit more.
 
Back