Frage

In my code I'm trying to obtain a value from a textbox within my options window when my submit button is clicked. I know I need to covert the string into a double which I've done, but my problem is that distanceOption just get sets to 0.0 instead of the value the user puts into the text box. I think my trouble might be with the linecount of the textbox. My linecount variable keeps showing as -1 and not sure if that is what the value should be.

        void Options_Clicked(object sender, RoutedEventArgs e)
    {
        OptionsWindow optionsWindow = new OptionsWindow();
        optionsWindow.Show();
        optionsWindow.DistanceButton.Click += new RoutedEventHandler(Distance_Clicked);
    }
    void Distance_Clicked(object sender, RoutedEventArgs e)
    {
        OptionsWindow optionsWindow = new OptionsWindow();
                  int lineCount = optionsWindow.DistanceBox.LineCount;
                 this.distanceOption = Convert.ToDouble(optionsWindow.DistanceBox.GetLineText(lineCount));

    }

If there is a better way to obtain the value from the textbox I would be open to that as well. Thanks!

War es hilfreich?

Lösung

Here is an example

public partial class MainWindow : Window
{
    NewWindow optionsWindow = new NewWindow();

    public MainWindow()
    {
        InitializeComponent();

        optionsWindow.button1.Click += new RoutedEventHandler(button1_Click);
        optionsWindow.Show();

    }

    void button1_Click(object sender, RoutedEventArgs e)
    {
        double d = Convert.ToDouble(optionsWindow.textBox1.GetLineText(0));
    }


}

Andere Tipps

Create a proper ViewModel and use DataBinding:

XAML:

<TextBox Text="{Binding MyDouble}"/>

ViewModel:

public double MyDouble {get;set;} //Probably NotifyPropertyChanged
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top