Home > Software engineering >  Trying to make the method of a button click use variable in WPF
Trying to make the method of a button click use variable in WPF

Time:03-23

I am new to WPF and Coding in general, I am trying to create a small UI to read and write data on .txt files. All the reading part functions works well but now I am having trouble making a small UI to display the information found in the .txt files.

My problem is more with how button clicks work in WPF. All the data is stored in the forms of object of class Static_Data_Scheme and Dynamic_Data_Scheme inside the program, these class are just simple collection of Dictionaries Objects. Part of my data is 'static' meaning it will be stored in .txt files that won't change location and so can be loaded when the program is started, and another part of my data is 'Dynamic' meaning it can be stored anywhere and the file path can be changed using a file explorer in the program. Important thing to note, the Static_Data_Scheme is needed to generate the Dynamic_Data_Scheme.

My initial way of handling it when I made the program to test it out was to generates both Data Scheme with the same button press called load, but since the static dictionaries can be loaded right at the start of the program I want to try and add that method to the MainWindow instead and only have the program generates the Dynamic_Data_Scheme when I press the load button, but I'm unable to find any documentation on how to add arguments to the click method of the button.

Current code that works but that I don't like due to the fact that Static_Data_Scheme.Start method is called each time the load button is pressed and could instead be loaded only once when the program starts :

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Save_Loader_Click(object sender, RoutedEventArgs e)
    {
        Static_Data_Scheme static_Data_Scheme = new Static_Data_Scheme();
        static_Data_Scheme = static_Data_Scheme.Start();
        Dynamic_Data_Scheme dynamic_Data_Scheme = new Dynamic_Data_Scheme();
        Save_Parser.Parse_Save(@"file_path", static_Data_Scheme, ref dynamic_Data_Scheme);
    }

What I wanna try to achieve is something like that :

    public MainWindow()
    {
        InitializeComponent();
        Static_Data_Scheme static_Data_Scheme = new Static_Data_Scheme();
        static_Data_Scheme = static_Data_Scheme.Start();
    }

    private void Save_Loader_Click(object sender, RoutedEventArgs e)
    {
        Dynamic_Data_Scheme dynamic_Data_Scheme = new Dynamic_Data_Scheme();
        Save_Parser.Parse_Save(@"file_path", static_Data_Scheme, ref dynamic_Data_Scheme);
    }

But this doesn't work due to the fact that the Save_Parser.Parse_Save method lack the static_Data_Scheme variable and I can't add it to the Save_Loader_Click method either.

So my question is how do I tell my ave_Loader_Click method to get the static_Data_Scheme from the program?

CodePudding user response:

You almost had it, just move the variable outside of your method:

    Static_Data_Scheme static_Data_Scheme = new();

    public MainWindow()
    {
        InitializeComponent();
        /* static_Data_Scheme = ???? */static_Data_Scheme.Start();
    }

    private void Save_Loader_Click(object sender, RoutedEventArgs e)
    {
        var Data_Scheme = new Dynamic_Data_Scheme();
        Save_Parser.Parse_Save(@"file_path", static_Data_Scheme, ref dynamic_Data_Scheme);
    }
  • Related