timm
timm

Reputation: 7

C# Reading a file into an array of strings and printing it into a Textbox

I have a problem with my code as a described it a little bit in the title.

So the thing I want to do is to read a number from a file and printing it into a TextBox but the only thing I can get written there is System.String[].

And here is my code:

private void ladenToolStripMenuItem_Click(object sender, EventArgs e)
{
    // Kontostand aus Datei auslesen und in variable speichern anschließend in tb schreiben

    string[] Kontostand = File.ReadAllLines(pathkonto);

    string tbkontostand = Kontostand.ToString();

    this.lbKontostand.Text = "Kontostand: " + tbkontostand + "€";


    string[] Log = File.ReadAllLines(pathlog);

    string LoginTextbox = Log.ToString();

    this.tbLog.Text = LoginTextbox;
}

Maybe i should say that my Kontostand is an label which i want to look like that:

Kontostand: 500 €

And my file where i want to read that out looks like that:

500

Thanks for helping me out :)

Timm

Upvotes: 1

Views: 311

Answers (4)

Peter Duniho
Peter Duniho

Reputation: 70661

You didn't specify, but it appears you're using Winforms. If so, you should use this:

    string[] Log = File.ReadAllLines(pathlog);

    this.tbLog.Lines = Log;

The default behavior of the ToString() method is to just return the name of the type of the object. The string[] type does not override the default behavior, so that's what you're getting in the text box.

However, the Winforms TextBox class has a Lines property of type string[]. So you just need to set that directly to the string[] you get from reading the file.

In your original code, you also want to include in a Label file lines from a different source, which you can easily do like this:

string[] Kontostand = File.ReadAllLines(pathkonto);

this.lbKontostand.Text = "Kontostand: "
    + string.Join("", Kontostand) 
    + "€";

The Label control doesn't have a Lines property, so in this particular case, you do need to use something like string.Join().

Since Label controls also don't have a multi-line mode, I simply concatenate the contents of the file with the empty string as the separator. There'd be no point in using Environment.NewLine here. You could of course use any string you want instead of "". It's up to you.

Now, from your edited question, it appears you may not need any sort of multi-line support at all, because the data you describe is just a single line. If this is in fact the case, your code can be quite a lot simpler (and more efficient):

private void ladenToolStripMenuItem_Click(object sender, EventArgs e)
{
    this.lbKontostand.Text = "Kontostand: "
        + File.ReadAllText(pathkonto)
        + "€";
    this.tbLog.Text = File.ReadAllText(pathlog);
}

For that matter, if you simply want to copy the file contents into the respective controls, that's the way to do it anyway. Reading the contents in an array, only to copy the entire contents of that array into the control, is less efficient than just reading all of the file text and assigning it to the Text property directly. New-line characters will be read into the string returned by ReadAllText(), so whether the file contents are really multi-line or not, the above should work better than what you were originally trying to do.

Upvotes: 4

Akshey Bhat
Akshey Bhat

Reputation: 8545

string LoginTextbox = string.Join("\r\n", Log);

use string.Join to get a comma separated string of all the values in the array.

Upvotes: 0

Steve
Steve

Reputation: 216290

The first thing you need is to be sure that your TextBox has the Multiline property set to true and that you have sized its height enough to see more than one line. Next, you don't apply the ToString to an array of strings. This just produces the class name because arrays don't have an override of that method and thus they call the base Object.ToString().

Instead you can use AppendText to add first the fixed text, then string.Join to render the lines of your file followed by the final currency symbol.

 private void ladenToolStripMenuItem_Click(object sender, EventArgs e)
 {
    string[] Kontostand = File.ReadAllLines(pathkonto);
    this.lbKontostand.AppendText("Kontostand: " + Environment.NewLine)
    this.lbKontostand.AppendText(string.Join(Environment.NewLine,tbkontostand);
    this.lbKontostand.AppendText(" €");
 }

Upvotes: 3

rmbq
rmbq

Reputation: 473

Log.ToString(); just call ToString method of a string[] type variable. you need to loop all strings in your array

foreach(string s in Log)
   this.tbLog.Text += s+"\n";

Upvotes: 0

Related Questions