Reputation: 11
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...
}
}
}
Upvotes: 0
Views: 2623
Reputation: 109
You have an extra quote at the end of your Button tag (after VerticalAlignment="Bottom"").
Upvotes: 1
Reputation: 69
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?
Upvotes: 1
Reputation: 1047
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.
Upvotes: 0
Reputation: 89
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
.
Upvotes: 1