dimanche 9 mai 2021

Working with if statements and booleans in wpf c#

In a WPF form .NET framework, I'm trying to achieve the following (seemingly) simple task:

I have 3 buttons and 3 textboxes: Button 1 Textbox1

Button 2
Textbox2

Button 3
Textbox3

If I click button 1, I want textbox 1 to read true and the other 2 false. If I click button 2, I want textbox 2 to show true and the others false and the same for button 3 and textbox 3 respectively.

I thought I could achieve this by setting the value of all of the Booleans to either true or false depending on the button that has been clicked using the click event, but don't get the expected result

using System;
using System.Windows;
using System.Threading;
using System.Windows.Threading;


namespace WPF_Test
{

    public partial class MainWindow : Window
    {
        bool value1;
        bool value2;
        bool value3;
             
        public MainWindow()
        {
            InitializeComponent();

            if (value1 == true)
             {
               textbox1.Text = value1.ToString();
             } else if (value2 == true){
               textbox2.Text = value2.ToString();
             } else if (value3 == true){
               textbox3.Text = value3.ToString();
             }

        }                                                        

           
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            value1 = true;
            value2 = false;
            value3 = false;
        }

        private  void button2_Click(object sender, RoutedEventArgs e)
        {
            value1 = false;
            value2 = true;
            value3 = false;
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            value1 = false;
            value2 = false;
            value3 = true;
        }


       

    }


}

Any idea what I might be missing?

Kind regards, Q

Aucun commentaire:

Enregistrer un commentaire