15

I am trying to get user input from the textbox in a WPF application I am building. The user will enter a numeric value and I would like to store it in a variable. I am just starting on C#. How can I do it?

Currently I am opening the textbox and letting the user enter the value. After that the user has to press a button upon which the text from textbox is stored in a variable.

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    var h = text1.Text;
}

I know this isn't right. What is the right way?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
crossemup
  • 351
  • 1
  • 3
  • 9
  • Are you trying to convert the input to a number? – Michael McMullin Sep 14 '15 at 11:01
  • 1
    @crossemup If you want to store the value to be stored in a variable after button click. You are doing it correctly... Why u feel like this is not correct? – Sandeep Kushwah Sep 14 '15 at 11:04
  • No i am not trying to convert the input to a number. The input itself is a number. I am looking to store that number into a variabble – crossemup Sep 14 '15 at 11:05
  • 1
    Actually, it's a string. I'm asking if you need it converted to a number, because your code is already storing it correctly. If you need to access it outside the Button_Click event handler, then you simply need to define your variable outside that scope. – Michael McMullin Sep 14 '15 at 11:06
  • 1
    If you want to store the textbox value on button click event then you are going right. you can use Bindings also, but as you are a new bee to c# it will be complex for you. – Akansha Sep 14 '15 at 11:08

4 Answers4

22

Like @Michael McMullin already said, you need to define the variable outside your function like this:

string str;

private void Button_Click(object sender, RoutedEventArgs e)
{
    str = text1.Text;
}

// somewhere ...
DoSomething(str);

The point is: the visibility of variable depends on its scope. Please take a look at this explanation.

Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
8

Well, here is a simple example of how to do this with MVVM.

Firstly write a view-model:

public class SimpleViewModel : INotifyPropertyChanged
{
    private int myValue = 0;

    public int MyValue
    {
        get
        {
            return this.myValue;
        }
        set
        {
            this.myValue = value;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

Then write a converter, so you can translate your string to int and vice-versa:

[ValueConversion( typeof(int), typeof(string))]
class SimpleConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int returnedValue;

        if (int.TryParse((string)value, out returnedValue))
        {
            return returnedValue;
        }

        throw new Exception("The text is not a number");
    }
}

Then write your XAML code like this:

<Window x:Class="StackoverflowHelpWPF5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:[YOURNAMESPACEHERE]"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:SimpleViewModel></local:SimpleViewModel>
    </Window.DataContext>
    <Window.Resources>
        <local:SimpleConverter x:Key="myConverter"></local:SimpleConverter>
    </Window.Resources>
    <Grid>
        <TextBox Text="{Binding MyValue, Converter={StaticResource myConverter}, UpdateSourceTrigger=PropertyChanged}"></TextBox>
    </Grid>
</Window>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mattia Magosso
  • 503
  • 2
  • 9
  • 1
    I'm trying to make something simple: when you press the button, it shows a message box telling you the text inside the text box. Do I need to do all these just to simply get the text inside the text box? Can't I just access the text box directly and call getText() or something? – alxcyl Jan 18 '16 at 06:00
  • 4
    Wow this is a lot of code for something so mundane. And people wonder why I hate MVVM/MVC.... Great presentation, though. – vapcguy Sep 15 '16 at 19:27
8

You can also just give a name to your control:

<TextBox Height="251" ... Name="Content" />

And in the code:

private void Button_Click(object sender, RoutedEventArgs e)
{
    string content = Content.Text;
}
Milo
  • 3,365
  • 9
  • 30
  • 44
bresleveloper
  • 5,940
  • 3
  • 33
  • 47
0
// WPF

// Data
int number;

// Button click event
private void Button_Click(object sender, RoutedEventArgs e) {
    // Try to parse number
    bool isNumber = int.TryParse(text1.Text, out number);
}
Volodymyr Sichka
  • 531
  • 4
  • 10