0

I am new and trying to do something in c# but I dont know how to solve this prbolem.

My MainWindow.xaml.cs I have a RichTextBox with name RTB that I wanna save into text file.

I created new file Saving.cs where I would write every thing that I need for saving. This is my code.

Saving.cs

public string VarName = "";
    public void Save()
    {
        MainWindow main = new MainWindow();
        TextRange range = new TextRange(main.RTB.Document.ContentStart, main.RTB.Document.ContentEnd);
        FileStream stream = new FileStream(VarName, FileMode.Create);
        range.Save(stream, DataFormats.Text);
        stream.Close();
    }

MainWindow.xaml.cs

private void Button_Save(object sender, RoutedEventArgs e)
    {
        saveDoc.VarName = SaveName.Text + ".txt";
        saveDoc.Save();
    }

Everything works fine except that there is nothing in saved document. My problem is that I really dont know how to acces RTB from different file. It works when I put this code into MainWindow.xaml.cs but not when its in different file like Saving.cs.

I also dont know if its ok to have this in my Saving.cs "MainWindow main = new MainWindow();" or how to aproach this problem.

Thanks for your help kind stranger.

  • welcome to stackoverflow threedust. this is a good question. however, it would be easier if you pass the text from rich text box into your save method. if you insist accessing the text box directly from another class you can pass the text box, or use [findcontrol thing](https://stackoverflow.com/questions/3898588/find-control-by-name-from-windows-forms-controls) against the windows form. – Bagus Tesa Apr 14 '22 at 12:07
  • `dont know how to acces RTB from different file`, you shouldn't be accessing the control from another class to get the data IMO. You should have an instance of the class with a property that you can get from this class. Something like creating a new instance of `Saving.cs` that takes `main.RTB.Document` as a parameter on the contructor. Then you can set some properties in the `Saving.cs` to `ContentStart` and `ContentEnd`. Finally your `Save` routine would have what it needs to actually save that data. Could you update your post to include the definition of `Saving.cs`? – Trevor Apr 14 '22 at 12:09
  • Looking a little more into your post, `MainWindow main = new MainWindow();` you're constructing a new instance, you already have one to work with and should be using that. Also it appears you're using `wpf`, if so, I would recommend using data binding with `mvvm` if you could start now. Then all of this could be implemented more easily. – Trevor Apr 14 '22 at 12:15
  • new MainWindow() merely keeps the compiler happy, it won't give you a correct reference to a text box filled with text. The Save() method needs a parameter, FlowDocument being an obvious choice, now you can simply call saveDoc.Save(RTB.Document) – Hans Passant Apr 14 '22 at 12:18

1 Answers1

0

I hope these code snippets can help to solve your problem

MainWindow.xaml.cs :

 private void Button_Click(object sender, RoutedEventArgs e)
    {
        // Note: Please dont forget about OOP,
        // dont use 'public' mode on class fields (VarName)
        SaveDoc.MyProperty = SaveName.Text + ".txt"; 

        TextRange range = new TextRange(RTB.Document.ContentStart,RTB.Document.ContentEnd);

        //You can pass this 'range' field as a parameter to Class 'SaveDoc'
        SaveDoc.Save(range);
         
    }

Saving.cs :

class SaveDoc
{

    private static string VarName;
    
    //Use Properties to access fields from another classes
    public static string MyProperty
    {
        get { return VarName; }
        set { VarName = value; }
    }
    
    // Here you can pass the 'range' field from MainWindow as a parameter
    public static void Save(TextRange range)
    {
        FileStream stream = new FileStream(VarName, FileMode.Create);
        range.Save(stream, DataFormats.Text);
        stream.Close();
    }
}

Also you can do like this:

MainWindow.xaml.cs :

private void Button_Click(object sender, RoutedEventArgs e)
    {
        // Note: Please dont forget about OOP,
        // dont use 'public' mode on class fields (VarName)

        SaveDoc.MyProperty = SaveName.Text + ".txt";
        
        string range = new TextRange(RTB.Document.ContentStart,RTB.Document.ContentEnd).Text;
        //You can pass this 'range' field as parameter to Class 'SaveDoc'
        SaveDoc.Save(range);
         
    }

Saving.cs :

class SaveDoc
{
    private static string VarName;

    //Use Properties to access fields from another classes
    public static string MyProperty
    {
        get { return VarName; }
        set { VarName = value; }
    }
    // Here you can pass the 'range' field from MainWindow as a parameter
    public static void Save(string range)
    {
        // Standart algorithm to write something in file:
        FileStream stream = new FileStream(VarName, FileMode.Create);

        //convert string to bytes
        byte[] buf = Encoding.Default.GetBytes(range);

        // writing an array of bytes to a file
        stream.Write(buf, 0, buf.Length); 

        stream.Close();
    }
}
DrRed_LZ
  • 25
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 15 '22 at 00:07