Get user input from a textbox in a WPF application

微笑、不失礼 提交于 2020-01-01 02:33:52

问题


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?


回答1:


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.




回答2:


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>



回答3:


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



回答4:


// 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);
}


来源:https://stackoverflow.com/questions/32563111/get-user-input-from-a-textbox-in-a-wpf-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!