|
-
Apr 23rd, 2006, 05:20 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2.0] probably a simple solution...
I have a multiline textbox. Is there any way to add a single character as the first character on every line in the textbox?
-
Apr 23rd, 2006, 05:45 AM
#2
Re: [2.0] probably a simple solution...
Do you mean after the fact or as the text is entered? If it's after the fact then you just get the Lines array and add the character to each element. If it's as the text is entered then you'd have to handle an event and append the character. You may have to experiment a bit but I think KeyUp should do the trick.
-
Apr 23rd, 2006, 06:03 AM
#3
Re: [2.0] probably a simple solution...
Just tested this and it works, although you'll have to add the character to the first line yourself:
Code:
private void TextBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.TextBox1.AppendText("X");
}
}
-
Apr 23rd, 2006, 06:44 AM
#4
Thread Starter
Hyperactive Member
Re: [2.0] probably a simple solution...
This is what i mean. Lets say the textbox contains this:
Code:
This is line1
this is line 2
this is line3
4
5
6
i want to add a character at the begining of every line like:
Code:
*This is line1
*this is line 2
*this is line3
*4
*5
*6
Sorry for not being so clear in my first post.
-
Apr 23rd, 2006, 08:20 AM
#5
Re: [2.0] probably a simple solution...
Then, like I said in my first post, you get the Lines property and add the character to each element.
-
Apr 23rd, 2006, 10:21 AM
#6
Thread Starter
Hyperactive Member
Re: [2.0] probably a simple solution...
 Originally Posted by jmcilhinney
Then, like I said in my first post, you get the Lines property and add the character to each element.
I understand what you mean, and thats the way i was going to do it, but i don't understand how (if that makes sense). I have this:
Code:
void BtnAddCharClick(object sender, System.EventArgs e)
{
for (int i = 0; i < tbx.Lines.Length; i++)
{
// Stuck from here :S
}
}
-
Apr 23rd, 2006, 05:38 PM
#7
Re: [2.0] probably a simple solution...
Lines returns an array of String. If you want to add a character to the beginning of a String you would use the '+' concatenation operator. If you want to add a character to the beginning of every element of a String array you would use the concatenation operator in a loop. Once you've made the changes to the array you can then assign it back to the Lines property if you want those changes displayed in the TextBox:
Code:
string[] lines = this.textBox1.Lines;
for (int i = 0; i < lines.Length; i++)
{
lines[i] = "*" + lines[i];
}
this.textBox1.Lines = lines;
Note that Lines is not a live property. By that I mean it is not an existing object that the property returns a reference to. A new array is created each time you get the property. For that reason I have got it only once in the code above. That also means that if you want the changes you make reflected in the TextBox you must assign the changed array back to the property, as I have done in the code above.
-
Apr 24th, 2006, 02:39 PM
#8
Thread Starter
Hyperactive Member
Re: [2.0] probably a simple solution...
Thanks. I thought it would be something similar to that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|