Why does my public string not change between windows in C# (.net 8) and WPF?

T9 20 Reputation points
2024-02-26T01:35:43.9066667+00:00

Hi, I am trying to make a note app, so I need to change a public string value between two windows but the string value does not persist across the windows. The strings that are not changing are in the 2nd .cs file, "string edit_File_Name", "string file_Type", and "bool from_New_Note". When I made stop points to check, the data of the strings came up as 'null'. I also added this bit of code into line 84 to see if the 2 windows were in contact with each other, and "adf" did appear in the textbox.

edit_Note.txtBox.Text = "adf";

(change the file location to where you want to keep it. In Notepad save files as either .ntr or .ntd) Thanks.

<Window x:Class="Notery.Open_Note"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Notery"
        mc:Ignorable="d"
        Title="Open_Note" Height="450" Width="800">
    <Grid>
        <ListBox x:Name="lstbx" HorizontalAlignment="Left" Height="307" Margin="104,87,0,0" VerticalAlignment="Top" Width="296" d:ItemsSource="{d:SampleData ItemCount=5}" IsSynchronizedWithCurrentItem="False" SelectionChanged="lstbx_SelectionChanged"/>
        <RadioButton x:Name="rnBtn" Content="Regular Note" HorizontalAlignment="Left" Height="21" Margin="482,102,0,0" VerticalAlignment="Top" Width="107" Checked="rnBtn_Checked"/>
        <RadioButton x:Name="dnBtn" Content="Draft Note" HorizontalAlignment="Left" Height="21" Margin="482,128,0,0" VerticalAlignment="Top" Width="107" RenderTransformOrigin="0.525,2.339" Checked="dnBtn_Checked"/>
        <RadioButton x:Name="wnBtn" Content="Warning Note" HorizontalAlignment="Left" Height="21" Margin="482,154,0,0" VerticalAlignment="Top" Width="107" RenderTransformOrigin="0.525,2.339" Checked="wnBtn_Checked"/>
        <Button Content="Open" HorizontalAlignment="Left" Height="61" Margin="482,257,0,0" VerticalAlignment="Top" Width="107" FontSize="40" Click="Button_Click"/>
    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Notery;
/// <summary>
/// Interaction logic for Open_Note.xaml
/// </summary>
public partial class Open_Note : Window
{
    DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\smorg\Downloads\Notery\Tests");
    private string selectedType = ".ntr";
    public Open_Note()
    {
        InitializeComponent();
        FileInfo[] files = directoryInfo.GetFiles("*.ntr");
        foreach (FileInfo file in files)
        {
            lstbx.Items.Add(file.Name);
        }
        lstbx.SelectedIndex = 0;
    }
    private void lstbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    }
    private void rnBtn_Checked(object sender, RoutedEventArgs e)
    {
        lstbx.Items.Clear();
        FileInfo[] files = directoryInfo.GetFiles("*.ntr");
        foreach (FileInfo file in files)
        {
            lstbx.Items.Add(file.Name);
        }
        lstbx.SelectedIndex = 0;
        selectedType = ".ntr";
    }
    private void dnBtn_Checked(object sender, RoutedEventArgs e)
    {
        lstbx.Items.Clear();
        FileInfo[] files = directoryInfo.GetFiles("*.ntd");
        foreach (FileInfo file in files)
        {
            lstbx.Items.Add(file.Name);
        }
        lstbx.SelectedIndex = 0;
        selectedType = ".ntd";
    }
    private void wnBtn_Checked(object sender, RoutedEventArgs e)
    {
        lstbx.Items.Clear();
        FileInfo[] files = directoryInfo.GetFiles("*.ntw");
        foreach (FileInfo file in files)
        {
            lstbx.Items.Add(file.Name);
        }
        lstbx.SelectedIndex = 0;
        selectedType = ".ntw";
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
      //  string selected = @"C:\Users\smorg\Downloads\Notery\Tests\" + lstbx.SelectedItem.ToString();
        Edit_Note edit_Note = new Edit_Note();
        edit_Note.Show();
        edit_Note.edit_File_Name = @"C:\Users\smorg\Downloads\Notery\Tests\" + lstbx.SelectedItem.ToString();
        edit_Note.from_New_Note = false;
        edit_Note.note_Type = selectedType;
        this.Close();
    }
}
<Window x:Class="Notery.Edit_Note"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Notery"
        mc:Ignorable="d"
        Title="Edit_Note" Height="450" Width="800">
    <Grid>
        <TextBox x:Name="txtBox" HorizontalAlignment="Left" Height="117" Margin="209,246,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="253" TextChanged="TextBox_TextChanged"/>
        <Button Content="X" HorizontalAlignment="Left" Height="47" Margin="702,27,0,0" VerticalAlignment="Top" Width="60" FontSize="30" Click="Button_Click_1"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="1079,231,0,0" VerticalAlignment="Top" Width="0"/>
        <Button Content="Save" HorizontalAlignment="Left" Height="47" Margin="462,27,0,0" VerticalAlignment="Top" Width="94" FontSize="36" Click="Button_Click"/>
        <Button Content="Home" HorizontalAlignment="Left" Height="60" Margin="26,27,0,0" VerticalAlignment="Top" Width="71" FontSize="24" Click="Button_Click_3"/>
        <Button x:Name="btnx" Content="X" HorizontalAlignment="Left" Height="40" Margin="655,131,0,0" VerticalAlignment="Top" Width="46" FontSize="20" Click="Button_Click_2" Visibility="Hidden"/>
        <Label x:Name="lbl" Content="Saved" HorizontalAlignment="Left" Height="40" Margin="577,131,0,0" VerticalAlignment="Top" Width="73" FontSize="22" Visibility="Hidden"/>
    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Notery;
/// <summary>
/// Interaction logic for Edit_Note.xaml
/// </summary>
public partial class Edit_Note : Window
{
    public string edit_File_Name;
    public bool from_New_Note;
    public string note_Type;

    public Edit_Note()
    {
        InitializeComponent();

        if (from_New_Note == true)
        {
            txtBox.Text = "";
        } else
        {
           // StreamReader sr = new StreamReader(edit_File_Name);
           // txtBox.Text = sr.ReadToEnd();
        }

    }

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string f = @"C:\Users\smorg\Downloads\Notery\Tests\" + edit_File_Name + note_Type;
        var txt = new StreamWriter(f);
        txt.Write(txtBox.Text);
        btnx.Visibility = Visibility.Visible;
        lbl.Visibility = Visibility.Visible;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        this.Close();
    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        btnx.Visibility = Visibility.Hidden;
        lbl.Visibility = Visibility.Hidden;
    }

    private void Button_Click_3(object sender, RoutedEventArgs e)
    {
        /*MainWindow mainWindow = new MainWindow();         mainWindow.Show();        
	   this.Close();*/
    }
 }

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,708 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,600 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 47,256 Reputation points Microsoft Vendor
    2024-02-26T07:55:23.16+00:00

    Hi,@T9. Welcome to Microsoft Q&A .

    The issue is likely related to the fact that you're checking the values of from_New_Note and other variables in the constructor of the Edit_Note window. At the time of the constructor call, these values are not yet set, so they will have their default values (null for strings and false for bool).

    Here's a modification to help resolve this issue(I updated some code based on testing using replacement files):

      public Edit_Note(string editFileName, bool fromNewNote, string noteType)
      {
        InitializeComponent();
        // Use the passed parameters
        edit_File_Name = editFileName;
        from_New_Note = fromNewNote;
        note_Type = noteType;
        if (from_New_Note)
        {
          txtBox.Text = "";
        }
        else
        {
          // StreamReader sr = new StreamReader(edit_File_Name);
          // txtBox.Text = sr.ReadToEnd();
        }
      }
    ... 
     private void Button_Click(object sender, RoutedEventArgs e)
     {
     
       string f = System.IO.Path.Combine(edit_File_Name);
       var txt = new StreamWriter(f);
       txt.Write(txtBox.Text);
       btnx.Visibility = Visibility.Visible;
       lbl.Visibility = Visibility.Visible;
     
     }
    
    

    Open_Note.xaml.cs:

      private void Button_Click(object sender, RoutedEventArgs e)
      {
       
        Edit_Note edit_Note = new Edit_Note(
         @"E:\Test\" + lstbx.SelectedItem.ToString(),
         false, // Assuming from_New_Note is always false in this case
         selectedType);
        edit_Note.Show();
       
        this.Close();
      }
    
    
    

    By passing the values as parameters to the Edit_Note constructor, you ensure that the variables are correctly initialized when the Edit_Note window is created.

    User's image

    If the issue persists, please feel free to provide more details about the issue (screenshots of the issue) and steps to reproduce the issue.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful