Belgardo
Belgardo

Reputation: 3

WPF Button content to Textbox

I'm trying to make a calculator and its my first time coding using anything with a GUI

I'm using Visual Studio 2012 WPF using C# application

How do i get the button content to the calculator textbox (screen) ?

XAML:

<Button x:Name="one" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" 
        Foreground="White" FontSize="20" FontWeight="Bold" Width="50" Height="41"
        Content="1" Margin="31,350,491,64" Click="button_Click"
        HorizontalAlignment="Left" VerticalAlignment="Bottom"/>

C#:

private void button_Click(object sender, EventArgs e)
{   
    Button b = (Button)sender;
    screen.Text = screen.Text + b.Text;
}

it underlines the Text next to the b.

Error 1 'System.Windows.Controls.Button' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.Windows.Controls.Button' could be found (are you missing a using directive or an assembly reference?)

Upvotes: 0

Views: 4169

Answers (2)

eddie_cat
eddie_cat

Reputation: 2583

Time to learn about MSDN! Here is the page for the Button class. You will notice there is no Text property, but there is a Content property. You will have to cast to string as Content is of type object.

screen.Text = screen.Text + b.Content.ToString();

Note that you could simplify this code to:

screen.Text += b.Content.ToString();

Upvotes: 4

apomene
apomene

Reputation: 14389

Use:

 screen.text=screen.Text +(String)b.Content

instead of:

b.Text 

in WPF the text of a button is accessed via Button.Content property

Upvotes: 0

Related Questions