0

I have a label1 with Size(300, 100). The text comes from the server during the runtime and it varies, so I don't have the fixed text, but it fits in my label in 2-3 lines.

I have to find out WHEN IT STARTS A NEW LINE so I can add something to the beginning of each line.

I mean if the text is:

"aaaaaaaaaaaaaa

bbbbbbbbbbbbbb

ccccccccccc"

I have to add some character to each line and get something like this:

"aaaaaaaaaaaaaa

$bbbbbbbbbbbbbb

$ccccccccccc"

So far I've tried to play with TextRenderer class, but I think my solution is weird.

Any ideas?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
GugaMelkadze
  • 157
  • 1
  • 3
  • 11

2 Answers2

1

I'm definetly sure there's a new line '\n' character in your string, it won't be visible in the label but it is there. The only reason the label can display it in different lines it because there is a new line character.

aaaaaaaaaaaaaa

bbbbbbbbbbbbbb

ccccccccccc

Just try the following:

yourLabel.Text = yourString.Replace("\n", "\n$");

//Hope it helps

Bayeni
  • 1,046
  • 9
  • 16
1

One option would be to use a TextBox (or even RichTextBox), you can change the boarder style of this to None and the background colour so that it looks more like a label. If you use a TextBox set the Multiline property to true.

Then when you get your blob you could do something like this.

(please note this is very rough hacky type code, but it will give you a potential picture).

// Imagining you got your blob of data, spaces and all, in one hit.  
string blob = myWayOfGettingData;
textBox1.Text = blob;
int lineIndex = 0;
string[] allLines = new string[textBox1.Lines.Count()];
allLines = textBox1.Lines;
foreach (string line in textBox1.Lines)
{
    if (!string.IsNullOrEmpty(line))
    {
         allLines[lineIndex] = "$" + allLines[lineIndex];       
     }
     lineIndex++;
 }
 textBox1.Lines = allLines;

In the example I used I had a text file which contained your sample data, done a StreamReader ReadToEnd and went on from there.

MattR
  • 641
  • 3
  • 17
  • 38