0

I can't figure out how to do this correctly.

The "solution" here: "Get user input from textbox in WPF application" doesn't work, and it's about all I can find.

If I'm missing something simple, or if I'm simply going about this in entirely the wrong way please let me know.

Thanks in advance

<Window x:Class="WpfApplication2.MainWindow"
        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"
        xmlns:local="clr-namespace:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid Background="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}">
        <TextBox x:Name="textBox" TextWrapping="Wrap" Text="TextBox" Margin="114,48,125,158" TextChanged="textBox_TextChanged"/>
        <Button x:Name="button" Content="Button" Margin="188,0,212,86" RenderTransformOrigin="0.5,0.5 Height="38" VerticalAlignment="Bottom" Click="button_Click"">
        </Button> >

    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApplication2
{
     public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void textBox_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            /// trying to get textbox input via button press
            /// this doesn't work:
            string input = textBox_TextChanged.Text;

            ///neither does var input = textBox_TextChanged.Text
            /// or anything else in the textBox_Textchanged position
            /// I'm pulling my hair out...
        }

    }
}
1
  • 1
    As noted, your basic issue here appears to be typos in the XAML. Once the XAML is correct, you should find textBox is in fact a valid identifier and you can get the Text property from it. Note, however, that this whole bit of code falls into the "You're Doing It Wrong" category. You should be using WPF's data binding features to have the Text property automatically bound to the property of a view model, and when the button is clicked, you can get the current value straight from the view model. Then you don't have to name your controls at all or access their properties directly. Commented Aug 25, 2016 at 23:55

4 Answers 4

1

I believe you meant :

input = textBox.Text;

textBox is the name of your text box. At least that's what I see in the xml. The name you used is the name of the method subscribed to TextChanged .

Sign up to request clarification or add additional context in comments.

10 Comments

I've tried that too, and it didn't work either... the name textBox does not exist in current context... :/
Well, that is unexpected. And sounds very wrong. Try changing the name in the designer or the xml and use that name instead. It sounds like it wouldn't know the xml.
@SebastianHahn I just noticed. Look in the xml, you missed a " on the button line.RenderTransformOrigin="0.5,0.5 " Height="38" . And if you add that you might ahve an extra one just at the end of the line.
I changed it to <TextBox x:Name="textBox1"... and string input = textBox1.text; It didn't help.
@SebastianHahn Check my last comment.
|
1

You should get the content of the textbox by the Text property:

string t = textbox.Text;

But there is one thing in your xaml that prevents you from accessing the textbox that way by variable from code-behind, and that's the use of the "x:Name" attribute. Use the "Name" attribute (without namespace decoration) instead and I think it should work.

In WPF, what are the differences between the x:Name and Name attributes?

1 Comment

"one thing in your xaml that prevents you from accessing the textbox that way by variable from code-behind, and that's the use of the "x:Name" attribute" -- simply incorrect. Using x:Name for this purpose is fine.
1

You have an extra quote at the end of your Button tag (after VerticalAlignment="Bottom"").

Comments

0

This is indeed not the right way to approach the text. Several approaches are possible. I'll give you 2:

Approaches 1:

 private void button_Click(object sender, RoutedEventArgs e)
    {
        string input = textBox.Text 
    }

On button_Click you can try to get the Text direct from the named TextBox.

Approaches 2:

namespace WpfApplication2
{
 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private string text { get; set; }

    private void textBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        text=((TextBox)sender).Text;
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        /// whatever you will do 
    }

}
}

Every time you change the TextBox.Text it is stored in a private property "text". On button_Click, you can get the value there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.