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