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...
        }

    }
}
Community
  • 1
  • 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. – Peter Duniho Aug 25 '16 at 23:55

4 Answers4

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 .

Dark
  • 89
  • 4
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?

Community
  • 1
  • 1
h0od
  • 69
  • 2
  • 3
  • _"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. – Peter Duniho Aug 25 '16 at 23:52
1

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

Tim Oehler
  • 109
  • 3
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.

Stef Geysels
  • 1,023
  • 11
  • 27