0

Firstoff I have to say that I am new to the c# programming. My problem is, that I have a window with a textbox and a button in it and what I am trying to accomplish is to write some text into the textbox and on button click I'd like to save that text into the ukony.txt file. But using the code bellow, after clicking a button nothing happens.

public partial class Window1 : Window {
    public Window1() {
        InitializeComponent();

    }

    private void button_Click(object sender, RoutedEventArgs e) 
        {
        string writerfile = @"D:\Games\ukony.txt";
        Window1 a = new Window1();
        using (StreamWriter writer = new StreamWriter(writerfile)) 
            {
            writer.WriteLine(a.textBlock.Text);
            writer.WriteLine(a.textBlock1.Text);
            }
        }
    }
3166
  • 11
  • 1
  • 6
  • 4
    don't make new instance of `Window`, because you're making "new" Windows with empty `TextBox`. Use `this`, current instance (or current window) of `Window`, like this: `writer.WriteLine(this.textBlock.Text);` – Nino Jun 06 '17 at 13:16
  • Cases: 01) Window1 is initialized on button click event where the textbox text will always be empty, 02) it is possible that event might not be binded – Vicky S Jun 06 '17 at 13:17
  • Possible duplicate of [Append lines to a file using a StreamWriter](https://stackoverflow.com/questions/7306214/append-lines-to-a-file-using-a-streamwriter) – Felix D. Jun 06 '17 at 13:19
  • Set a breakpoint in the `string writerfile = @"D:\Games\ukony.txt";` line and check if it stops. If it does not, check the `InitializeComponent()` function, it should contain a line similar to `button.Click += Eventhandler(button_Click)` – Cleptus Jun 06 '17 at 13:25
  • is the file at least created? – Mong Zhu Jun 06 '17 at 13:31

3 Answers3

1

The reason for not working is the newly created instance of the Window1 class. which is entirely different from the UI that you are actually seeing. So you need not to create an instance at that place, directly use the textBox name to access the text

private void button_Click(object sender, RoutedEventArgs e) 
{
    string writerfile = @"D:\Games\ukony.txt";
    using (StreamWriter writer = new StreamWriter(writerfile)) 
    {
        writer.WriteLine(textBlock.Text);
        writer.WriteLine(textBlock1.Text);
    }
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

don't use a new instance of the window. Use the current one. To access the textblocks of the current instance you should use the this keyword:

private void button_Click(object sender, RoutedEventArgs e) 
{
    string writerfile = @"D:\Games\ukony.txt";

    using (StreamWriter writer = new StreamWriter(writerfile)) 
    {
        writer.WriteLine(this.textBlock.Text);
        writer.WriteLine(this.textBlock1.Text);
    }
}

Problem in detail: with this line:

Window1 a = new Window1();

you create a new Window with empty controls. These are not the same as the ones you see on the screen and in which you have probably typed in something.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
0

Why do you want to use a StreamWriter? I think it's more easier like this:

private void button_Click(object sender, RoutedEventArgs e) 
{
    string writerfile = @"D:\Games\ukony.txt";

    System.IO.File.WriteAllText(writerFile, this.textBlock.Text);
    System.IO.File.AppendAllText(writerFile, this.textBlock1.Text);
}
user11909
  • 1,235
  • 11
  • 27